简体   繁体   English

阅读Excel工作表时如何不跳过空行?

[英]How to NOT skip empty rows when reading Excel sheet?

I am using Microsoft.Office.Interop.Excel to read an Excel file. 我正在使用Microsoft.Office.Interop.Excel读取Excel文件。

The problem I am having is that when reading, it counts the first row that has information as row 1. So, if the first row to have information is row 3, it counts that as the first row. 我遇到的问题是,在读取时,它将具有信息的第一行计为第1行。因此,如果要获取信息的第一行为第3行,则将其计为第一行。 This is a problem because the Excel sheets sometimes have data on the first 2 rows, sometimes they don't. 这是一个问题,因为Excel工作表有时前两行都有数据,有时却没有。 But the data I need to read always starts on the third row. 但是我需要读取的数据总是从第三行开始。

Here is the routine I am using to read the sheets: 这是我用来阅读工作表的例程:

Microsoft.Office.Interop.Excel.Application exl = new Microsoft.Office.Interop.Excel.Application();
        Workbooks wkbs = exl.Workbooks;
        Microsoft.Office.Interop.Excel.Workbook wkb = null;
        Sheets shts;
        Microsoft.Office.Interop.Excel.Worksheet wks = null;

        // load work book
        //wkb = wkbs.Open(races_data_path, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);//oMissing
        wkb = wkbs.Open(races_data_path, Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);//oMissing

        shts = wkb.Sheets;
        wks = (Microsoft.Office.Interop.Excel.Worksheet)shts.get_Item(Properties.Settings.Default.excel_worksheet);

        // read lines from worksheet
        Microsoft.Office.Interop.Excel.Range range = wks.UsedRange;

        for (int rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
        {
            try
            {
                object[,] values = (object[,])range.Value2;

                if (Convert.ToString(values[rCnt, 4]) != "")
                {
                    //Console.WriteLine(String.Format("Ldd: {0} {1} {2}", Convert.ToInt16(values[rCnt, 2]), Convert.ToInt16(values[rCnt, 3]), Convert.ToString(values[rCnt, 4])));
                    if (races[Convert.ToInt16(values[rCnt, 1])] == null) races[Convert.ToInt16(values[rCnt, 1])] = new RaceObject();

                    races[Convert.ToInt16(values[rCnt, 1])].add_racer(Convert.ToInt16(values[rCnt, 2]), Convert.ToInt16(values[rCnt, 3]), Convert.ToString(values[rCnt, 4]), Convert.ToString(values[rCnt, 7]));
                }

            }
            catch (Exception ex)
            {
                data_loaded = false;
                rCnt = range.Rows.Count;
            }

        }

This example, you can see the loop count starts at '1'. 在此示例中,您可以看到循环计数从“ 1”开始。 This was for reading a sheet where the first 2 lines of the sheet were empty. 这是为了读取工作表的前两行为空的工作表。 If the first 2 lines have data, then this loop only works if I change the count start to '3'. 如果前两行有数据,则仅当我将计数起始位置更改为“ 3”时,此循环才有效。

For example: The start value = 1 if the sheet looks like this: 例如:如果工作表看起来像这样,则起始值= 1:

no data
no data
has data
has data
has data

The start value = 3 if the sheet looks like this: 如果工作表如下所示,则起始值= 3:

has unnecessary data
has unnecessary data
has data
has data
has data

Option 1: Add something to the first cell immediately before doing the rest: wks.cells(1, 1).value = "'" & wks.cells(1, 1).value This will extend the usedRange to the first cell. 选项1:在进行其余操作之前,立即在第一个单元格中添加一些内容:wks.cells(1,1).value =“'”&wks.cells(1,1).value这将把usedRange扩展到第一个单元格。

Then the data will always start on the third row and you can easily always skip the first two rows. 然后,数据将始终从第三行开始,您可以轻松地始终跳过前两行。

Option 2: You can also use wks.UsedRange.Cells(1, 1).Row to see whether the first row is 1 or 3. Then you can adjust whether you use those rows or not. 选项2:您还可以使用wks.UsedRange.Cells(1, 1).Row查看第一行是1还是3。然后可以调整是否使用这些行。

(Edited post based on comments) (根据评论编辑帖子)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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