Excel Returns 使用 C# Microsoft Interop 在第 144 行之后为空 Excel
Excel Returns Null After Line 144 using C# Microsoft Interop Excel
在 C# 中,我打开了一个 Excel 电子表格,并且正在遍历它。每当我到达第 144 行时,它后面的每一行的电子表格值 returns null。查看电子表格,第 144 - 250 行显然不为空。我尝试将其保存为不同的版本,但没有用。我试过复制并粘贴到一个全新的工作表中。没有任何效果。这是我的代码:
// 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
我确实尝试了 row.Cells[i, 1].Value2, row.Cells[i, 1].Value 和 row.Cells[i, 1].Text 并得到了相同的结果所有人的结果。我包括 "using Excel = Microsoft.Office.Interop.Excel;"
示例数据来自 Excel
4492605768531895 15.95
4492605768536544 19.95
4492605768565459 11.95
4492605739542347 14.95
4492605768635795 25.95
我认为这与 Range 对象大小有关。我尝试将 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;
}
}
在 C# 中,我打开了一个 Excel 电子表格,并且正在遍历它。每当我到达第 144 行时,它后面的每一行的电子表格值 returns null。查看电子表格,第 144 - 250 行显然不为空。我尝试将其保存为不同的版本,但没有用。我试过复制并粘贴到一个全新的工作表中。没有任何效果。这是我的代码:
// 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
我确实尝试了 row.Cells[i, 1].Value2, row.Cells[i, 1].Value 和 row.Cells[i, 1].Text 并得到了相同的结果所有人的结果。我包括 "using Excel = Microsoft.Office.Interop.Excel;"
示例数据来自 Excel
4492605768531895 15.95
4492605768536544 19.95
4492605768565459 11.95
4492605739542347 14.95
4492605768635795 25.95
我认为这与 Range 对象大小有关。我尝试将 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;
}
}