简体   繁体   English

Excel 使用 C# Microsoft Interop Excel 在第 144 行后返回 Null

[英]Excel Returns Null After Line 144 using C# Microsoft Interop Excel

In C#, I've opened an Excel spreadsheet and I am iterating through it.在 C# 中,我打开了一个 Excel 电子表格,我正在遍历它。 Whenever I get to line 144, the spreadsheet value returns null for every single line following it.每当我到达第 144 行时,电子表格值对于它后面的每一行都返回 null。 Looking at the spreadsheet, it is clearly not null for lines 144 - 250. I tried saving it in different versions but that didn't work.查看电子表格,第 144 - 250 行显然不为空。我尝试将其保存在不同的版本中,但这没有用。 I've tried copying and pasting into a brand new worksheet.我试过复制并粘贴到一个全新的工作表中。 Nothing has worked.没有任何效果。 Here is my code:这是我的代码:

        // initiate Excel
        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;

        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Open(lblFileName.Text, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        //Get the used Range
        Excel.Range usedRange = xlWorkSheet.UsedRange;

        //Iterate the rows in the used range
        string customerNumber;
        string amount;
        int i = 0;
        decimal dAmount = 0;

        bool runProcess = true;
        bool hasHeader = false;
        string cellValue;
        foreach (Excel.Range row in usedRange.Rows)
        {
            i++; 
            // Column 1 is customer number
            // THE FOLLOWING LINE RETURNS NULL STARTING AT ROW 144
            cellValue = row.Cells[i, 1].Value2 == null ? "" : row.Cells[i, 1].Value2.ToString();
            if (!(cellValue.Length >= 5 && cellValue.Length <= 20))
            {
                    MessageBox.Show(cellValue + " Must be between 5 and 20 Characters - File Will Not Be Processed Row#" + i.ToString());
                    runProcess = false;
            }

        }
        // run process
        // release EXCEL

I did try row.Cells[i, 1].Value2, row.Cells[i, 1].Value and row.Cells[i, 1].Text and got the same results for all.我确实尝试了 row.Cells[i, 1].Value2, row.Cells[i, 1].Value 和 row.Cells[i, 1].Text 并得到了相同的结果。 I am including "using Excel = Microsoft.Office.Interop.Excel;"我包括“使用 Excel = Microsoft.Office.Interop.Excel;”

Sample Data from Excel来自 Excel 的示例数据

 4492605768531895  15.95
 4492605768536544  19.95
 4492605768565459  11.95
 4492605739542347  14.95
 4492605768635795  25.95

I think it has something to do with the Range object size.我认为这与 Range 对象大小有关。 I tried replace the foreach to this and it worked (for me the null was occurring in line 102)我尝试将 foreach 替换为这个并且它有效(对我来说空值发生在第 102 行)

int maxRows = usedRange.Rows.Count;
for (int i=1;i<=maxRows;i++)
{
    // Column 1 is customer number
   // THE FOLLOWING LINE RETURNS NULL STARTING AT ROW 144
   cellValue = xlWorkSheet.Cells[i, 1].Value2 == null ? "" : xlWorkSheet.Cells[i, 1].Value2.ToString();

    if (!(cellValue.Length >= 5 && cellValue.Length <= 20))
    {
        MessageBox.Show(cellValue + " Must be between 5 and 20 Characters - File Will Not Be Processed Row#" + i.ToString());
        runProcess = false;
    }

}

暂无
暂无

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

相关问题 C#运行后关闭Microsoft Interop Excel进程 - C# Closing Microsoft Interop Excel Process after running 使用Interop c#在Microsoft Excel中检测数据透视表 - Detect pivot table in Microsoft Excel using Interop c# C#,微软互操作,Excel数字格式问题 - C#, Microsoft interop, Excel numberformat problem 在C#中使用Microsoft.Office.Interop.Excel将数据Excel移至40行数据后的右列 - Move data Excel to Right Column after 40 Rows Data using Microsoft.Office.Interop.Excel in C# C#使用Microsoft.Office.Interop.Excel读取Excel单元格值 - C# reading Excel cell values using Microsoft.Office.Interop.Excel C#使用microsoft.office.interop.excel在widndows表单上将电子邮件导出到excel - C# exporting e-mails to excel on widndows forms using microsoft.office.interop.excel 使用 C# 中的 Microsoft.Office.Interop.Excel 从 excel 读取大量数据 - reading huge data from excel using Microsoft.Office.Interop.Excel in C# 使用 Microsoft Office Interop Excel 使用 C# 在 excel 图表中自定义数据标签 - custom datalabels in excel charts with C# using Microsoft Office Interop Excel 如何在 C# 中使用 Microsoft.Office.Interop.Excel 在 Excel 中的矩形内写入文本 - How to write a text inside a rectangle in excel using Microsoft.Office.Interop.Excel in c# 如何在不使用 Microsoft.Office.Interop.Excel 库的情况下在 C# 中读取 excel 文件 - How to read an excel file in C# without using Microsoft.Office.Interop.Excel libraries
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM