简单的java的导出excel的方法

时间:2022-11-22 14:50:43
@RequestMapping(params="exportExcel")
public void exportExcel(HttpServletRequest request,HttpServletResponse response){
OutputStream ouputStream = null;
try{
response.setContentType("application/vnd.ms-excel");
String bankCardId = request.getParameter("bankCardId");
String month = request.getParameter("month");
String sy = request.getParameter("sy");
String fileName = sy+month;
if(StringUtil.isEmpty(fileName)){
fileName="allData";
}
// 根据浏览器进行转码
if (BrowserUtils.isIE(request)) 
{

response.setHeader("content-disposition",
"attachment;filename="+ java.net.URLEncoder.encode(fileName,"UTF-8") + ".xls");

} else {

String newtitle = new String(fileName.getBytes("ISO8859-1"),
"UTF-8");
response.setHeader("content-disposition","attachment;filename=" + newtitle + ".xls");
}


// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
       // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet  

       HSSFSheet sheet = wb.createSheet(fileName); 

//设置列宽
this.setSheetColumnWidth(sheet, 100);

       // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  
       HSSFRow row = sheet.createRow((int) 0);  
       // 第四步,创建单元格,并设置值表头 设置表头居中  
       HSSFCellStyle style = wb.createCellStyle();  
       style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
       HSSFCell cell = row.createCell((short) 0);  
       cell.setCellValue("编号");  
       cell.setCellStyle(style);  
       cell = row.createCell((short) 1);  
       cell.setCellValue("日期");  
       cell.setCellStyle(style);  
       cell = row.createCell((short) 2);  
       cell.setCellValue("项目");  
       cell.setCellStyle(style);  
       cell = row.createCell((short) 3);  
       cell.setCellValue("金额");  
       cell.setCellStyle(style); 
       cell = row.createCell((short) 4);  
       cell.setCellValue("内容");  
       cell.setCellStyle(style); 
List<Map<String,Object>> lists=dailyTotalAccountService.getFundList(bankCardId, month, sy);
if(lists.size()>0&&lists!=null){
BigDecimal money = null;
for(int i = 0; i < lists.size(); i++){
           row = sheet.createRow((int) i + 1);  
           // 第四步,创建单元格,并设置值  
           row.createCell((short) 0).setCellValue((String)lists.get(i).get("number"));  
           row.createCell((short) 1).setCellValue((String)lists.get(i).get("submitTime"));  
           row.createCell((short) 2).setCellValue((String)lists.get(i).get("projectName"));
           row.createCell((short) 3).setCellValue(lists.get(i).get("money").toString()); 
           row.createCell((short) 4).setCellValue((String)lists.get(i).get("description")); 
          


}
}
//写到输出流
ouputStream = response.getOutputStream();   
wb.write(ouputStream);  
}catch(Exception e){
e.printStackTrace();


}finally{

try{
if(ouputStream !=null){
ouputStream.flush();
    ouputStream.close();
}

}catch(Exception e){
e.printStackTrace();

}


}



/**
* 设置列宽

* @param sheet
*/
private void setSheetColumnWidth(HSSFSheet sheet, int count) {
for (int i = 0; i < count; i++) {
sheet.setColumnWidth(i, 25*256);
}
}