欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POI导出Excel

程序员文章站 2022-07-13 13:18:19
...
Logexport 
 

package org.ld.zjchannels.export;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.ld.zjchannels.bean.ActionLog;
import org.ld.zjchannels.utils.DateEx;

public class Logexport {
 public void writeExcel(List list,String no)
 {
  try {
  //构建一个输出流
  FileOutputStream Fileops=new FileOutputStream(no);
  //实例化一个工作薄
  HSSFWorkbook hssfwb=new HSSFWorkbook(); 
  HSSFFont headFont =hssfwb.createFont();
  headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  headFont.setFontHeightInPoints((short)10); //字体大小         
  headFont.setColor(HSSFFont.COLOR_RED); //字体颜色 注意使用HSSFont下的颜色常
  HSSFCellStyle normalStyle = hssfwb.createCellStyle();
  normalStyle.setFont(headFont);//设置字体
  
  //normalStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
  //normalStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
  //normalStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); 

  //设置工作薄的名称
  HSSFSheet hssfs=hssfwb.createSheet("业务日志");
  hssfs.setColumnWidth((short)1,(short)5000);//  设置单元格宽度
  hssfs.setColumnWidth((short)2,(short)5000);
  hssfs.setColumnWidth((short)3,(short)5000);
  hssfs.setColumnWidth((short)4,(short)7000);
  hssfs.setColumnWidth((short)5,(short)4000);
  hssfs.setColumnWidth((short)6,(short)4000);
  hssfs.setColumnWidth((short)7,(short)4000);
  HSSFRichTextString s=null;
  
  HSSFRow hsr=hssfs.createRow(0);  
  
  s=new HSSFRichTextString("日志编号");
  HSSFCell cell0=hsr.createCell((short)0);
  cell0.setCellStyle(normalStyle);
  cell0.setCellValue(s);
  
  s=new HSSFRichTextString("推荐者号码");
  HSSFCell cell1=hsr.createCell((short)1);
  cell1.setCellStyle(normalStyle);
  cell1.setCellValue(s);
  
  s=new HSSFRichTextString("办理号码");
  HSSFCell cell2=hsr.createCell((short)2);
  cell2.setCellStyle(normalStyle);
  cell2.setCellValue(s);
  
  
  s=new HSSFRichTextString("业务名称");
  HSSFCell cell4=hsr.createCell((short)3);
  cell4.setCellStyle(normalStyle);
  cell4.setCellValue(s);
  
  s=new HSSFRichTextString("备注信息");
  HSSFCell cell7=hsr.createCell((short)4);
  cell7.setCellStyle(normalStyle);
  cell7.setCellValue(s);
  
  s=new HSSFRichTextString("推荐或办理时间");
  HSSFCell cell8=hsr.createCell((short)5);
  cell8.setCellStyle(normalStyle);
  cell8.setCellValue(s);
  
  //s=new HSSFRichTextString("计算");
  //hsr.createCell((short)7).setCellValue(s);
  //hsr.createCell((short)6).setCellStyle();
  int i=1;//设置行号,从0开始,因为第一行被表头占据;所以从1开始的   
  for(int b=0;b<list.size();b++)
  {
   Map map= (HashMap)list.get(b);
   //动态创建行;
   HSSFRow row=hssfs.createRow(i);
   //创建单元格
   HSSFCell cell=row.createCell((short)0); 
   
   s=new HSSFRichTextString(map.get("LOGNO")+"");
   row.createCell((short)0).setCellValue(s);   
   
   s=new HSSFRichTextString(map.get("LOGRECOMMENDTEL")+"");
   row.createCell((short)1).setCellValue(s);
   
   s=new HSSFRichTextString(map.get("LOGTELEPHONE")+"");
   row.createCell((short)2).setCellValue(s);
   
   s=new HSSFRichTextString(map.get("PRONAME")+"");
   row.createCell((short)3).setCellValue(s);
   
   s=new HSSFRichTextString(map.get("LOGREMARK")+"");
   row.createCell((short)4).setCellValue(s);
   
   s=new HSSFRichTextString(map.get("LOGTIME")+"");
   row.createCell((short)5).setCellValue(s);
   
   // cell = row.createCell((short)7);
    //执行Excel公式
        //  cell.setCellFormula("SUM(A"+(i+1)+":E"+(i+1)+")");
          //System.out.println("SUM(A"+(i+1)+":E"+(i+1)+")");
          i++;    
  }
  hssfwb.write(Fileops);  
  }catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
}
Action中实现下载Excel代码

 Logexport logexprt=new Logexport();
   String no=request.getRealPath("upload/exportFile")+"\\"+CheckData.createId()+".xls";
   logexprt.writeExcel(page.getItems(), no);
   SmartUpload su=new SmartUpload();   
   su.initialize(this.getServlet().getServletConfig(),request,response);
   su.setContentDisposition(null);
   su.downloadFile(no);
   return  null;

 

 HSSFCellStyle cellStyle= wookBook.createCellStyle();
  cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  cellStyle.setBorderBottom(HSSFCellStyle.BorderBORDER_MEDIUM);
  cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
  cellStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
  cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
  cellStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
  cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
  cellStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
  cellStyle.setTopBorderColor(HSSFColor.BLACK.index);

 

          sheet.autoSizeColumn(( short ) 0 ); // 调整第一列宽度 
          sheet.autoSizeColumn(( short ) 1 ); // 调整第二列宽度 
          sheet.autoSizeColumn(( short ) 2 ); // 调整第三列宽度 
          sheet.autoSizeColumn(( short ) 3 ); // 调整第四列宽度