java导出Excel遇到的问题

在同一个sheet页中,如果需要展示两个表格 如果直接sheet.createRow(index)那么表格1里面就会没有值,表格2里面有值。正确的做法是表格1里面创建行数sheet.createRow(index),表格2直接获取行数进行操作sheet.getRow(index);这样表格1和表格2里面的值都会显示。 如果在表格1和表格3之间有很多空行,当你在循环空行之前没有创建对应的行数,那么就会有空指针异常,在循环之前创建对应行数就可以了sheet.createRow(number); 样式失效问题,我将表格设置边框为黑色,只有前面三行有效,后面的失效了,百度了一下,发现是因为我将设置的样式也循环了,应该将设置的样式放在循环外,直接在单元格里面设置样式就可以了 for(int i = 0; i < xx.size; i++){ HSSFCellStyle styleGreen = workbook.createCellStyle(); styleGreen.setAlignment(HorizontalAlignment.CENTER); //水平居中 styleGreen.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中 styleGreen.setWrapText(true);//设置自动换行 styleGreen.setFillForegroundColor(IndexedColors.LIME.getIndex()); styleGreen.setFillPattern(FillPatternType.SOLID_FOREGROUND); styleGreen.setTopBorderColor(IndexedColors.BLACK.getIndex());//黑色边框 styleGreen.setLeftBorderColor(IndexedColors.BLACK.getIndex()); styleGreen.setBottomBorderColor(IndexedColors.BLACK.getIndex()); styleGreen.setRightBorderColor(IndexedColors.BLACK.getIndex()); styleGreen.setBorderTop(BorderStyle.THIN); styleGreen.setBorderLeft(BorderStyle.THIN); styleGreen.setBorderRight(BorderStyle.THIN); styleGreen.setBorderBottom(BorderStyle.THIN); cell.setCellStyle(styleGreen); } 改为: HSSFCellStyle styleGreen = workbook.createCellStyle(); styleGreen.setAlignment(HorizontalAlignment.CENTER); //水平居中 styleGreen.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中 styleGreen.setWrapText(true);//设置自动换行 styleGreen.setFillForegroundColor(IndexedColors.LIME.getIndex()); styleGreen.setFillPattern(FillPatternType.SOLID_FOREGROUND); styleGreen.setTopBorderColor(IndexedColors.BLACK.getIndex());//黑色边框 styleGreen.setLeftBorderColor(IndexedColors.BLACK.getIndex()); styleGreen.setBottomBorderColor(IndexedColors.BLACK.getIndex()); styleGreen.setRightBorderColor(IndexedColors.BLACK.getIndex()); styleGreen.setBorderTop(BorderStyle.THIN); styleGreen.setBorderLeft(BorderStyle.THIN); styleGreen.setBorderRight(BorderStyle.THIN); styleGreen.setBorderBottom(BorderStyle.THIN); for(int i = 0; i < xx.size; i++){ cell.setCellStyle(styleGreen); }

经验分享 程序员 微信小程序 职场和发展