POI导出EXCEL的做法

用POI导出Excel 模板
   导出的时候,应当分为三步:
     第一步,建立一个导出Excel格式的工具类,方便在后面的控制器中直接调用生成Excel格式;
     第二步,用spring+mybatis的框架创建service来创建所需要导出的文件的方法(),注意要分层次来写,从controller调用service接口的方法,而service的方法实现是在serviceimpl,并且serviceImpl类中SQL方法的实现是调用Mapper接口中方法。而Mapper接口方法的实现是在映射Mapper.xml定义;
     第三步,在控制层将第二步查询的数据拼装到第一步的导出文件的格式中,在页面使用按钮绑定事件导出。
   注意:
1.格式尽量完整,功能齐全,拼装数据时候注意:为空字符串时候,要判断,为空就赋值空;很重要,不然会报错!!!文件名在不同的浏览器的兼容的问题。表格下的命名。
      2.在命名方法和控制器时候,要遵循驼峰规则和快速知道方法功能的习惯,所以,写上注释的代码是很有必要的。建议养成这个良好的习惯。
     3.开发模块时候,要注意分步来实现功能的做法,这样是最有力自己完善和完成模块的。
     4.知识点和不会的,要弄懂,尤其是联合查询几个数据库,分页和按时间排序等的。细节是尤其需要注意的。

  下面是曾经做过的项目的模版:
  导出格式类
package com.util;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import com.ancun.bqbh.capture.model.BqbhProductConsume;

/**
* <p>
* ExcelExport类主要用于excel导出(POI)
*
*/
public class ExcelExport {
        public static Workbook export(List<BqbhProductConsume> list) { 
        //格式化日期  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String[] excelHeader = { "时间", "概要", "类型","文件大小/MB","花费数额","到期时间"};
            // 声明一个工作薄
            //HSSFWorkbook wb = new HSSFWorkbook();
        Workbook  wb = new SXSSFWorkbook();
            // 生成一个表格 这个是考虑50万条都可以导出的大数据表格
            Sheet sheet = wb.createSheet("保全记录");
            //设置表格默认列宽度
            sheet.setDefaultColumnWidth(20);
           
             

            // 生成一个样式 
            CellStyle headStyle = wb.createCellStyle(); 
            CellStyle bodyStyle = wb.createCellStyle();
            // 设置这些样式
           

            headStyle.setFillForegroundColor(IndexedColors.YELLOW.index);// 设置背景色
            headStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            bodyStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            bodyStyle.setFillForegroundColor(IndexedColors.WHITE.index);
           
            //设置边框
            headStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框

            headStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框

            headStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框

            headStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
            
            headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
            headStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
           
            bodyStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框

            bodyStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框

            bodyStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框

            bodyStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
            
            bodyStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
           
            bodyStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
           
             // 设置字体
            Font font = wb.createFont();
            font.setColor(HSSFColor.BLACK.index);//字体颜色黑色
            font.setFontHeightInPoints((short) 12);//设置字体大小
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
           
            Font bodyFont = wb.createFont();
            bodyFont.setColor(HSSFColor.BLACK.index);//字体颜色黑色
            bodyFont.setFontHeightInPoints((short) 12);//设置字体大小
            // 把字体应用到当前的样式
//            bodyStyle.setFont(bodyFont);
            headStyle.setFont(font);
            Row row = sheet.createRow((int) 0); //设置第一行的表格名字
            // 获取填充表格的第一行的数据
            for (int i = 0; i < excelHeader.length; i++) {   
                Cell cell = row.createCell(i);   
                cell.setCellValue(excelHeader[i]);   
                cell.setCellStyle(headStyle);  //设置单元格格式 
//                sheet.autoSizeColumn(i); //自动确定宽度  
            }   
            //循环将数据 集合list填入表格的行
            Integer total=0;
            for (int i = 0,j=list.size(); i < j+1; i++) {
            //从第二行开始填充数据
                row = sheet.createRow(i + 1);
                Cell cell0=row.createCell(0);
                    Cell cell1=row.createCell(1);
                    Cell cell2=row.createCell(2);
                    Cell cell3=row.createCell(3);
                    Cell cell4=row.createCell(4);
                    Cell cell5=row.createCell(5);
                    cell0.setCellStyle(bodyStyle);
                    cell1.setCellStyle(bodyStyle);
                    cell2.setCellStyle(bodyStyle);
                    cell3.setCellStyle(bodyStyle);
                    cell4.setCellStyle(bodyStyle);
                    cell5.setCellStyle(bodyStyle);
                   
                   
                if(i==j){
                row.setHeight((short) 600);
                cell0.setCellValue("总计");
                        cell4.setCellValue(total.intValue());
                        break;
            }
              
                BqbhProductConsume bqc = list.get(i);
                total+=(null!=bqc.getSaveMount()?bqc.getSaveMount():0);
                //建立时间
                    String getCreateDate=(null!=bqc.getGmtCreate()?sdf.format(bqc.getGmtCreate()):"");
                    //时间
                    String getSaveFinishTime=(null!=bqc.getSaveFinishTime()?sdf.format(bqc.getSaveFinishTime()):"");
                   
                    cell0.setCellValue(getCreateDate);
                    cell1.setCellValue(bqc.getSaveDesc());
                    //cell2.setCellValue(bqc.getSaveType());
                    cell3.setCellValue(bqc.getSaveSizeString());
                    cell4.setCellValue(bqc.getSaveMount());
                    cell5.setCellValue(getSaveFinishTime);
                   
                     //判断保存类型
                     if (bqc.getSaveType()==1) {
                    cell2.setCellValue("网页");
                   
}else{
cell2.setCellValue("作品");
}
           }   
           return wb;   
      }      

    /**
     * 
     * @Title: processFileName
     * 
     * @Description: ie,chrom,firfox下处理文件名显示乱码
     */ 
    public static String processFileName(HttpServletRequest request, String fileNames) { 
            String codedfilename = null; 
            try { 
                String agent = request.getHeader("USER-AGENT"); 
                if (null != agent && -1 != agent.indexOf("MSIE") || null != agent 
                        && -1 != agent.indexOf("Trident")) {//IE 浏览器
     
                    String name = java.net.URLEncoder.encode(fileNames, "UTF8"); 
     
                    codedfilename = name; 
                } else if (null != agent && -1 != agent.indexOf("Mozilla")) {// 火狐、谷歌等 
     
     
                    codedfilename = new String(fileNames.getBytes("UTF-8"), "iso-8859-1"); 
                } 
            } catch (Exception e) { 
                e.printStackTrace(); 
            } 
            return codedfilename; 
        } 
}   

 

猜你喜欢

转载自wangyf-love-dch.iteye.com/blog/2249737