gpt4 book ai didi

java - 使用 autoSizeColumn 时列在 XSSF Apache POI 中隐藏

转载 作者:行者123 更新时间:2023-11-30 11:23:56 25 4
gpt4 key购买 nike

我正在尝试使用 Apache POI XSSF 将表导出为 xlsx。该表有 5 列。

第一行的标题栏合并了 5 列。在第二行,表格的 5 个标题。其余行是数据。我想将列的宽度设置为每个标题 block 的最大宽度。

我在添加标题值时添加了一个条件并尝试了 mySheet.autoSizeColumn(colnum) ,以便列的宽度与标题数据相同。但是当我这样做时,所有列都隐藏在导出的 xlsx 工作表中。

这些是我使用的样式元素:

public CellStyle createHeadingStyle(XSSFWorkbook myWorkBook){

XSSFCellStyle style = myWorkBook.createCellStyle();
Font fontHeader = myWorkBook.createFont();
fontHeader.setBoldweight((short)4);
fontHeader.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
fontHeader.setFontHeightInPoints((short)8);
fontHeader.setFontName("GE Inspira Pitch");
style.setFont(fontHeader);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setHidden(false);

//HSSFPalette palette = myWorkBook.getCustomPalette();
//palette.setColorAtIndex(new Byte((byte) 41), new Byte((byte) 153), new Byte((byte) 204), new Byte((byte) 255));

style.setFillForegroundColor(new XSSFColor(new java.awt.Color(41,153,204,255)));
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
return style;
}

public CellStyle createTitleStyle(XSSFWorkbook myWorkBook){

CellStyle style = myWorkBook.createCellStyle();
Font fontHeader = myWorkBook.createFont();
fontHeader.setBoldweight((short)5);
fontHeader.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
fontHeader.setFontHeightInPoints((short)10);
fontHeader.setColor(HSSFColor.RED.index);
fontHeader.setFontName("GE Inspira Pitch");
style.setFont(fontHeader);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setWrapText(true);
style.setHidden(false);
return style;
}
public CellStyle createDataStyle(XSSFWorkbook myWorkBook){

CellStyle style = myWorkBook.createCellStyle();
Font font = myWorkBook.createFont();
font.setFontName("GE Inspira Pitch");
font.setFontHeightInPoints((short)10);
style.setFont(font);
style.setAlignment(CellStyle.ALIGN_RIGHT);
style.setHidden(false);
return style;
}

这是创建 xlsx 的代码块:

titleLenght = 2;
cell1.setCellValue(excelTitle);
cell1.setCellStyle(titleCellStyle);
boolean headerStyle = true;
for (int j = 0; j < list.size(); j++) {
Row row = mySheet.createRow(j+2+titleLength);
List<String> l2= list.get(j);
for(int k=0; k<l2.size(); k++)
{
Cell cell = row.createCell(k);
cell.setCellValue(l2.get(k));
if(headerStyle){
mySheet.autoSizeColumn(k);
cell.setCellStyle(headingCellStyle);
}else{
cell.setCellStyle(dataCellStyle);
}
}
headerStyle = false;
}

最佳答案

我对代码进行了以下更改并且它起作用了。

titleLenght = 2;
cell1.setCellValue(excelTitle);

boolean headerStyle = true;
for (int j = 0; j < list.size(); j++) {
Row row = mySheet.createRow(j+2+titleLength);
List<String> l2= list.get(j);
for(int k=0; k<l2.size(); k++)
{
Cell cell = row.createCell(k);
cell.setCellValue(l2.get(k));
if(headerStyle){
**cell.setCellStyle(headingCellStyle);
int origColWidth = mySheet.getColumnWidth(k);
mySheet.autoSizeColumn(k);
// Making sure col width is not going less than the header width
if (origColWidth > mySheet.getColumnWidth(k)) {
mySheet.setColumnWidth(k, origColWidth);
}**
}else{
cell.setCellStyle(dataCellStyle);
}
}
headerStyle = false;
}


// Set title style element at last so the first column will not take
// width of title field
cell1.setCellStyle(titleCellStyle);

在我之前的代码中,对于 header ,autoSizeColumn(K) 返回零,因为没有与之关联的样式。具体来说,没有分配字体,因为我在设置样式之前设置了 used autoSizeColumn(k)。因此宽度返回为零。这使得所有列都被隐藏了。

现在我添加了一个条件,使宽度在任何情况下都不会变为零。我已将标题样式移动到末尾,这样它就不会影响第一个标题列的宽度。

结论:

  1. 创建单元格
  2. 设置值和设置样式
  3. 设置 autoColumnSize 或计算列宽

关于java - 使用 autoSizeColumn 时列在 XSSF Apache POI 中隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20904938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com