C# Excel : Correct way to get Rows and Columns count

后端 未结 2 1887
一个人的身影
一个人的身影 2020-12-17 01:31

I have problem with C# Interop Excel get valid range with big file

https://www.dropbox.com/s/betci638b1faw8g/Demo%20Training%20Data.xlsx?dl=0

This is my fi

2条回答
  •  我在风中等你
    2020-12-17 01:53

    I would use this approach to get the Rows and Columns count which will return the result of the cells which are not empty.

    // Find the last real row
    lastUsedRow = worksheet.Cells.Find("*",System.Reflection.Missing.Value, 
                                   System.Reflection.Missing.Value, System.Reflection.Missing.Value, 
                                   Excel.XlSearchOrder.xlByRows,Excel.XlSearchDirection.xlPrevious, 
                                   false,System.Reflection.Missing.Value,System.Reflection.Missing.Value).Row;
    
    // Find the last real column
    lastUsedColumn = worksheet.Cells.Find("*", System.Reflection.Missing.Value, 
                                   System.Reflection.Missing.Value,System.Reflection.Missing.Value, 
                                   Excel.XlSearchOrder.xlByColumns,Excel.XlSearchDirection.xlPrevious, 
                                   false,System.Reflection.Missing.Value,System.Reflection.Missing.Value).Column;
    

    Here is the complete code for your reference:

    using Excel = Microsoft.Office.Interop.Excel;
    
    Excel.Application xlApp     = null;
    Excel.Workbook wb           = null;
    Excel.Worksheet worksheet   = null;
    int lastUsedRow             = 0;
    int lastUsedColumn          = 0;
    string srcFile              = @"Path to your XLSX file";
    
    xlApp = new Excel.ApplicationClass();
    xlApp.Visible = false;
    wb = xlApp.Workbooks.Open(srcFile,
                                   0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
                                   true, false, 0, true, false, false);
    
    worksheet = (Excel.Worksheet)wb.Worksheets[1];
    Excel.Range range
    
    // Find the last real row
    lastUsedRow = worksheet.Cells.Find("*",System.Reflection.Missing.Value, 
                                   System.Reflection.Missing.Value, System.Reflection.Missing.Value, 
                                   Excel.XlSearchOrder.xlByRows,Excel.XlSearchDirection.xlPrevious, 
                                   false,System.Reflection.Missing.Value,System.Reflection.Missing.Value).Row;
    
    // Find the last real column
    lastUsedColumn = worksheet.Cells.Find("*", System.Reflection.Missing.Value, 
                                   System.Reflection.Missing.Value,System.Reflection.Missing.Value, 
                                   Excel.XlSearchOrder.xlByColumns,Excel.XlSearchDirection.xlPrevious, 
                                   false,System.Reflection.Missing.Value,System.Reflection.Missing.Value).Column;
    
    xlApp.Workbooks.Close();
    xlApp.Quit();
    
    Marshal.ReleaseComObject(worksheet);
    Marshal.ReleaseComObject(wb);
    Marshal.ReleaseComObject(xlApp);
    

提交回复
热议问题