如何使用ASP.NET查找Excel表的行数?

时间:2017-09-14 09:39:06

标签: c# asp.net excel

我使用下面提到的代码来查找Excel表的行数,但似乎有些问题,因为我没有得到所需的输出。请提供一些解决方案。

代码如下:

using Excel = Microsoft.Office.Interop.Excel;

Excel.Application xlApp     = null;
Excel.Workbook wb           = null;
Excel.Worksheet worksheet   = null;
int lastUsedRow             = 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;


xlApp.Workbooks.Close();
xlApp.Quit();

Marshal.ReleaseComObject(worksheet);
Marshal.ReleaseComObject(wb);
Marshal.ReleaseComObject(xlApp);

1 个答案:

答案 0 :(得分:0)

您可以尝试:

wb = xlApp.Workbooks.Open(srcFile,
                           Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                           Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

worksheet = (Excel.Worksheet)wb.Worksheets[1];
object[,] values = worksheet .UsedRange.Value2;
int rowCount = values.GetLength(0);
int colCount = values.GetLength(1);