c# - 如何将表格格式添加到 Excel 工作表中
问题描述
将数据写入工作表后,我正在尝试添加表格格式。我遇到了这个博客,它有 POI 而不是 NPOI 实现。所以我尝试在 C# 中编写更苗条的代码。
我试过的代码:
XSSFTable my_table = ((XSSFSheet)sheet).CreateTable();
CT_Table cttable = my_table.GetCTTable();
CT_TableStyleInfo table_style = new CT_TableStyleInfo();
cttable.tableStyleInfo = table_style;
table_style.name = "TableStyleMedium9";
table_style.showColumnStripes = false;
table_style.showRowStripes = true;
AreaReference my_data_range = new AreaReference(new CellReference(5, 0), new CellReference(10, 4));
cttable.@ref = my_data_range.FormatAsString();
cttable.displayName = "MYTABLE";
cttable.name = "Test";
cttable.id = 12;
注意:
找不到替代
CTTableStyleInfo table_style = cttable.addNewTableStyleInfo();
功能addNewTableStyleInfo()
不可用所以尝试像
CT_TableStyleInfo table_style = new CT_TableStyleInfo();
cttable.tableStyleInfo = table_style;
将流写入文件时:
(await workbook.ConfigureAwait(false)).Write(stream);
获得例外:
System.NullReferenceException:“对象引用未设置为对象的实例。”
堆栈跟踪:
at NPOI.XSSF.UserModel.XSSFTable.UpdateHeaders()
at NPOI.XSSF.UserModel.XSSFTable.WriteTo(Stream out1)
at NPOI.XSSF.UserModel.XSSFTable.Commit()
at NPOI.POIXMLDocumentPart.OnSave(List`1 alreadySaved)
at NPOI.POIXMLDocumentPart.OnSave(List`1 alreadySaved)
at NPOI.POIXMLDocumentPart.OnSave(List`1 alreadySaved)
at NPOI.POIXMLDocument.Write(Stream stream)
at App..Facade.MainFacade.<BuildReportAsync>d__25.MoveNext() in C:\App..\MainFacade.cs:line 215
如果我删除表格部分,那么工作正常,这意味着表格格式有问题。请帮忙。
解决方案
推荐阅读
- javascript - 从 Javascript 对象键中删除引号
- javascript - 获取 React 中函数返回的 promise 的值
- javascript - 鳗鱼python:Javascript错误鳗鱼不是函数
- image - Tkinter 没有在 Toplevel 窗口中显示图像,而其他人则显示
- botframework - 来自 IEventActivity 的 BotFramework 用户文化
- python - 将大型 JAR 二进制文件的 python 包装器打包到 PyPi
- azure - Azure 实例容器:容器连接问题
- bash - 在读取没有 EOF 的输入时如何让 grep 退出
- jmeter - 如何在 JMeter 中为 CSRF 令牌编写正则表达式?
- reactjs - Apollo GraphQL 缓存问题(cacheRedirects 和 readFragment)