Java
[Apache POI] xlsx 파일을 xls 파일로 변환하는 메서드
브라이언7
2015. 4. 29. 16:35
나는 check box와 같은 Excel FormControl 까지 변환하기를 원했는데, 다음과 같은 코드로는 Form Control들까지 복사되지 않는다.
[출처] http://stackoverflow.com/questions/20049922/java-poi-api-convert-from-xlsx-to-xls
/** * xlsx 파일(XSSFWorkbook)을 xls 파일(HSSFWorkbook)로 변환하는 메서드. * [참조] http://stackoverflow.com/questions/20049922/java-poi-api-convert-from-xlsx-to-xls * * @param xssfWorkbook * @return */ private HSSFWorkbook convertXlsxToXls(XSSFWorkbook xssfWorkbook) { HSSFWorkbook hssfWorkbook = new HSSFWorkbook(); int sheetCnt = xssfWorkbook.getNumberOfSheets(); for (int i = 0; i < sheetCnt; i++) { Sheet sIn = xssfWorkbook.getSheetAt(i); Sheet sOut = hssfWorkbook.createSheet(sIn.getSheetName()); IteratorrowIt = sIn.rowIterator(); while (rowIt.hasNext()) { Row rowIn = rowIt.next(); Row rowOut = sOut.createRow(rowIn.getRowNum()); Iterator
cellIt = rowIn.cellIterator(); while (cellIt.hasNext()) { Cell cellIn = cellIt.next(); Cell cellOut = rowOut.createCell(cellIn.getColumnIndex(), cellIn.getCellType()); switch (cellIn.getCellType()) { case Cell.CELL_TYPE_BLANK: break; case Cell.CELL_TYPE_BOOLEAN: cellOut.setCellValue(cellIn.getBooleanCellValue()); break; case Cell.CELL_TYPE_ERROR: cellOut.setCellValue(cellIn.getErrorCellValue()); break; case Cell.CELL_TYPE_FORMULA: cellOut.setCellFormula(cellIn.getCellFormula()); break; case Cell.CELL_TYPE_NUMERIC: cellOut.setCellValue(cellIn.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: cellOut.setCellValue(cellIn.getStringCellValue()); break; } CellStyle styleIn = cellIn.getCellStyle(); CellStyle styleOut = cellOut.getCellStyle(); styleOut.setDataFormat(styleIn.getDataFormat()); cellOut.setCellComment(cellIn.getCellComment()); } } } return hssfWorkbook; } |
위 웹페이지에 다음과 같은
getOutputStream() has already been called for this response
에러 해결책이 나와 있는데, 이렇게 하면 되는구나. 흥미롭다.
out.clear();
//out--> jsp자체 객체
out=pageContext.pushBody();
//out--> jsp자체 객체
//getOutputStream() has already been called for this response
//오류 해결을 위판 편법
ServletOutputStream sout = response.getOutputStream();
newWb.write(sout);