Skip to content

Commit

Permalink
Merge pull request #607 from skadefro/master
Browse files Browse the repository at this point in the history
Guess data type in ReadRange
  • Loading branch information
skadefro authored Apr 24, 2022
2 parents a352a01 + c379492 commit bbd7105
Showing 1 changed file with 11 additions and 50 deletions.
61 changes: 11 additions & 50 deletions OpenRPA.Office/Activities/ReadRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,16 @@ protected override void Execute(CodeActivityContext context)
if (string.IsNullOrEmpty(cells))
{
range = base.worksheet.UsedRange;

//Range last = base.worksheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing);
//Range range = base.worksheet.get_Range("A1", last);

//int lastUsedRow = range.Row;
//int lastUsedColumn = range.Column;
}
else
{
if (!cells.Contains(":")) throw new ArgumentException("Cell should contain a range dedenition, meaning it should contain a colon :");
range = base.worksheet.get_Range(cells);
}
//object[,] valueArray = (object[,])range.Value;
object[,] valueArray = (object[,])range.get_Value(Microsoft.Office.Interop.Excel.XlRangeValueDataType.xlRangeValueDefault);
if(valueArray != null)
{
var o = ProcessObjects(useHeaderRow, ignoreEmptyRows, valueArray);
var o = ProcessObjects(range, useHeaderRow, ignoreEmptyRows, valueArray);

System.Data.DataTable dt = o as System.Data.DataTable;
dt.TableName = base.worksheet.Name;
Expand All @@ -79,13 +72,6 @@ protected override void Execute(CodeActivityContext context)

}


//dt.AsEnumerable();

//string json = Newtonsoft.Json.JsonConvert.SerializeObject(dt, Newtonsoft.Json.Formatting.Indented);
////context.SetValue(Json, JObject.Parse(json));
//context.SetValue(Json, JArray.Parse(json));

if (ClearFormats.Get(context))
{
worksheet.Columns.ClearFormats();
Expand All @@ -94,38 +80,6 @@ protected override void Execute(CodeActivityContext context)

if (lastUsedColumn!=null || lastUsedRow!=null)
{

// Unhide All Cells and clear formats

// Detect Last used Row - Ignore cells that contains formulas that result in blank values
//int lastRowIgnoreFormulas = worksheet.Cells.Find(
// "*",
// System.Reflection.Missing.Value,
// XlFindLookIn.xlValues,
// XlLookAt.xlWhole,
// XlSearchOrder.xlByRows,
// XlSearchDirection.xlPrevious,
// false,
// System.Reflection.Missing.Value,
// System.Reflection.Missing.Value).Row;
// Detect Last Used Column - Ignore cells that contains formulas that result in blank values
//int lastColIgnoreFormulas = worksheet.Cells.Find(
// "*",
//System.Reflection.Missing.Value,
// System.Reflection.Missing.Value,
// System.Reflection.Missing.Value,
// XlSearchOrder.xlByColumns,
// XlSearchDirection.xlPrevious,
// false,
// System.Reflection.Missing.Value,
// System.Reflection.Missing.Value).Column;

// Detect Last used Row / Column - Including cells that contains formulas that result in blank values
//int lastColIncludeFormulas = worksheet.UsedRange.Columns.Count;
//int lastColIncludeFormulas = worksheet.UsedRange.Rows.Count;



//range = base.worksheet.UsedRange;
int _lastUsedColumn = worksheet.UsedRange.Columns.Count;
int _lastUsedRow = worksheet.UsedRange.Rows.Count;
Expand Down Expand Up @@ -154,23 +108,30 @@ static string ColumnIndexToColumnLetter(int colIndex)
}
return colLetter;
}
private System.Data.DataTable ProcessObjects(bool useHeaderRow, bool ignoreEmptyRows, object[,] valueArray)
private System.Data.DataTable ProcessObjects(Microsoft.Office.Interop.Excel.Range range, bool useHeaderRow, bool ignoreEmptyRows, object[,] valueArray)
{
System.Data.DataTable dt = new System.Data.DataTable();
var beginat = 1;
if(useHeaderRow)
{
for (int k = 1; k <= valueArray.GetLength(1); k++)
{
dt.Columns.Add((string)valueArray[1, k]); //add columns to the data table.
var v = (worksheet.Cells[range.Row + 1, range.Column + (k - 1)] as Range).get_Value(Type.Missing);
if (v == null) v = (worksheet.Cells[range.Row, range.Column + (k - 1)] as Range).get_Value(Type.Missing);
Type type = typeof(string);
if (v != null) type = v.GetType();
dt.Columns.Add((string)valueArray[1, k], type); //add columns to the data table.
}
beginat = 2;
}
else
{
for (int k = 1; k <= valueArray.GetLength(1); k++)
{
dt.Columns.Add(k.ToString()); //add columns to the data table.
var v = (worksheet.Cells[range.Row + 1, range.Column + (k - 1)] as Range).get_Value(Type.Missing);
Type type = typeof(string);
if (v != null) type = v.GetType();
dt.Columns.Add(k.ToString(), type); //add columns to the data table.
}
beginat = 1;
}
Expand Down

0 comments on commit bbd7105

Please sign in to comment.