I am trying to write a ResultSet table in Excel (* .xlsx) using Apache Poi.
Invalid table object error in Office Excel
However, despite the fact that he writes the Excel file without any errors, when I try to open it in Office Excel 2013, it shows an error and deletes the table object to provide only a simple view of the data.


Here is an example of sample code in this example :
public static void writeExcel(ResultSet rs, int sqliteRowCount, String dir) { System.out.println("Writing Excel(*.xlsx) File..."); XSSFWorkbook workbook = null; try { if (rs != null) { // Get ResultSet MetaData ResultSetMetaData rsmd = rs.getMetaData(); // Number of columns int numColumns = rsmd.getColumnCount(); // Number of rows // + 1 for headers int numRows = sqliteRowCount + 1; workbook = new XSSFWorkbook(); // Create Excel Table XSSFSheet sheet = workbook.createSheet("Text"); XSSFTable table = sheet.createTable(); table.setDisplayName("Test"); CTTable cttable; cttable = table.getCTTable(); // Style configurations CTTableStyleInfo style = cttable.addNewTableStyleInfo(); style.setName("TableStyleMedium16"); style.setShowColumnStripes(false); style.setShowRowStripes(true); // Set Table Span Area AreaReference reference = new AreaReference(new CellReference(0, 0), new CellReference(numRows - 1, numColumns - 1)); cttable.setRef(reference.formatAsString()); cttable.setId(1); cttable.setName("Test"); cttable.setDisplayName("Test"); cttable.setTotalsRowCount(numRows); cttable.setTotalsRowShown(false); // Create Columns CTTableColumns columns = cttable.addNewTableColumns(); columns.setCount(numColumns); // Create Column, Row, Cell Objects CTTableColumn column; XSSFRow row; // Add Header and Columns XSSFRow headerRow = sheet.createRow(0); for (int i = 0; i < numColumns; i++) { column = columns.addNewTableColumn(); column.setName("Column" + (i + 1)); column.setId(i + 1); headerRow.createCell(i).setCellValue(rsmd.getColumnLabel(i + 1)); } // Write each row from ResultSet int rowNumber = 1; while (rs.next()) { row = sheet.createRow(rowNumber); for (int y = 0; y < numColumns; y++) { row.createCell(y).setCellValue(rs.getString(y + 1)); } rowNumber++; } // Set AutoFilter CTAutoFilter fltr = CTAutoFilter.Factory.newInstance(); fltr.setRef((new AreaReference(new CellReference(0, 0), new CellReference(numRows - 1, numColumns - 1))).formatAsString()); cttable.setAutoFilter(fltr); // sheet.setAutoFilter(CellRangeAddress.valueOf((new AreaReference(new CellReference(0, 0), new CellReference(numRows - 1, numColumns - 1))).formatAsString())); // Freeze Pan sheet.createFreezePane(0, 1, 0, 2); } } catch (SQLException ex) { System.out.println("SQL Error while writing Excel file!"); } finally { try { // Let write the excel file now if (workbook != null) { String excelDir = dir + File.separator + "workbook.xlsx"; try (final FileOutputStream out = new FileOutputStream(excelDir)) { workbook.write(out); } } } catch (IOException ex) { System.out.println("IO Error while writing Excel summary file!"); } } }
I know that something is wrong with my code, but I can not figure it out. Any idea why this is happening, where there will be a potential error in my code.
Update 1:
Table of XML file in Excel archive, if created using Apache POI
<?xml version="1.0" encoding="UTF-8"?> <table displayName="Test" ref="A1:B881" id="1" name="Test" totalsRowCount="881" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" totalsRowShown="0"><autoFilter ref="A1:B881"/><tableColumns count="2"><tableColumn name="ID" id="1"/><tableColumn name="Name" id="2"/><tableStyleInfo name="TableStyleMedium2" showColumnStripes="true" showRowStripes="true"/></table>
The table of the XML file in the Excel archive, if the table is created manually
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <table xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" id="1" name="Table1" displayName="Table1" ref="A1:B881" totalsRowShown="0"><autoFilter ref="A1:B881"/><tableColumns count="2"><tableColumn id="1" name="ID"/><tableColumn id="2" name="Name"/></tableColumns><tableStyleInfo name="TableStyleLight9" showFirstColumn="0" showLastColumn="0" showRowStripes="1" showColumnStripes="0"/></table>
In addition, if I open the Excel archive, it does not have a folder with a theme created by Apache POI, but it is present in the one that is created manually in Office Excel. Itβs strange.
Update 2: Example executable code (using Netbeans):
package apachepoi_exceltest; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.apache.poi.ss.util.AreaReference; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellReference; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFTable; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumns; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyleInfo; public class ApachePOI_ExcelTest { public static void main(String[] args) { String outputDir = "Your Local Directory Here";
Used libraries:
OOXML Schemas-1.1.jar
poi-3.11-beta2-20140822.jar
poi-OOXML-3,11-beta2-20140822.jar
XMLBeans-2.6.0.jar