I'm trying to export JTable to XLSX file using xlsx4j, but have trouble with setting the columns widths. I've succeeded in setting the default width for all the columns, but I would like to set the widths of columns to fit data lenght automatically:
public void exportToXLS() throws Exception
{
//Create the spreadsheet
SpreadsheetMLPackage pkg = SpreadsheetMLPackage.createPackage();
WorksheetPart sheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Sheet1", 1);
//Set default format for rows and columns
CTSheetFormatPr format = Context.getsmlObjectFactory().createCTSheetFormatPr();
format.setDefaultColWidth(20.0);
format.setDefaultRowHeight(16.8);
format.setCustomHeight(Boolean.TRUE);
sheet.getJaxbElement().setSheetFormatPr(format);
//Get a few JTable properties
int iSelectedTab = tabPane.getSelectedIndex();
int[] rowsSelected = table[iSelectedTab].getSelectedRows();
int iColumns = table[0].getColumnCount();
int iCurrentRow;
//Retrieve SheetData from sheet object
SheetData sheetData = sheet.getJaxbElement().getSheetData();
Row row;
Cell cell;
//Copy data from JTable to spreadsheet
for(int iRow=0; iRow < rowsSelected.length; iRow++)
{
iCurrentRow = rowsSelected[iRow];
row = Context.getsmlObjectFactory().createRow();
List<Cell>rowl = row.getC();
for(int iCol = 0; iCol < iColumns; iCol++)
{
cell = Context.getsmlObjectFactory().createCell();
CTRst ctrst = new CTRst();
ctrst.setT(table[iSelectedTab].getValueAt(iCurrentRow, iCol).toString());
cell.setT(STCellType.INLINE_STR);
cell.setIs(ctrst);
rowl.add(cell);
}
sheetData.getRow().add(row);
}
//Set the columns widths
List<Cols> lstCols = sheet.getJaxbElement().getCols();
System.out.println("lstCols.size() is " + lstCols.size());
for(int i = 0; i< lstCols.size(); i++)
{
List<Col> lstCol = lstCols.get(i).getCol();
System.out.println("lstCol.size() is " + lstCol.size());
for(int j=0; j<lstCol.size(); j++)
{
lstCol.get(j).setBestFit(Boolean.TRUE);
lstCol.get(j).setWidth(30.0);
System.out.println("Column " + i + " : " + j);
}
}
//Save the spreadsheet to file
pkg.save(new File("Export.xlsx"));
}
The above code shows in console: lstCols.size() is 0 so it looks like after adding rows and cells to the SheetData, there are no columns definitions.
On the other hand if I create the columns manually this way:
Cols cols = Context.smlObjectFactory.createCols();
Col col = Context.smlObjectFactory.createCol();
col.setBestFit(Boolean.TRUE);
cols.getCol().add(col);
sheet.getJaxbElement().getCols().add(cols);
I get the catastrophic error in XLSX file.
From the ECMA-376 spec (I looked at 3ed, section 18.3.1.17, p1774):
This example shows that column 4 (D) has 'best fit' applied to it, which is also a custom width. Also, column 5 (E) is listed as having a custom width and a style applied at the column level (as opposed to the cell level).
<cols>
<col min="4" max="4" width="12" bestFit="1" customWidth="1"/>
<col min="5" max="5" width="9.140625" style="3"/>
</cols>
so, it looks like you just need to complete your col object definition, by specifying the col number (via min/max attributes).
<col min="2" max="2" bestFit="1"/>
gives a col which starts with zero width, so you should specify @width.
@bestFit doesn't seem to do what you'd expect (you want something more like autoFit?).
See custom-column-widths-in-excel-open-xml and calculating-column-widths-in-excel-open-xml for more.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.