1

I am trying to add table format after writing data into sheet. I came across this blog which has POI not NPOI implementation. So I tried writing slimier in C#.

Code I tried:

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;

Note: Could not find alternate for CTTableStyleInfo table_style = cttable.addNewTableStyleInfo(); as function addNewTableStyleInfo() not available so tried like

CT_TableStyleInfo table_style = new CT_TableStyleInfo();
cttable.tableStyleInfo = table_style;

While writing the stream to file:

(await workbook.ConfigureAwait(false)).Write(stream);

Getting Exception:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

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

If I remove table part then working fine which means something wrong while table format. Please help.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Prem
  • 316
  • 1
  • 5
  • 23
  • Using NPOI version 2.3.0 – Prem Jul 09 '19 at 11:17
  • 1
    Hi @uwe-keim, Thanks for the response. I have idea about NullReferenceException but in my case I am missing something which is not specified in the question reference you mentioned. Please remove the duplicate marker. Thanks again.! – Prem Jul 10 '19 at 05:50
  • 1
    The question is regarding how to add table format style in excel using NPOI not NullReferenceException. – Prem Jul 10 '19 at 05:54

0 Answers0