简体   繁体   English

Open XML SDK:尝试填充超过25列时出现“Unreadable content”错误

[英]Open XML SDK: get “Unreadable content” error when trying to populate more than 25 columns

I have created a spreadsheet using Open XML SDK in C#, and successfully populated two worksheets. 我在C#中使用Open XML SDK创建了一个电子表格,并成功填充了两个工作表。

When trying to populate a third, I get an "Unreadable content" error when opening the completed document, and it appears to occur when I attempt to populate more than 25 cells in a row on the third. 当尝试填充第三个时,我在打开完成的文档时出现“不可读内容”错误,并且当我尝试在第三个行中连续填充超过25个单元格时,它似乎发生。

I'm using the same code fragment as has worked successfully elsewhere in the document: 我使用的代码片段与在文档中其他地方成功运行的代码片段相同:

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);
}

The only cells populated are A and AA through AH. 唯一填充的细胞是A和AA到AH。

Am I right in thinking I'm hitting some sort of limit, and if so, what's the way to reset it? 我是否正确地认为我正在达到某种限制,如果是这样,重置它的方法是什么?

I've seen several posts about the unreadable content error, and I've looked through the documentation, but I've so far been unable to find anything that applies. 我看过几篇关于不可读内容错误的帖子,我查看了文档,但到目前为止我还没找到任何适用的内容。

Help would be appreciated! 帮助将不胜感激!

Thanks 谢谢

Accepted answer is definetely correct but is a workaround. 接受的答案是定义正确的,但是是一种解决方法。 It will only work if you are inserting the cells in order. 它只有在按顺序插入单元格时才有效。

If you are using the InsertCellInWorksheet method from MSDN , there is a bug on the cell reference comparing when comparing a cell references of different length such as 'Z' and 'AA'. 如果您正在使用MSDN中的InsertCellInWorksheet方法,则在比较不同长度的单元格引用(如“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;
    }
}

You might want to change the string.Compare to: 您可能想要将string更改为:比较:

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

using the ColumnNameParse method taken from this answer : 使用从这个答案中获取的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));
}

Please check out the "InsertCellInWorksheet(...)" method. 请查看“InsertCellInWorksheet(...)”方法。 If you use this construction inside - 如果你在里面使用这种结构 -

...

row.InsertBefore(newCell, refCell);

...

it won't work correctly if you want to fill in "A - Z" AND "AA - ..." columns, even if you want to fill in only two columns (for example), - "Z" and "AA". 如果要填写“A - Z”和“AA - ...”列,即使您只想填写两列(例如), - “Z”和“AA”,它也无法正常工作。 So, try to use this method instead: 因此,请尝试使用此方法:

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

Good luck! 祝好运!

Change 更改

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

to

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM