0033-excel导出

来源:互联网 发布:印光法师文钞全集淘宝 编辑:程序博客网 时间:2024/06/17 22:48

        EO eo= new EO();//导出文件类
        eo.setExcelName(excelName);
        eo.setHeaders(StringUtils.join(map.values().toArray(), ",").split(","));
        eo.setHeaderKeys(StringUtils.join(map.keySet().toArray(), ",").split(","));
        eo.setDataMaps(map); 
        ExcelTemplate template = new ExcelTemplate();
        template.export(response, eo);

导出模板如下:

import java.io.IOException;

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
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.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;


public class ExcelTemplate{

    private static final Logger logger = Logger.getLogger(ExcelTemplate.class);

    private HSSFFont titleFont;

    private HSSFFont headFont;

    private HSSFFont contentFont;

    private HSSFCellStyle titleStyle;

    private HSSFCellStyle headStyle;

    private HSSFCellStyle contentStyle;

    public ExcelTemplateNew() {
    }


    public void export(HttpServletResponse response, EO eo) {
        try {
            response.setHeader("Content-Disposition", "attachment;filename="
                    + URLEncoder.encode(eo.getExcelName(), "UTF-8")
                    + ".xls");
        } catch (UnsupportedEncodingException e) {
            logger.error("生成Excel文件失败:" + e.getMessage(), e);
        }
        response.setContentType("application/x-msdownload");
        response.setCharacterEncoding("UTF-8");
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            HSSFWorkbook workbook = new HSSFWorkbook();
            create(workbook, eo);
            workbook.write(out);
        } catch (Exception e) {
            logger.error("导出文件时发生错误:" + e.getMessage(), e);
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                logger.error("导出文件时发生错误:" + e.getMessage(), e);
            }
        }

    }

    private void create(final HSSFWorkbook workbook,
            final EO eo) {
        initConfig(workbook);
        //create work set
        HSSFSheet sheet = ExcelUtil.createSheet(workbook,
                eo.getExceltName());

        String headers[] = eo.getHeaders();
        // Title
        HSSFRow titleRow = ExcelUtil.createRow(sheet, 0, 600);
        ExcelUtil.mergeCell(sheet, 0, 0, 0, headers.length);
        HSSFCell titleCell = titleRow.createCell(0);
        titleCell.setCellValue(eo.getReportName());
        titleCell.setCellStyle(titleStyle);

        // Header
        HSSFRow headRow = ExcelUtil.createRow(sheet, 1, 480);

        HSSFCell numCell = headRow.createCell(0);
        numCell.setCellValue("index");
        numCell.setCellStyle(headStyle);

        for (int i = 0; i < headers.length; i++) {
            HSSFCell headCell = headRow.createCell(i + 1);
            headCell.setCellValue(headers[i]);
            headCell.setCellStyle(headStyle);
        }

        List<Map<String, Object>> dataMaps = eo.getDataMaps();
        String headerKeys[] = eo.getHeaderKeys();

        //insert data
        for (int i = 0; i < dataMaps.size(); i++) {
            HSSFRow row = ExcelUtil.createRow(sheet, i + 2, 400);
            HSSFCell rowNum = row.createCell(0);
            rowNum.setCellValue(i + 1);
            rowNum.setCellStyle(contentStyle);
            for (int j = 0; j < headers.length; j++) {
                HSSFCell cell = row.createCell(j + 1);
                cell.setCellValue(dataMaps.get(i).get(headerKeys[j]) == null ? ""
                        : dataMaps.get(i).get(headerKeys[j]).toString());
                cell.setCellStyle(contentStyle);
            }
        }
    }

 
    private void initConfig(HSSFWorkbook workbook) {

        HSSFPalette palette = workbook.getCustomPalette();
        palette.setColorAtIndex((short) 8, (byte) (0xff & 255),
                (byte) (0xff & 255), (byte) (0xff & 255));
        palette.setColorAtIndex((short) 9, (byte) (0xff & 219),
                (byte) (0xff & 229), (byte) (0xff & 241));
        palette.setColorAtIndex((short) 10, (byte) (0xff & 79),
                (byte) (0xff & 129), (byte) (0xff & 189));

        this.titleFont = (HSSFFont) ExcelUtil.createFont(workbook,
                HSSFFont.BOLDWEIGHT_BOLD, HSSFFont.COLOR_NORMAL, (short) 11);

        this.headFont = (HSSFFont) ExcelUtil.createFont(workbook,
                HSSFFont.BOLDWEIGHT_NORMAL, (short) 8, (short) 11);

        this.contentFont = (HSSFFont) ExcelUtil.createFont(workbook,
                HSSFFont.BOLDWEIGHT_NORMAL, HSSFFont.COLOR_NORMAL, (short) 10);

        this.titleStyle = (HSSFCellStyle) ExcelUtil.createCellStyle(workbook,
                (short) 9, HSSFCellStyle.ALIGN_CENTER, titleFont);

        this.headStyle = (HSSFCellStyle) ExcelUtil.createCellStyle(workbook,
                (short) 10, HSSFCellStyle.ALIGN_CENTER, headFont);

        this.contentStyle = (HSSFCellStyle) ExcelUtil.createBorderCellStyle(
                workbook, HSSFColor.WHITE.index, (short) 8,
                HSSFCellStyle.ALIGN_CENTER, contentFont);
    }

    public HSSFFont getTitleFont() {
        return titleFont;
    }

    public void setTitleFont(HSSFFont titleFont) {
        this.titleFont = titleFont;
    }

    public HSSFFont getHeadFont() {
        return headFont;
    }

    public void setHeadFont(HSSFFont headFont) {
        this.headFont = headFont;
    }

    public HSSFFont getContentFont() {
        return contentFont;
    }

    public void setContentFont(HSSFFont contentFont) {
        this.contentFont = contentFont;
    }

    public HSSFCellStyle getTitleStyle() {
        return titleStyle;
    }

    public void setTitleStyle(HSSFCellStyle titleStyle) {
        this.titleStyle = titleStyle;
    }

    public HSSFCellStyle getHeadStyle() {
        return headStyle;
    }

    public void setHeadStyle(HSSFCellStyle headStyle) {
        this.headStyle = headStyle;
    }

    public HSSFCellStyle getContentStyle() {
        return contentStyle;
    }

    public void setContentStyle(HSSFCellStyle contentStyle) {
        this.contentStyle = contentStyle;
    }
}


0 0
原创粉丝点击