Open XML SDK:尝试填充超过25列时出现“无法读取的内容”错误

7

我已经使用C#中的Open XML SDK创建了一个电子表格,并成功地填充了两个工作表。

当尝试填充第三个工作表时,打开完成的文档时会出现一个“无法读取内容”错误,并且似乎是在第三个工作表上尝试填充超过25个单元格时发生的。

我正在使用与文档其他部分成功工作的相同的代码片段:

string[] headers2 = {
    "Reference", "Raised", "Priority", "Affected", "Linked incidents",
    "Points", "SLA", "Stopped", "Target" };
// remaining headers are month/years created on the fly
string[] headerCells = {
    "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
    "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", 
    "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH" };
...
// headers
// once we've finished the presets, the month/year pairs take over
cellNo = (uint)0;
foreach (string h in headers2)
{
    cell = InsertCellInWorksheet(headerCells[cellNo++], 2, worksheetPart);
    cell.CellValue = new CellValue(h);
    cell.DataType = new EnumValue<CellValues>(CellValues.String);
}

string[] monthYears = 
    GetData_month_years(currentReportingPeriod - 1);
for (int j = 0; j < monthYears.Count(); j++)
{
    cell = InsertCellInWorksheet(headerCells[cellNo++], 2, worksheetPart);
    cell.CellValue = new CellValue(monthYears[j].ToString());
    cell.DataType = new EnumValue<CellValues>(CellValues.String);
}

只有A和AA到AH这些单元格中有数据。

我想我是遇到了某种限制,如果是的话,该如何重置?

我看到过一些关于无法读取内容错误的帖子,并且已经阅读了文档,但目前还没有找到任何适用的东西。

希望能得到帮助!

谢谢

3个回答

11

采纳的答案确实是正确的,但只是一个解决方法。这仅在按顺序插入单元格时才起作用。

如果您正在使用来自MSDN的InsertCellInWorksheet方法,则会存在一个Bug,即当比较长度不同的单元格引用(例如'Z'和'AA')时,会出现问题。

// Cells must be in sequential order according to CellReference. 
// Determine where to insert the new cell.
Cell refCell = null;
foreach (Cell cell in row.Elements<Cell>())
{
    if (string.Compare(cell.CellReference.Value, cellReference, true) > 0)
    {
        refCell = cell;
        break;
    }
}

您可能希望将 string.Compare 更改为:

if (ColumnNameParse(cell.CellReference.Value) > ColumnNameParse(cellReference))
{
    refCell = cell;
    break;
}

使用从这个答案中采用的ColumnNameParse方法:

public int ColumnNameParse(string cellReference)
{
    var value = cellReference.TrimEnd("1234567890".ToCharArray());

    // assumes value.Length is [1,3]
    // assumes value is uppercase
    var digits = value.PadLeft(3).Select(x => "ABCDEFGHIJKLMNOPQRSTUVWXYZ".IndexOf(x));
    return digits.Aggregate(0, (current, index) => (current * 26) + (index + 1));
}

1
这花了我两个晚上。该死的,我讨厌 OpenXML。谢谢你的回答! - Sam Vanhoutte
我花了很多时间才意识到这是单元格定位错误。我不明白为什么OpenXML不支持R1C1单元格地址名称,这会让每个人的工作更容易。 - Kevin

7
请查看“InsertCellInWorksheet(...)”方法。如果您在其中使用此结构 -
...

row.InsertBefore(newCell, refCell);

...

如果你想要填写"A-Z"和"AA-..."列,即使你只想填写两列(例如),"Z"和"AA",这种方法也不会正常工作。因此,尝试使用以下方法代替:

...
row.Append(newCell);
...

祝你好运!


-1

更改

if (string.Compare(cell.CellReference.Value, cellReference, true) > 0) 

if (string.Compare(cell.CellReference.Value, cellReference, true) > 0
                        && cell.CellReference.Value.Length >= cellReference.Length)

由于B7不遵循AA7的规则,因此返回已翻译的文本。 - abx222
必须承认,将其转换为整数是迄今为止最简单的解决方案。 - Kevin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接