ExcelUtils.java 4.5 KB
package com.jevon.utils;

import com.google.common.base.Strings;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author chen
 * @version 1.0
 * @date 2019/10/16 0016 18:46
 */
public class ExcelUtils {

    //各个列的表头
    private List<String> heardList;
    //各个列的元素key值
    private List<String> heardKey;
    //需要填充的数据信息
    private List<Map<String,String>> data;
    //工作表
    private String sheetName = "模板";


    public List<String> getHeardList() {
        return heardList;
    }

    public void setHeardList(List<String> heardList) {
        this.heardList = heardList;
    }

    public List<String> getHeardKey() {
        return heardKey;
    }

    public void setHeardKey(List<String> heardKey) {
        this.heardKey = heardKey;
    }

    public List<Map<String, String>> getData() {
        return data;
    }

    public void setData(List<Map<String, String>> data) {
        this.data = data;
    }

    public String getSheetName() {
        return sheetName;
    }

    public void setSheetName(String sheetName) {
        this.sheetName = sheetName;
    }

    /**
     * 开始导出数据信息
     *
     */
    public byte[] exportExport(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //检查参数配置信息
        checkConfig();
        //创建工作簿
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建工作表
        HSSFSheet wbSheet = wb.createSheet(this.sheetName);

        //在第0行创建rows
        HSSFRow row = wbSheet.createRow((int)0);
        //设置列头元素
        HSSFCell cellHead = null;
        for (int i = 0; i < heardList.size(); i++) {
            cellHead = row.createCell(i);
            cellHead.setCellValue(heardList.get(i));
        }
        //开始写入实体数据信息
        int a = 1;
        for (int i = 0; i < data.size(); i++) {
            HSSFRow roww = wbSheet.createRow((int) a);
            Map map = data.get(i);
            HSSFCell cell = null;
            for (int j = 0; j < heardKey.size(); j++) {
                cell = roww.createCell(j);
                Object valueObject = map.get(heardKey.get(j));
                String value = null;
                if (valueObject == null) {
                    valueObject = "";
                }
                if (valueObject instanceof String) {
                    //取出的数据是字符串直接赋值
                    value = (String) map.get(heardKey.get(j));
                } else if (valueObject instanceof Integer) {
                    //取出的数据是Integer
                    value = String.valueOf(((Integer) (valueObject)).floatValue());
                } else if (valueObject instanceof BigDecimal) {
                    //取出的数据是BigDecimal
                    value = String.valueOf(((BigDecimal) (valueObject)).floatValue());
                } else {
                    value = valueObject.toString();
                }
                cell.setCellValue(Strings.isNullOrEmpty(value) ? "" : value);
            }
            a++;
        }

        //导出数据
        try {
            //设置Http响应头告诉浏览器下载这个附件
            response.setHeader("Content-Disposition", "attachment;Filename=" + System.currentTimeMillis() + ".xls");
            OutputStream outputStream = response.getOutputStream();
            wb.write(outputStream);
            outputStream.flush();
            outputStream.close();
            return wb.getBytes();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new IOException("导出Excel出现严重异常,异常信息:" + ex.getMessage());
        }

    }

    /**
     * 检查数据配置问题
     *
     * @throws IOException 抛出数据异常类
     */
    protected void checkConfig() throws IOException {
        if (heardKey == null || heardList.size() == 0) {
            throw new IOException("列名数组不能为空或者为NULL");
        }
        if (StringUtils.isBlank(sheetName)) {
            throw new IOException("工作表表名不能为NULL");
        }
    }

}