Excel Apache poi autosizecolumn不工作
我想将对象列表写入excel文件。但autoSizeColumn不起作用。我的代码如下:Excel Apache poi autosizecolumn不工作,excel,apache-poi,autosize,Excel,Apache Poi,Autosize,我想将对象列表写入excel文件。但autoSizeColumn不起作用。我的代码如下: List<ReportTmPerHour> reportTm = reportTm(); Iterator<ReportTmPerHour> iterator = reportTm.iterator(); String fileName="C:\\Users\\kkk\\Desktop\\mmm_ccc.xlsx"; Workbook
List<ReportTmPerHour> reportTm = reportTm();
Iterator<ReportTmPerHour> iterator = reportTm.iterator();
String fileName="C:\\Users\\kkk\\Desktop\\mmm_ccc.xlsx";
Workbook workbook = null;
if(fileName.endsWith("xlsx")){
workbook = new XSSFWorkbook();
}else if(fileName.endsWith("xls")){
workbook = new HSSFWorkbook();
}else{
try {
throw new Exception("invalid file name, should be xls or xlsx");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Sheet sheet = workbook.createSheet("TM");
int rowIndex = 0;
createHeadersForTm(sheet);
while(iterator.hasNext()){
ReportTmPerHour reportTmPerHour = iterator.next();
Row row = sheet.createRow(++rowIndex);
Cell cell = row.createCell(0);
cell.setCellValue(reportTmPerHour.getIsemriNo());
cell.setCellStyle(arg0);
Cell cell10 = row.createCell(1);
cell10.setCellValue(reportTmPerHour.getSube());
Cell cell11=row.createCell(2);
cell11.setCellValue(reportTmPerHour.getAob());
Cell cell12=row.createCell(3);
cell12.setCellValue(reportTmPerHour.getKesintiBildirim());
Cell cell13=row.createCell(4);
cell13.setCellValue(reportTmPerHour.getAciklama());
Cell cell14=row.createCell(5);
cell14.setCellValue(reportTmPerHour.getEtkilenenMahalleler());
Cell cell15=row.createCell(6);
if (reportTmPerHour.getBaslamaZamani()!=null) {
cell15.setCellValue(reportTmPerHour.getBaslamaZamani());
}
Cell cell16=row.createCell(7);
cell16.setCellValue(reportTmPerHour.getTahminiBitisZamani());
Cell cell17=row.createCell(8);
cell17.setCellValue(reportTmPerHour.getTmName());
Cell cell18=row.createCell(9);
cell18.setCellValue(reportTmPerHour.getEtkilenensayi());
}
columnAutoSize(sheet);
//lets write the excel data to file now
FileOutputStream fos;
try {
fos = new FileOutputStream(new File(fileName));
workbook.write(fos);
fos.close();
System.out.println(fileName + " written successfully");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
private void columnAutoSize(Sheet sheet) {
for(int columnPosition = 1; columnPosition< 9; columnPosition++) {
sheet.autoSizeColumn(columnPosition);
}
}
相当肯定的是,columnAutoSizesheet;不是内置POI函数-代码在哪里?我编辑了问题并添加了方法@Gagravarr我错在哪里?@Gagravarr