尝试使用此方法导出一个 Excel 文件:
private Sheet activeSheet;
public void addText(int column, int rowNumber, String s) throws Exception {
// TODO Auto-generated method stub
Row row = activeSheet.getRow(rowNumber);
if(row==null){
row = activeSheet.createRow(rowNumber);
}
Cell cell = row.createCell(column);
s = ToolUtils.nn(s);
if(s.indexOf("\n")!=-1){
int nbLines = StringUtils.countMatches(s, "\n") ;
CellStyle cs = workbook.createCellStyle();
cs.setWrapText(true);
//cs.set
cell.setCellStyle(cs);
row.setHeightInPoints(((nbLines+1)*activeSheet.getDefaultRowHeightInPoints()));
activeSheet.autoSizeColumn(column);
}
//cell.setCellValue(s);
cell.setCellValue(s == null ? "" : s);
}
我遇到了这个错误:
java.lang.IllegalStateException: Could not auto-size column. Make sure the column was tracked prior to auto-sizing the column
我尝试调用这个方法:
trackAllColumnsForAutoSizing()
修复了该问题,但它不能像工作表界面的已知方法一样工作。我需要升级 apache poi
版本吗?
我该如何修复将数据插入excel列的方法?
最佳答案
private SXSSFSheet activeSheet;
public void addText(int column, int rowNumber, String s) throws Exception {
// TODO Auto-generated method stub
ActiveSheet=workbook.createSheet();
Row row = activeSheet.getRow(rowNumber);
if(row==null){
row = activeSheet.createRow(rowNumber);
}
Cell cell = row.createCell(column);
cell.setCellValue(s == null ? "" : s);
s = ToolUtils.nn(s);
if(s.indexOf("\n")!=-1){
int nbLines = StringUtils.countMatches(s, "\n") ;
CellStyle cs = workbook.createCellStyle();
cs.setWrapText(true);
//cs.set
cell.setCellStyle(cs);
row.setHeightInPoints(((nbLines+1)*activeSheet.getDefaultRowHeightInPoints()));
activeSheet.trackColumnsForAutoSizing(); activeSheet.autoSizeColumn(column);
}
}
尝试一次可能会对您有所帮助 可能存在一些语法错误,刚刚在 IDE 中解决/调整了这些错误。
关于java - 如何使用 poi 3.15 处理 java 报告中的自动大小问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51423917/