POI導入データを使ってExcelデータ量が多すぎるとエラー解決案が出ます。
7314 ワード
エクスポートエクセルは4000行のエラーを超えてはいけません。 POI操作エクセルでは、導出したデータが大きくない場合は問題ないが、データが多い場合や多い場合は、
以下のエラーを報告します。cell styleが多すぎるので、cell tyleをサイクル外に置いてエラーを報告します。
java.lang.IllagalStation Exception:The maximnumber of cell style was exceded.You can define up to 4000 style in a.xls workbook at org.apaache.poi.hssf.usermodel.HSSFWork book.createCelStele(HSSFWork book.java:1162)
エラーの例
以下のエラーを報告します。cell styleが多すぎるので、cell tyleをサイクル外に置いてエラーを報告します。
java.lang.IllagalStation Exception:The maximnumber of cell style was exceded.You can define up to 4000 style in a.xls workbook at org.apaache.poi.hssf.usermodel.HSSFWork book.createCelStele(HSSFWork book.java:1162)
エラーの例
for (int i = 0; i < 10000; i++) {
Row row = sheet.createRow(i);
Cell cell = row.createCell((short) 0);
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
style.setFont(font);
cell.setCellStyle(style);
}
修正後の正しいコードCellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
style.setFont(font);
for (int i = 0; i < 10000; i++) {
Row row = sheet.createRow(i);
Cell cell = row.createCell((short) 0);
cell.setCellStyle(style);
}
以下はエクセルのコアコードを生成します。public static void export( ExcelBean excelBean, HttpServletResponse response) throws IOException {
String filename = excelBean.getName();
filename = new String(filename.replaceAll("\\s|;", "").getBytes("gbk"), "ISO8859-1");
response.setContentType("application/octet-stream;charset=utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=" + filename);
OutputStream outputStream = response.getOutputStream();
createExcel(excelBean, outputStream);
outputStream.flush();
outputStream.close();
}
public static void createExcel(ExcelBean excelBean, OutputStream outputStream) throws IOException{
//
HSSFWorkbook wb = new HSSFWorkbook();
//
HSSFSheet sheet = wb.createSheet(excelBean.getSheetName());
HSSFRow row0 = sheet.createRow(0);
int descIndexBegin = 9;
// int descIndexEnd = 12;
HSSFRow row9 = sheet.createRow(descIndexBegin);
HSSFRow row = sheet.createRow(1);
row.setHeightInPoints(25.0f);// :25
//
HSSFCellStyle style = wb.createCellStyle();
HSSFCellStyle descStyle = wb.createCellStyle();
//
setAlignCenter(style);
//
setTitleStyle(wb,style);
if(excelBean.isHeadBold()){
HSSFFont headfont = wb.createFont();
headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setFont(headfont);
}
HSSFCell cell;
ExcelTitle[] titles = excelBean.getTitles();
//========== ( )==========
if(titles.length>=2){
sheet.addMergedRegion(new CellRangeAddress(0,0,0,titles.length-1));// , , ,
}
cell = row0.createCell(0);
String topTitle = excelBean.getName();
String titleName = topTitle.substring(0,topTitle.lastIndexOf("."));
cell.setCellValue(titleName);//
cell.setCellStyle(style);
setTopTitleStyle(wb,cell,titleName);
//========== ( )==========
//==================== ==========begin==========
String desc = excelBean.getDescription();
if(StringUtils.isNoneBlank(desc)){
if(titles.length>=2){
sheet.addMergedRegion(new CellRangeAddress(descIndexBegin,descIndexBegin,0,titles.length-1));//firstRow, lastRow, firstCol, lastCol
}
HSSFCell descCell = row9.createCell(0);
descCell.setCellValue("** :"+desc+"\r
");//
// setAlignLeft(style);
setAlignLeftAndTop(descCell,descStyle);//
}
//==================== ==========end==========
//==================== ====================
for(int i=0; i < titles.length; i++){
ExcelTitle title = titles[i];
cell= row.createCell(i);
// ' 、 ',
String titleValue = title.getValue();
int beginIndex = titleValue.indexOf("(");
int endIndex = titleValue.indexOf(")");
String subTitle = titleValue.substring(beginIndex,endIndex);
if(subTitle.contains(" ") || subTitle.contains(" ")){
HSSFRichTextString ts= new HSSFRichTextString(titleValue);
HSSFFont font = wb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setColor(HSSFColor.RED.index);
font.setFontHeightInPoints((short) 14);//
HSSFFont font2 = wb.createFont();
font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font2.setFontHeightInPoints((short) 14);//
ts.applyFont(0,beginIndex,font2);//
ts.applyFont(beginIndex,endIndex+1,font);//
cell.setCellValue(ts);
}else{
cell.setCellValue(titleValue);
}
cell.setCellStyle(style);
int columnWidth = title.getWidth() > 0 ? title.getWidth() : excelBean.getColumnWidth();
sheet.setColumnWidth(i, getColWidth(columnWidth));
}
//==================== ====================
//==================== tips:example====================
int rowIndex = 2;
int rowHeight = excelBean.getRowHeight();
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFCellStyle cellStyle = wb.createCellStyle();
for(String[] data : excelBean.getDataList()){
row = sheet.createRow(rowIndex ++ );
if(rowHeight > 0){
row.setHeight((short) getRowHeight(rowHeight));
}
else{
rowHeight = 18;
}
for(int j=0; j 0 ? titles[j].getWidth() : excelBean.getColumnWidth();
insertImage(wb, patriarch, value, rowHeight, columnWidth, (short)(j), rowIndex);
}
else{
setLink(wb, cell, value);
}
}else{*/
cell.setCellValue(value);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cell.setCellStyle(cellStyle);
/* }*/
}
}
for (String[] tipsList : excelBean.getTipsList()) {
row = sheet.getRow(1);//
for(int j=0; j