6

enter image description here

I'm using NPOI to manipulate Excel(.xlsx) file data & format. I was wondering if there is a way to format the cell range to the table.

// something like.
ITable table = worksheet.FormatAsTable("A1:C4");

Have done some research on the internet but no luck yet. Any help would be much appreciated!

2
  • 1
    This link can be useful: it's a sample for Java POI, but NPOI is its .NET port and has similar (if not same) API.
    – Gian Paolo
    Dec 7, 2020 at 9:24
  • It works!! Thank you so so much!!
    – Rohim Chou
    Dec 7, 2020 at 10:54

2 Answers 2

7

[2021/05/28 Update]: Thanks for reminding. Found that without setting ctTable's id, name and displayName would get this error Removed Part: /xl/tables/table1.xml part with XML error. (Table) Load error. Line 1, column 247. (Following sample code fixed.)


Based on comment and link offered by @Gian Paolo, the C# way to achieve 'format as table' with NPOI would be like this:

Install-Package NPOI -Version 2.5.3

// NPOI dependencies
using NPOI.OpenXmlFormats.Spreadsheet;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;


IWorkbook workbook = new XSSFWorkbook();
XSSFSheet worksheet = workbook.CreateSheet("Grades") as XSSFSheet;

InsertTestData(worksheet);

// Format Cell Range As Table
XSSFTable xssfTable = worksheet.CreateTable();
CT_Table ctTable = xssfTable.GetCTTable();
AreaReference myDataRange = new AreaReference(new CellReference(0, 0), new CellReference(3, 2));
ctTable.@ref = myDataRange.FormatAsString();
ctTable.id = 1;
ctTable.name = "Table1";
ctTable.displayName = "Table1";
ctTable.tableStyleInfo = new CT_TableStyleInfo();
ctTable.tableStyleInfo.name = "TableStyleMedium2"; // TableStyleMedium2 is one of XSSFBuiltinTableStyle
ctTable.tableStyleInfo.showRowStripes = true;
ctTable.tableColumns = new CT_TableColumns();
ctTable.tableColumns.tableColumn = new List<CT_TableColumn>();
ctTable.tableColumns.tableColumn.Add(new CT_TableColumn() { id = 1, name = "ID" });
ctTable.tableColumns.tableColumn.Add(new CT_TableColumn() { id = 2, name = "Name" });
ctTable.tableColumns.tableColumn.Add(new CT_TableColumn() { id = 3, name = "Score" });

using (FileStream file = new FileStream(@"test.xlsx", FileMode.Create))
{
    workbook.Write(file);
}
// Function to Populate Test Data
private void InsertTestData(XSSFSheet worksheet)
{
    worksheet.CreateRow(0);
    worksheet.GetRow(0).CreateCell(0).SetCellValue("ID");
    worksheet.GetRow(0).CreateCell(1).SetCellValue("Name");
    worksheet.GetRow(0).CreateCell(2).SetCellValue("Score");
    worksheet.CreateRow(1);
    worksheet.GetRow(1).CreateCell(0).SetCellValue(1);
    worksheet.GetRow(1).CreateCell(1).SetCellValue("John");
    worksheet.GetRow(1).CreateCell(2).SetCellValue(82);
    worksheet.CreateRow(2);
    worksheet.GetRow(2).CreateCell(0).SetCellValue(2);
    worksheet.GetRow(2).CreateCell(1).SetCellValue("Sam");
    worksheet.GetRow(2).CreateCell(2).SetCellValue(90);
    worksheet.CreateRow(3);
    worksheet.GetRow(3).CreateCell(0).SetCellValue(3);
    worksheet.GetRow(3).CreateCell(1).SetCellValue("Amy");
    worksheet.GetRow(3).CreateCell(2).SetCellValue(88);
}

Result:

enter image description here

7
  • 1
    I'm trying your method, but I get this error when I open the excel file: "Removed Part: /xl/tables/table1.xml part with XML error. (Table) Load error. Line 1, column 247."
    – Rob None
    Apr 26, 2021 at 14:53
  • @RobNone Have the same issue. Did you resolved it? May 27, 2021 at 13:35
  • 2
    @KamilSpisak answer updated, could see if it works.
    – Rohim Chou
    May 28, 2021 at 1:30
  • 3
    @Rohim Chou Thanks a lot, it's working like a charm! Furthermore I've managed to add filtering to headers by using: ctTable.autoFilter = new CT_AutoFilter(); ctTable.autoFilter.@ref = myDataRange.FormatAsString(); Then the result looks exactly the same as in question's screenshot. Jun 2, 2021 at 14:45
  • 3
    @KamilSpisak Just in case someone comes looking for filters, setting the @ref prop didn't work for me. To get filters working, I had to do the following: ctTableCatStat.autoFilter = new CT_AutoFilter(); ctTableCatStat.autoFilter.filterColumn = new List<CT_FilterColumn>(); ctTableCatStat.autoFilter.filterColumn.Add(new CT_FilterColumn() { colId = 1, showButton = true }); This last one just repeat for each column you want filtered Sep 10, 2021 at 22:27
2

Not enough rep to add a comment - but if anyone is getting strange Excel errors when everything looks fine, I had an issue with iterating an object list into tableColumn. Building an array first fixed all my issues:

var headerNames = _headers.Select(x => x.Name).ToArray();
for (uint i = 0; i < headerNames.Count; i++)
{
    ctTable.tableColumns.tableColumn.Add(new CT_TableColumn() { id = i + 1, name = headerNames[i] });
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.