导出excel

1.添加辅助类
public class Import {
public static HSSFWorkbook getHSSFWorkbook(String sheetName,String []title,String [][]values, HSSFWorkbook wb){
        // 第一步,创建一个webbook,对应一个Excel文件 
       if(wb == null){
           wb = new HSSFWorkbook();
       }
       // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet 
       HSSFSheet sheet = wb.createSheet(sheetName); 
       // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short 
       HSSFRow row = sheet.createRow(0); 
       // 第四步,创建单元格,并设置值表头 设置表头居中 
       HSSFCellStyle style = wb.createCellStyle(); 
       style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 
      
       HSSFCell cell = null; 
       //创建标题
       for(int i=0;i<title.length;i++){
           cell = row.createCell(i); 
           cell.setCellValue(title[i]); 
           cell.setCellStyle(style); 
       }
       //创建内容
       for(int i=0;i<values.length;i++){
           row = sheet.createRow(i + 1);
           for(int j=0;j<values[i].length;j++){
                row.createCell(j).setCellValue(values[i][j]);
           }
       }
      
      return wb;
   }
}

/**************************************************************************/
调用以及具体实现

@RequestMapping(value = "/excel")
    @ResponseBody
    public void exportFeedBack(HttpServletResponse response){

  String fileName = "商品信息"+System.currentTimeMillis()+".xls";//excel的名称
  String sheetName = "商品信息";//sheet名称
 
  String []title = new String[]{"商品ID","商品名称","价格","库存","描述"};
  List<Goods> list =goodsService.getList();
  String [][]values = new String[list.size()][];
  for(int i=0;i<list.size();i++){
  values[i] = new String[title.length];
  Goods obj = list.get(i);
  values[i][0] = obj.getGid()+"";
             values[i][1] = obj.getGname()+"";
             values[i][2] = obj.getPrice()+"";
             values[i][3] = obj.getKucun()+"";
             values[i][4] = obj.getGdesc()+"";

  }    
             HSSFWorkbook wb = Import.getHSSFWorkbook(sheetName, title, values, null);
            
             //将文件存到指定位置 
             try { 
                  this.setResponseHeader(response, fileName); 
                  OutputStream os = response.getOutputStream(); 
                  wb.write(os); 
                  os.flush(); 
                  os.close(); 
             } catch (Exception e) { 
                  e.printStackTrace(); 
             } 
            
  }
    
     public void setResponseHeader(HttpServletResponse response, String fileName) { 
         try {
              fileName = new String(fileName.getBytes(),"ISO8859-1"); 
              response.setContentType("application/octet-stream;charset=ISO8859-1"); 
              response.setHeader("Content-Disposition", "attachment;filename="+ fileName); 
              response.addHeader("Pargam", "no-cache"); 
              response.addHeader("Cache-Control", "no-cache"); 
         } catch (Exception ex) { 
              ex.printStackTrace(); 
         } 
    }

注:只支持.xls格式的,07版的.xlsx无法打开!
jsp页面编写:<input type="button" onclick="importExcel();" value="导出excel">
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.11.3.js"></script>
<script type="text/javascript">
   function importExcel(){
   window.location.href="<%=request.getContextPath()%>/goods/excel";  
   }
</script>

猜你喜欢

转载自ear.iteye.com/blog/2363533