使用C#在Excel工作簿中读取,缺少值或值插入为NULL

时间:2019-03-21 14:00:56

标签: c# excel interop

使用C#和Microsoft的Interop库,我正在阅读一个类似Excel workbook的excel工作簿

使用以下代码:

//start at row 10
for (int rows = 10; rows <= rowCount; rows++)
{
    Console.WriteLine("Row Increase " + rows);
    //start at column 8(H)
    for (int cols = 1; cols <= colCount; cols++)
    {
        Console.WriteLine("Column Increase " + cols);
        if (xlRange.Cells[rows, cols].Value2 != null)
        {
            Console.Write(xlRange.Cells[rows, cols].Value2.ToString() + "\t");
            string compare = xlRange.Cells[rows, cols].Value2.ToString();
        }
    }
}

我的控制台输出如下

Row Increase 10
Column Increase 1
Column Increase 2
Column Increase 3
Column Increase 4
Column Increase 5
Column Increase 6
Column Increase 7
0       Column Increase 8
ASIN    Column Increase 9
B008O6YQSE      Column Increase 10
Column Increase 11
0       Column Increase 12
Column Increase 13
0       Column Increase 14
1001    Column Increase 15
Column Increase 16
Column Increase 17
Column Increase 18
Column Increase 19
Column Increase 20
Column Increase 21
Column Increase 22
Column Increase 23
Column Increase 24
Column Increase 25
Column Increase 26
Column Increase 27
Row Increase 11
Column Increase 1
Column Increase 2
Column Increase 3
Column Increase 4
Column Increase 5
Column Increase 6
Column Increase 7
3       Column Increase 8
ASIN    Column Increase 9
B008O6YQSE      Column Increase 10
Column Increase 11
3       Column Increase 12
Column Increase 13
0       Column Increase 14
1002    Column Increase 15
Column Increase 16
Column Increase 17
Column Increase 18
Column Increase 19
Column Increase 20
Column Increase 21
Column Increase 22
Column Increase 23
Column Increase 24
Column Increase 25
Column Increase 26
Column Increase 27
Row Increase 12
Column Increase 1
Column Increase 2
Column Increase 3
Column Increase 4
Column Increase 5
Column Increase 6
Column Increase 7
2       Column Increase 8
Product Title   Column Increase 9
Bissell 8/14 Pleated Filter by Rug Doctor, One Replacement Vacuum Filter Made with                 HEPA Media, Made to Trap Pollutants when Deep Cleaning Carpets       Column Increase 10
Column Increase 11
2       Column Increase 12
Column Increase 13
0       Column Increase 14
1003    Column Increase 15
Column Increase 16
Column Increase 17
Column Increase 18
Column Increase 19
Column Increase 20
Column Increase 21
Column Increase 22
Column Increase 23
Column Increase 24
Column Increase 25
Column Increase 26
Column Increase 27

我的代码未读过列L,因为它认为这些值是NULL且未输入if语句,或者由于某种原因未认为它们存在。该程序似乎还认为15列实际上是存储在1001列中的8(问题ID字段)的存储位置。

我尝试了从.xlsm.xlsx的转换,删除了if语句(由于null值而返回错误),并在excel表中添加了值。 OUGHT字段曾经是空白;我添加了OUGHT一词,但程序未读取它。

0 个答案:

没有答案