在c#中查找Excel工作表中最后一个填充的行



我有一个要求,即Excel工作表中有多少行填充了数据,即不为空的行。我需要使用c#来实现这一点。

目前我正在使用以下代码:

Excel.Application excelApp = new Excel.Application();
            string workbookPath = "C:\ScriptTest\bin\Debug\SDownloadScripts\ExcelResult.xlsx";
            Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath,
        0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
        true, false, 0, true, false, false);
            Excel.Sheets excelSheets = excelWorkbook.Worksheets;
            string currentSheet = "Sheet1";
            Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
            //long i = excelWorksheet.UsedRange.Rows.Count;
            int lastUsedRow = excelWorksheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell,
Type.Missing).Row;
            return lastUsedRow;
        }

我的单子只填了四行,但我得到了65536行。因此,我需要4个,即没有用一些数据填充的行。请提出建议。

试试这个。。

string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:UsersxxxxxxDesktop1-8-13-ct.xlsx';Extended Properties=Excel 12.0;Persist Security Info=False";
    //Create Connection to Excel work book
    OleDbConnection excelConnection =new OleDbConnection(excelConnectionString);
    //Create OleDbCommand to fetch data from Excel
     OleDbCommand cmd = new OleDbCommand("select count(*) from [Sheet1$]", excelConnection);
    con.Open();
    int rows = (int)cmd.ExecuteScalar();
  con.Close();

私有数据集CreateDataSource(字符串strFilePath,字符串strSheetName){数据集myDataSet;myDataSet=null;尝试{

        if (strSheetName.Length > 0)
        {
            StringBuilder strConnectionString = new StringBuilder();
            strConnectionString.AppendFormat("Provider={0};", "Microsoft.ACE.OLEDB.12.0");
            strConnectionString.AppendFormat("Data Source={0};", strFilePath);
            strConnectionString.Append("Extended Properties=");
            strConnectionString.Append(((char)34).ToString()); //start of trickypart
            strConnectionString.Append("Excel 12.0 Xml;");
            // always treat contents of cells as text, which gives us full control/responsibility for casting to numeric when ncessary
            strConnectionString.Append(((char)34).ToString()); // end of tricky part
            string str = strConnectionString.ToString();
            OleDbConnection conn = new OleDbConnection(str);
            OleDbDataAdapter myCommand = new OleDbDataAdapter(" SELECT * FROM [" + strSheetName + "$] where QuestionDescription <>''", str);
            myDataSet = new DataSet();
            myCommand.Fill(myDataSet);
        }
        else
        {
            trError.Visible = true;
            lblError.Text = "File is Invalid format";
        }
    }
    catch
    {
        trError.Visible = true;
        lblError.Text = "Invalid format!!";
    }
    return myDataSet;
}

对于使用上面的代码,您可以查询以获得非空白行。结果将被存储到数据集中。您可以从数据集中获取非空单元格计数。要完成此代码工作,您需要使用"Microsoft.ACE.OLEDB.12.0"提供程序。

我在stackoverflow上发现一些线程提出了类似的问题。也许这些已经解决了你的问题。这个答案似乎就是你想要的。

根据您的结果,XlCelltype的文档和注释外的行,我假设您的工作表中的usedRange大于实际使用的区域。如果其他解决方案不起作用,并且数据行之间没有空行,则可以尝试搜索具有空单元格的列中的第一行(此行-1应该是您所需的行数)。

最新更新