我试图使用设置列宽在 excel 2010 上设置列宽



我正在尝试使用 POI 设置setColumnWidth(rowindex, width)colunm宽度,问题是宽度是双倍,上面的函数只接受 Int

我尝试使用autosizecolumn但它仍然存在于列中填充不足的空间,我认为问题是 excel 和 java 之间的字体不匹配

法典

RowTotal = NewSheet.createRow(0);
cell1 = RowTotal.createCell(0);
cell1.setCellValue("Row Name/Label");
cell1 = RowTotal.createCell(1);
cell1.setCellValue("Sum of premium payable");
cell1 = RowTotal.createCell(2);
cell1.setCellValue("Sum of arrears payable");
NewSheet.setColumnWidth(0, 3755.84);
NewSheet.setColumnWidth(1, 5519.68);;
NewSheet.setColumnWidth(2, 5207.36);

您不能将双精度值传递给 setColumnWidth 和 autosizecolumn,因为 Apache POI 的两种方法都接受 int 值作为参数。 Apache POI的方法: 1. 自动调整列大小(整数列( 调整列宽以适合内容。 2. 设置列宽(int columnIndex, int width( 设置宽度(以字符宽度的 1/256 为单位( 您可以参考 https://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFSheet.html 了解更多详情。

使用:

NewSheet.getColumnHelper().setColWidth(0, 3755.84);
NewSheet.getColumnHelper().setCustomWidth(0, true);

我不确定第二行是否必要,但如果您在 excel 中手动更改列的宽度,它将自动设置 CustomWidth = true。所以我只是为了安全起见而添加它。

最新更新