首页 > 代码库 > poi方法是导出Excel

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