POI导出Excel及设置单元格 例子

package com.krycai.report.servicepol;

import java.io.FileOutputStream;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;  //对Excel 97(-2007)文件操作的纯Java实现
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;

/**
 * 导出认证类型人流量统计表
 * @author Allen
 * @since: v1.0
 */
public class ReportForAuthenServicepoi {
private final static Logger logger = Logger.getLogger(ReportForAuthenService.class);

HSSFWorkbook wb;  //创建工作薄对象
    FileOutputStream fos;
   
/** 
  * 导出报表
  * 
  * @param reportBean     
* @throws Exception 
  */
public String exportExcel(ReportBean reportBean ) throws Exception {
        try {    
        
         //表单名称的处理过程
            String fileName = "认证途径人流量统计表_";
            if(StringUtils.stringNotEmpty(reportBean.getTimeType())){
             fileName += DateUtils.toString(new Date(), "yyyyMMdd") + "-" + reportBean.getTimeType();
            }else{
             fileName += reportBean.getBeginTime().replace("-", "")+"_"+reportBean.getEndTime().replace("-", "");
            }
            if(StringUtils.stringNotEmpty(reportBean.getShopName())){
             fileName +="_"+reportBean.getShopName();
            }
            if(reportBean.getAuthenType() != null){
             fileName +="_"+reportBean.getAuthenType().getMessage();
            }
            if(StringUtils.stringNotEmpty(reportBean.getWlcName())){
             fileName += "_" + reportBean.getWlcName();
            }
            fileName = fileName.replace(".", ",");
            fileName +=".xls";
            
            List<ReportForAuthenBean> reportForVisitorBeans = WifiUserReportService.reportAuthen(reportBean);
            fos = new FileOutputStream(WifiUserReportService.REPORT_PATH + fileName);
            wb= new HSSFWorkbook(); 
           
            HSSFSheet sh = wb.createSheet();
            sh.setColumnWidth( 0, 256*15+184);
            sh.setColumnWidth( 1, 256*30+184);  
            sh.setColumnWidth( 2, 256*30+184);  
            sh.setColumnWidth( 3, 256*30+184);  
            sh.setColumnWidth( 4, 256*30+184);  
            
            // 第一行表头标题,CellRangeAddress 参数:起始行号,终止行号, 起始列号,终止列号
            HSSFRow row = sh.createRow(0);
            HSSFCell cell0 = row.createCell(0);
            cell0.setCellValue(new HSSFRichTextString("认证途径人流量统计表"));
            cell0.setCellStyle(HSSFFontUtils.headFont(wb));
            sh.addMergedRegion(new CellRangeAddress(0, 0, 0,2));
            
            // 第二行时间表示
            HSSFRow row1 = sh.createRow(1);
            HSSFCell cell1 = row1.createCell(0);
            cell1.setCellValue(new HSSFRichTextString("时间范围:"));
            cell1.setCellStyle(HSSFFontUtils.cloFont(wb));
            sh.addMergedRegion(new CellRangeAddress(1, 1, 0, 0));


            if(StringUtils.stringNotEmpty(reportBean.getTimeType())){
                HSSFCell cell2 = row1.createCell(1);
                cell2.setCellValue(DateUtils.toString(new Date(), "yyyyMMdd") + "-" + reportBean.getTimeType());
                cell2.setCellStyle(HSSFFontUtils.dateFont(wb));
                sh.addMergedRegion(new CellRangeAddress(1, 1, 1, 2));
            }else{
                HSSFCell cell2 = row1.createCell(1);
                cell2.setCellValue(reportBean.getBeginTime()+ "至" + reportBean.getEndTime());
                cell2.setCellStyle(HSSFFontUtils.dateFont(wb));
                sh.addMergedRegion(new CellRangeAddress(1, 1, 1, 2));
            }
            
            if(StringUtils.stringNotEmpty(reportBean.getShopName())){
            
                HSSFCell cell3 = row1.createCell(2);
                cell3.setCellValue(new HSSFRichTextString("店铺:" + reportBean.getShopName()));
                cell3.setCellStyle(HSSFFontUtils.cloFont(wb));
                sh.addMergedRegion(new CellRangeAddress(1, 1, 3, 4));
            }
            
           // 第三行表头列名
            HSSFRow row3 = sh.createRow(2);
            HSSFCell cell = row3.createCell(0);
            cell.setCellValue(new HSSFRichTextString("序号")); 
            cell.setCellStyle(HSSFFontUtils.cloFont(wb));
            
            cell = row3.createCell(1);
            cell.setCellValue(new HSSFRichTextString("认证途径"));
            cell.setCellStyle(HSSFFontUtils.cloFont(wb));


            cell = row3.createCell(2);
            cell.setCellValue(new HSSFRichTextString("人流量"));
            cell.setCellStyle(HSSFFontUtils.cloFont(wb));
            
            cell = row3.createCell(3);
            cell.setCellValue(new HSSFRichTextString("已关注"));
            cell.setCellStyle(HSSFFontUtils.cloFont(wb));
            
            cell = row3.createCell(4);
            cell.setCellValue(new HSSFRichTextString("未关注"));
            cell.setCellStyle(HSSFFontUtils.cloFont(wb));
            
            //填充数据的内容
            int i = 3, j = 1, z = 0;
            ReportForAuthenBean reportForVisitorBean=new ReportForAuthenBean();
            while(z<reportForVisitorBeans.size()){
             //ws.setRowView(i, 450);
            
             reportForVisitorBean=reportForVisitorBeans.get(z);
             row = sh.createRow(i);
            
             cell = row.createCell(0);
                cell.setCellValue(new HSSFRichTextString(String.valueOf(j))); 
                cell.setCellStyle(HSSFFontUtils.cloFont(wb));
                
                cell = row.createCell(1);
                cell.setCellValue(new HSSFRichTextString(reportForVisitorBean.getAuthenType().getMessage()));
                cell.setCellStyle(HSSFFontUtils.cloFont(wb));


                cell = row.createCell(2);
                cell.setCellValue(Double.parseDouble(reportForVisitorBean.getCount()));
                cell.setCellStyle(HSSFFontUtils.cloFont(wb));
                
                cell = row.createCell(3);
                cell.setCellValue(Double.parseDouble(reportForVisitorBean.getSubscribe()));
                cell.setCellStyle(HSSFFontUtils.cloFont(wb));
                
                cell = row.createCell(4);
                cell.setCellValue(Double.parseDouble(reportForVisitorBean.getUnsubscribe()));
                cell.setCellStyle(HSSFFontUtils.cloFont(wb));
                
                i++;
             j++;
             z++;
            }
            
            wb.write(fos);
            return WifiUserReportService.REPORT_PATH + fileName;
        }catch (Exception e){
         logger.error("[ReportForAuthenService]==> 生成认证途径人流量统计表失败:,异常信息:", e);
}finally {
try {
if(fos != null){
fos.close();
}
}
catch (Exception e) {
logger.error("[ReportForAuthenService]==> 关闭流失败,异常信息:", e);
}
}
return null;
    }
            
    
    


}

猜你喜欢

转载自blog.csdn.net/baidu_28068985/article/details/80584778
今日推荐