使用 Interop 在 Excel.Range 中忽略并覆盖空白单元格
Blank cell is ignored and overwritten in Excel.Range using Interop
在 excel 文件中有数据,如下所示,其中第 1 行和第 2 行被视为 header 行,当根据第一列拆分此特定文件时,将复制这些行。
拆分后的文件如下所示:
问题 是列 D 的值被复制到错误的列,在这种情况下是 C。
我想我需要检查单元格是否为空或为空,并在写入期间将 blank/null 值放入新文件。问题是如何将其合并到我的以下代码中?
private FileEntity GetFileObject(Excel.Range range)
{
FileEntity fileEntity = new FileEntity();
fileEntity.RowValues = new List<RowEntity>();
for (int rowCount = 1; rowCount <= range.Rows.Count; rowCount++)
{
RowEntity rowEntity = new RowEntity();
rowEntity.ColumnValues = new List<string>();
for (int columnCount = 1; columnCount <= range.Columns.Count; columnCount++)
{
if ((range.Cells[rowCount, columnCount] as Excel.Range).Value != null)
{
rowEntity.ColumnValues.Add((range.Cells[rowCount, columnCount] as Excel.Range).Value.ToString());
}
}
fileEntity.RowValues.Add(rowEntity);
}
return fileEntity;
}
这是因为文档结构具有避免浪费的内在效率 space。您必须检查单元格中的引用(A1、B1 等),然后您才能知道它来自哪里以及如何处理它。
int rowIdx = 0;
foreach (Row r in sheetData.Elements<Row>().Skip(1))
{
rowIdx++;
IEnumerable<Cell> thisRow = r.Elements<Cell>();
foreach (var c in thisRow)
{
//This will tell you what cell you are looking at (A2, D14, whatever..)
string cellRef = c.CellReference.ToString().Substring(0, 1);
switch (cellRef)
{
case "A":
//do something...
break;
case "B":
//do something...
break;
case "C":
//do something...
break;
....ETC.......
找到解决方案:
if ((range.Cells[rowCount, columnCount] as Excel.Range).Value != null)
{
rowEntity.ColumnValues.Add((range.Cells[rowCount, columnCount] as Excel.Range).Value.ToString());
}
else
rowEntity.ColumnValues.Add(""); //just add this line. keeps the blank cell as created with empty string
在 excel 文件中有数据,如下所示,其中第 1 行和第 2 行被视为 header 行,当根据第一列拆分此特定文件时,将复制这些行。
拆分后的文件如下所示:
问题 是列 D 的值被复制到错误的列,在这种情况下是 C。
我想我需要检查单元格是否为空或为空,并在写入期间将 blank/null 值放入新文件。问题是如何将其合并到我的以下代码中?
private FileEntity GetFileObject(Excel.Range range)
{
FileEntity fileEntity = new FileEntity();
fileEntity.RowValues = new List<RowEntity>();
for (int rowCount = 1; rowCount <= range.Rows.Count; rowCount++)
{
RowEntity rowEntity = new RowEntity();
rowEntity.ColumnValues = new List<string>();
for (int columnCount = 1; columnCount <= range.Columns.Count; columnCount++)
{
if ((range.Cells[rowCount, columnCount] as Excel.Range).Value != null)
{
rowEntity.ColumnValues.Add((range.Cells[rowCount, columnCount] as Excel.Range).Value.ToString());
}
}
fileEntity.RowValues.Add(rowEntity);
}
return fileEntity;
}
这是因为文档结构具有避免浪费的内在效率 space。您必须检查单元格中的引用(A1、B1 等),然后您才能知道它来自哪里以及如何处理它。
int rowIdx = 0;
foreach (Row r in sheetData.Elements<Row>().Skip(1))
{
rowIdx++;
IEnumerable<Cell> thisRow = r.Elements<Cell>();
foreach (var c in thisRow)
{
//This will tell you what cell you are looking at (A2, D14, whatever..)
string cellRef = c.CellReference.ToString().Substring(0, 1);
switch (cellRef)
{
case "A":
//do something...
break;
case "B":
//do something...
break;
case "C":
//do something...
break;
....ETC.......
找到解决方案:
if ((range.Cells[rowCount, columnCount] as Excel.Range).Value != null)
{
rowEntity.ColumnValues.Add((range.Cells[rowCount, columnCount] as Excel.Range).Value.ToString());
}
else
rowEntity.ColumnValues.Add(""); //just add this line. keeps the blank cell as created with empty string