1. 程式人生 > >使用POI匯出EXCEL表格

使用POI匯出EXCEL表格

1.工具類

public class ExcelUtil {
	@SuppressWarnings("deprecation")
	public static void createCell(HSSFCellStyle cellstyle, HSSFRow row, short col, String val) {
		HSSFCell cell = row.createCell(col);
		cell.setCellValue(val);
		cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);
		cell.setCellStyle(cellstyle);
	}

	public static void createExcel(OutputStream os, List<RiskCount> riskCount) throws Exception {
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFCellStyle cellstyle = wb.createCellStyle();
		HSSFSheet sheet = wb.createSheet();
		HSSFRow row = sheet.createRow((short) 0);
		sheet.createFreezePane(0, 1);
		ExcelUtil.createCell(cellstyle, row, (short) 0, "組織機構");  //中文是列名,根據實際情況自己寫
		ExcelUtil.createCell(cellstyle, row, (short) 1, "風險型別");
		ExcelUtil.createCell(cellstyle, row, (short) 2, "風險名稱");
		ExcelUtil.createCell(cellstyle, row, (short) 3, "風險結果");
		ExcelUtil.createCell(cellstyle, row, (short) 4, "風險等級");
		ExcelUtil.createCell(cellstyle, row, (short) 5, "專業分類");
		ExcelUtil.createCell(cellstyle, row, (short) 6, "風險數量");
		ExcelUtil.createCell(cellstyle, row, (short) 7, "狀態");
		ExcelUtil.createCell(cellstyle, row, (short) 8, "賬期");

		int i = 0;
		for (RiskCount risk : riskCount) {
			HSSFRow rowi = sheet.createRow((short) (++i));
			ExcelUtil.createCell(cellstyle, rowi, (short) 0, risk.getProvinceCode());
			ExcelUtil.createCell(cellstyle, rowi, (short) 1, risk.getRiskCode());
			ExcelUtil.createCell(cellstyle, rowi, (short) 2, risk.getRiskName());
			ExcelUtil.createCell(cellstyle, rowi, (short) 3, risk.getRiskType());
			ExcelUtil.createCell(cellstyle, rowi, (short) 4, risk.getRiskGrade());
			ExcelUtil.createCell(cellstyle, rowi, (short) 5,
					risk.getRiskCount() == null ? "0" : risk.getRiskCount().toString());
			ExcelUtil.createCell(cellstyle, rowi, (short) 6, risk.getCountDate());
			ExcelUtil.createCell(cellstyle, rowi, (short) 7, risk.getRiskResult());
			ExcelUtil.createCell(cellstyle, rowi, (short) 8, risk.getStatus());

		}
		wb.write(os);
		os.flush();
		os.close();
	}
}

2

[email protected](value="/exportExcel/{id}",method=RequestMethod.GET)
		public void exportExcel(HttpServletRequest req,HttpServletResponse res,
														@PathVariable String id) throws Exception{
			
			List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
			Map<String,Object> map=new HashMap<String,Object>();
		try{		
				String[] para=id.split(",");			
				for(int i=0;i<para.length;i++){
					map=riskCountService.getReportExportById(para[i].trim());									
				    list.add(map);
				}		
			OutputStream os= res.getOutputStream();
			String fileName="風險報表";
			res.setHeader("content-disposition", "attachment;fileName="
	           +new String(fileName.getBytes("gb2312"),"ISO8859-1")+".xls");
			res.setContentType("application/msexcel");			
			ExcelUtil.createReportExcel(os, list);
			logger.info("風險報表匯出請求成功");
			}catch(Exception e){
				e.printStackTrace();
				logger.info("風險報表匯出異常" + e.getMessage());
			}
		 
       }