I know there are a few approach to find the total number of filled rows and columns based on individual requirements. One of the approach I'm trying to use Cells.Find approach to find the number of filled rows and columns. I found one relevant post here: C# Excel : Correct way to get Rows and Columns count, this is exactly what I need. I copied the format and replaced the variables and worked fine. However the return value of lastUsedColumn is 1 instead of 4, whereas the lastUsedRow return 5 which is exactly correct. The file format for my Excel file is in .xls not .csv. I had been studying C# for a while now by reading online documents, but still couldn't figure why xlByColumns isn't working here, or I'm missing something..? Can anyone help to explain..? Thanks.
Example of data in my Excel(.xls)
0.02 | 1.352 | 2.447 | -3.9924
0.04 | 2.991 | 9.556 | 3.227
0.06 | -9.119 | 1.883 | 2.004
0.08 | 5.382 | -9.003 | 7.441
1.00 | -8.803 | - 6.443 | 7.210
*The symbol | represent the column, so there are a total of 4 columns and a total of 5 rows.
Excel.Application app = null;
Excel.Workbook wb = null;
Excel.Worksheet ws = null;
double lastUsedRow = 0;
double lastUsedColumn = 0;
string FFile = @"C:\Users\Student\Downloads\FFile.xls";
StreamWriter rowvalue = File.CreateText(@"C:\Users\Student\Downloads\rowvalue.xls");
StreamWriter colvalue = File.CreateText(@"C:\Users\Student\Downloads\colvalue.xls");
app = new Excel.Application();
app.Visible = false;
wb = app.Workbooks.Open(FFile, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
ws = (Excel.Worksheet)wb.Worksheets[1];
object missV = System.Reflection.Missing.Value;
// Find the last real row
// Return result 5 (correct)
lastUsedRow = ws.Cells.Find("*", missV, missV, missV, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlPrevious, false, true, missV).Row;
rowvalue.WriteLine(lastUsedRow);
// Find the last real column
// Return result 1 (wrong)
lastUsedColumn = ws.Cells.Find("*", missV, missV, missV, Excel.XlSearchOrder.xlByColumns, Excel.XlSearchDirection.xlPrevious, false, true, missV).Column;
colvalue.WriteLine(lastUsedColumn);