java生成导出excle文件

public void getExportExcle(List< Object > list,HttpServletResponse response) throws UnsupportedEncodingException {
                //转码 
 response.setContentType("application/vnd.ms-excel;charset=UTF-8");
 String filename = new String("生成Excle.xls".getBytes("gb2312"),"ISO8859-1");
 response.addHeader("Content-Disposition", "filename=" + filename);
                //设置字体样式
 WritableFont wfont = new WritableFont(WritableFont.createFont("宋体"), 13, WritableFont.BOLD, false,                 UnderlineStyle.NO_UNDERLINE,Colour.BLACK);  
 WritableCellFormat wcfFC = new WritableCellFormat(wfont);
                Date date = new Date(); 

 WritableWorkbook wwb = null;  
 try {
 OutputStream os=response.getOutputStream();

 //创建Excel工作表 
 wwb =  Workbook.createWorkbook(os);

 //创建sheet 
 WritableSheet ws = wwb.createSheet("库存产品", 0); 

 //合并单元格(左列,左行,右列,右行)从第1行第1列到第2行第4列 
 ws.mergeCells(0, 0, 6, 1);
 Label header = new Label(0, 0, "库存产品",  wcfFC);     
 ws.addCell(header);
 ws.mergeCells(0, 2, 6, 2); 
 Label l = new Label(0, 2, "导出时间:"+date.toLocaleString());//第3列    
 ws.addCell(l); 
 l = new Label(0, 3, "仓库编号",  wcfFC );//第2行,第3列    
 ws.addCell(l);
 l = new Label(1, 3, "仓库名称",  wcfFC );//第2行,第2列    
 ws.addCell(l);
 l = new Label(2, 3, "产品编号",  wcfFC );//第2行,第4列    
 ws.addCell(l);
 l = new Label(3, 3, "产品名称",  wcfFC );//第2行,第2列    
 ws.addCell(l);
 l = new Label(4, 3, "当前库存",  wcfFC );//第2行,第4列    
 ws.addCell(l);
 l = new Label(5, 3, "包装单位",  wcfFC );//第2行,第2列    
 ws.addCell(l);
 l = new Label(6, 3, "包装条码",  wcfFC);//第2行,第2列    
 ws.addCell(l);

 ws.setColumnView(0,12);    
 ws.setColumnView(1,30);    
 ws.setColumnView(2,15);
 ws.setColumnView(3,30);    
 ws.setColumnView(4,15);    
 ws.setColumnView(5,15);
 ws.setColumnView(6,20);
 for(int i=0;i<list.size();i++){

 Object object=list.get(i);
 if( object !=null){
 l = new Label(0, i+4, "测试",  wcfFC );//第2行,第1列    
 ws.addCell(l);
 l = new Label(1, i+4,  "测试",  wcfFC );//第2行,第2列  
 ws.addCell(l);
 l = new Label(2, i+4,  "测试",  wcfFC );//第2行,第3列  
 ws.addCell(l);
 l = new Label(3, i+4,  "测试",  wcfFC );//第2行,第4列   
 ws.addCell(l);
 l = new Label(4, i+4,  "测试",  wcfFC );//第2行,第5列  
 ws.addCell(l);
 l = new Label(5, i+4,  "测试",  wcfFC );//第2行,第6列  
 ws.addCell(l);
 l = new Label(6, i+4, "测试",  wcfFC );//第2行,第7列    
 ws.addCell(l);
 }
 }
 wwb.write(); 
 wwb.close();    
 os.flush();
 os.close();
 }
 catch (WriteException ex) {    
 ex.printStackTrace();    
 } catch (IOException ex) {    
 ex.printStackTrace();    
 }    

 }


你可能感兴趣的:(java导出excle文件)