There is a MS Excel sheet filled with one matrix data.
How can I get only the numbers region?
Right now I'm extracting the data range with the workSheet.UsedRange
. But if I delete one matrix line, it will be considered in the usedRange, even though the line has been deleted! That's just because I have previously used those cells.
So, how can I get only the range with actual things (numbers) on it?
The code:
public Array ReadData(string fullPath, Size size)
{
Excel.Application application;
Workbook workBook;
Worksheet workSheet;
Range range;
application = new Excel.ApplicationClass();
workBook = application.Workbooks.Open(fullPath, 0, true, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
workSheet = (Worksheet)workBook.Worksheets.get_Item(1);
//HERE IS THE PROBLEM
range = workSheet.UsedRange;
if (!size.IsEmpty)
{
Size availableSize = new Size(range.Columns.Count, range.Rows.Count);
if ((availableSize.Width < size.Width) || (availableSize.Height < size.Height))
{
string msg = string.Format("Available data range ({0}) at the sheet is smaller than needed size ({1})", availableSize, size);
throw new ArgumentException(msg);
}
else if ((availableSize.Width > size.Width) || (availableSize.Height > size.Height))
{
range = workSheet.get_Range(workSheet.Cells[1, 1], workSheet.Cells[size.Width, size.Height]);
string msg = string.Format("Available data range ({0}) at the sheet is bigger than needed size ({1})", availableSize, size);
Trace.TraceInformation(msg);
}
}
Array data;
data = (Array)range.Cells.Value2;
return data;
}
The possible solution was post in VB I think. In C# will be:
range = workSheet.UsedRange;
range = range.CurrentRegion.SpecialCells(XlCellType.xlCellTypeConstants, XlSpecialCellsValue.xlNumbers);
but it raises an excpetion "no cells found"