【问题标题】:OpenXML Spreadsheet get column ordinal indexOpenXML 电子表格获取列序号索引
【发布时间】:2019-07-08 18:19:06
【问题描述】:

我正在使用 OpenXML 库创建 Excel 导出。我可以使用默认单元格引用设置单元格引用,例如A1.

由于我想跨列动态填充数据,我想知道是否可以使用数字坐标参考,例如单元格[1, 1]?

所以不要做类似的事情:

Cell cell = InsertCellInWorksheet("A", 1, worksheet);

我想做:

Cell cell = InsertCellInWorksheet(columnIndex, rowIndex, worksheet);

我似乎找到了很多使用 Excel 网格参考 (“A1”) 的示例,但对于数字表示却一无所获。

非常感谢

安迪

更新

我找到了一种通过here 的答案获取列标题的方法,它根据数字索引计算列名。

这使列很好,我可以单步执行代码,并且可以看到它在 Z、AA、AB 等列中循环。我现在发现,在遍历列时,如果它构建任何超过 Z 列的内容,我会收到错误留言Excel found unreadable content in...Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes.

这是设置单元格的代码:

int columnIndex = 1;
string col;
uint rowIndex = 60;
foreach (var item in _data)
{
    col = ExcelHelper.GetColumnName(columnIndex);
    ExcelHelper.SetCell(wsp, col, rowIndex, item.PeriodEnd.ToString("dd/MM/yyyy"), excelStylesheet.TableCell);
    columnIndex++;
}

如果我将 col 硬编码为“AA”并将 rowIndex 设置为递增,它可以正常工作,但这样会生成一个不可读的 Excel 文件。

我看不出这是如何/为什么会发生的,因为我可以看到 col 在代码中被正确设置,但某处似乎出了问题。

任何人都可以帮助阐明可能导致此问题的原因或知道更好地识别问题的方法吗?

再次感谢

安迪

【问题讨论】:

  • 这在 excel 部分似乎非常棘手。因为列标题表示是字母数字。我认为操作字母数字列不会很困难。
  • 据我所知,Open XML SDK 不支持通过数字索引引用单元格。至少在我们的项目中我们使用默认值(即“A1”)。但是您可以定义自己的单元格名称(别名)。

标签: c# excel openxml


【解决方案1】:

你可以使用下面的代码

row.Append(newCell);

而不是这个

row.InsertBefore(newCell, refCell);

Open XML SDK: get "Unreadable content" error when trying to populate more than 25 columns

【讨论】:

猜你喜欢
  • 2012-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-19
  • 1970-01-01
相关资源
最近更新 更多