简体   繁体   中英

Use interop and c# to count the rows in an Excel spreadsheet's worksheet with data in

I have just written what has to be considered utterly hideous code to count the rows that contain data in the worksheets called "Data" from all the spreadsheets in a given directory. Here's the code

    private const string _ExcelLogDirectoryPath = @"..\..\..\..\Model\ExcelLogs\";
    static void Main()
    {
        var excelLogPaths = Directory.GetFiles(_ExcelLogDirectoryPath, "*.xl*");
        var excel = new Microsoft.Office.Interop.Excel.Application();
        var excelRowCounts = new Dictionary<string, int>();
        foreach (var filePath in excelLogPaths)
        {
            var spreadsheet = excel.Workbooks.Open(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "/" + filePath);
            var worksheet = spreadsheet.Sheets["Data"] as Worksheet;
            if (worksheet != null)
            {
                // var rowCount = UsedRange.Rows.Count - 1; DOES NOT WORK, THE number is bigger than the 'real' answer
                var rowCount = 0;
                for (var i = 1 ; i < 1000000000; i++)
                {
                    var cell = worksheet.Cells[i, 1].Value2; // "Value2", great name for a property, thanks guys
                    if (cell != null && cell.ToString() != "") // Very fragile (e.g. skipped rows will break this)
                    {
                        rowCount++;
                    }
                    else
                    {
                        break;
                    }
                }
                var name = spreadsheet.Name.Substring(spreadsheet.Name.IndexOf('p'), spreadsheet.Name.IndexOf('.') - spreadsheet.Name.IndexOf('p'));
                excelRowCounts.Add(name, rowCount - 1);
            }
        }

I cannot believe this is the right way to do this. It is crazy slow and includes calls to properties with names like Value2 that do not feel like an intended part of a public API. But the method suggested elsewhere dramatically over reports the number of rows (with data in them).

What is the correct was to count the rows with data in them from an Excel worksheet?

========== EDIT 1 ==========

The reason that both UsedRange.Rows.Count and Sid's ACE.OLEDB solution over report the number of rows appears to be a pink background colour that is applied to some of the columns (but only extending to row 7091). Is there a simple/elegant way to count the rows with data in them (ie with non-null cell values) regardless of the display colour?

========== EDIT 2 ===========

Sid's ACE.OLEDB solution with the addition he suggests so that the tSQL line reads

var sql = "SELECT COUNT (*) FROM [" + sheetName + "$] WHERE NOT F1 IS NULL";

works. I'll mark that as the answer.

This should do the trick. You can call it with each filename to retrieve the number of rows.

private string GetNumberOfRows(string filename, string sheetName)
{
    string connectionString;
    string count = "";

    if (filename.EndsWith(".xlsx"))
    {
        connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Mode=ReadWrite;Extended Properties=\"Excel 12.0;HDR=NO\"";
    }
    else if (filename.EndsWith(".xls"))
    {
        connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + ";Mode=ReadWrite;Extended Properties=\"Excel 8.0;HDR=NO;\"";
    }

    string SQL = "SELECT COUNT (*) FROM [" + sheetName + "$]";

    using (OleDbConnection conn = new OleDbConnection(connectionString))
    {
        conn.Open();

        using (OleDbCommand cmd = new OleDbCommand(SQL, conn))
        {
            using (OleDbDataReader reader = cmd.ExecuteReader())
            {
                reader.Read();
                count = reader[0].ToString();
            }
        }

        conn.Close();
    }

    return count;
}

There might be an even faster way of retrieving just the row count, but I know this works.

如果您使用互操作,为什么不使用 UsedRange?

_Worksheet.UsedRange.Rows.Count

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM