我正在尝试将数据写入表后添加表格式。我碰到了this blog,它具有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:'对象引用未设置为对象的实例。'
StackTrace:
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
如果我删除表格部分,则工作正常,这表示表格格式有问题。请帮忙。