【发布时间】:2012-05-12 03:49:46
【问题描述】:
我发现使用 OpenXml 我的列并不总是对齐。例如看这个例子
这是标题行 第 37 列 = “仅限交换” 第 38 列 = “现有数量” 第 39 栏 = “成本法” 第 40 列 = “成本法金额”
现在,在下一行 - 在 37 和 38 中都有实际数据的行是空白的,但它省略了第 38 列。所以我的数据看起来像这样。
第 37 列 = ""(应为空白) 第 38 列 = “设置金额”(应为空白,因为它应与“现有数量”对齐) 第 39 列 = 0
请注意,它删除了真正的列/单元格 38,现在我的解析器不再排队。 37 和 38 都是空白,但不会丢失 37。
这里有一些代码展示了我是如何得到字符串数组的——它实际上只是一个来自 MSDN 的修改示例。
public InventoryItemLoadProxy CreateInventoryItemFromSpreadsheetRow(Row row, SharedStringTable sharedStrings)
{
var invItem = new InventoryItemLoadProxy();
var theCells = row.Descendants<Cell>();
var textValues =
from cell in row.Descendants<Cell>()
select(cell.CellValue == null ? string.Empty : ((cell.DataType != null
&& cell.DataType.HasValue
&& cell.DataType == CellValues.SharedString) ? sharedStrings.ChildElements[int.Parse(cell.CellValue.InnerText)].InnerText : cell.CellValue.InnerText));
if(textValues.Any(x => x != string.Empty))
{
var textArray = textValues.ToArray();
invItem.PartNumber = textArray[0].ToStrippedPartNumber();
invItem.DisplayPartNumber = textArray[0];
//More parsing...
}
}
你可以看到我是说如果它是 null 就让它 string.empty (是的 - 那个疯狂的 linq 语句会在某个时候被重构)。
【问题讨论】: