標籤:poi方法是匯出excel
protected void renderMergedOutputModel(Map<String, Object> model,HttpServletRequest request, HttpServletResponse response)throws Exception {
Workbook wb = new XSSFWorkbook();
CellStyle cellStyle = wb.createCellStyle();
Font font = wb.createFont();
font.setColor(HSSFColor.RED.index); //紅色
cellStyle.setFont(font);
Sheet sheet1 = (Sheet) wb.createSheet("FactoryComplainReason");
Row rowHead = (Row) sheet1.createRow(0);
Cell cellHead = rowHead.createCell(0);
cellHead.setCellValue("投訴時間");
cellHead = rowHead.createCell(1);
cellHead.setCellValue("訂單編號");
cellHead = rowHead.createCell(2);
cellHead.setCellValue("商品明細");
for (int i = 1; i < 5; i++) {//行
Row row = (Row) sheet1.createRow(i);
//迴圈寫入列資料
for (int j = 0; j < 3; j++) { //列
Cell cell = row.createCell(j);
cell.setCellValue("測試"+j);
cell.setCellStyle(cellStyle);
}
}
//1、以彈出下載框的方式匯出
response.addHeader("Content-Disposition", "attachment;filename=FactoryComplainReason.xlsx");
//建立檔案流
response.setContentType("application/vnd.ms-excel;charset=gb2312");
OutputStream out = new BufferedOutputStream(response.getOutputStream());
//2、以指定下載路徑方式匯出 -start
//String path = "c:/";
//String fileName = "FactoryComplainReason3";
//String fileType = "xlsx";
//OutputStream out = new FileOutputStream(path+fileName+"."+fileType);
//以指定下載路徑方式匯出 -end
//寫入資料
wb.write(out);
out.flush();
//關閉檔案流
out.close();
}
poi方法是匯出Excel