java - 如何使用 inig poi 3.15 处理 java 报告中的自动大小问题?
问题描述
尝试使用此方法导出一个 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:无法自动调整列大小。确保在自动调整列大小之前跟踪列
我试图调用这个方法:
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 中的那些。
推荐阅读
- javascript - HTMLButtonElement.onclick 中未定义如何修复功能
- python - cookie_str = match.group(1).AttributeError: 'NoneType' 对象没有属性 'group'
- python - Pandas 基于旧列的新列,有条件处理无值
- css - SVG 填充的 CSS 过渡不会同时发生
- html - JS 事件后 div 中的项目移动
- vue.js - 使用 Vue.set() 更新 Vuex 存储中的对象数组中的整个对象?
- reactjs - React - 音频播放器 mp3 源文件
- angular - Angular Material - 如何在运行时更改按钮颜色
- angular - 如何将检查值放入 Angular 模板驱动表单 (ngModel) 的数组中
- tensorflow - 如何为无监督学习模型创建损失函数,其中输出类似于游戏代理的直接输入?