poi 导出 excel

运去金成铁,时来铁似金。读书须用意,一字值千金。

1、前端

    // 导出只能使用 window.location.href 文件才能下载,用其他的不行,所以请求参数只能用 get 的方式拼接在后面
   getExcel(){
       window.location.href = "../sys/statistics/poi?dateBegin="+this.query.dateBegin+"&dateEnd="+this.query.dateEnd+"&itemCode="+this.query.itemCode+"&cuCode="+this.query.cuCode+"&classis="+this.query.classis+"&brand="+this.query.brand;
   },

2、加遮罩
答:参考博客 导出 excel 加遮罩

3、后端控制层

    @GetMapping("/poi")
    @ApiOperation(value = "导出excel", notes = "")
    public R poi( HttpServletResponse response, HttpServletRequest request
                ,@RequestParam String dateBegin
                ,@RequestParam String dateEnd
                ,@RequestParam String itemCode
                ,@RequestParam String cuCode
                ,@RequestParam String classis
                ,@RequestParam String brand
    ){
        OrderStatisticsDto orderStatisticsDto = new OrderStatisticsDto();
        orderStatisticsDto.setDateBegin(dateBegin);
        orderStatisticsDto.setDateEnd(dateEnd);
        orderStatisticsDto.setItemCode(itemCode);
        orderStatisticsDto.setCuCode(cuCode);
        orderStatisticsDto.setClassis(classis);
        orderStatisticsDto.setBrand(brand);
        return (R) disTableForStatisticsService.poi(response,request,orderStatisticsDto);
    }

4、后端服务层

    public R poi(HttpServletResponse response, HttpServletRequest request,OrderStatisticsDto orderStatisticsDto) {

//        orderStatisticsDto.setPage(1);
//        orderStatisticsDto.setRows(10);
        Map<String, Object> condition = orderStatisticsDto.getCondition();
        condition.put("dataType", "DataCount");
        List<Map> orderStatisticsCount = disTableForStatisticsDao.getOrderStatistics(condition);
        Integer rsCount = (Integer)orderStatisticsCount.get(0).get("RSCount");
        List<OrderStatisticsVo> orderStatisticsVos = getOrderStatisticsVos(condition, rsCount);

        try {
            String title ="订单统计表";
//            String[] rowsName=new String[]{"序号","ID","会计科目名称","会计名称描述","记账方向","记账方向"};
            ExportExcelUtils ex =new ExportExcelUtils(title, domain, orderStatisticsVos,response);
            ex.exportData();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }

5、导出工具类
答:单元格颜色
  poi 导出 excel

package com.platform.utils;

import com.platform.response.statistics.OrderStatisticsBrand;
import com.platform.response.statistics.OrderStatisticsItem;
import com.platform.response.statistics.OrderStatisticsVo;
import com.platform.response.statistics.PicColor;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddress;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;


public class ExportExcelUtils {
    private String title; // 导出表格的表名

    private String domain;// 服务器域名

    private BufferedImage bufferImg = null;

    private List<OrderStatisticsVo>  dataList = new ArrayList<>(); // 对象数组的List集合

    private HttpServletResponse  response;

    // 传入要导入的数据
    public ExportExcelUtils(String title,String domain,List<OrderStatisticsVo>  dataList,HttpServletResponse  response){
        this.title=title;
        this.domain=domain;
        this.dataList=dataList;
        this.response = response;
    }
    // 导出数据
    public void exportData(){
        try {
            HSSFWorkbook workbook =new HSSFWorkbook(); // 创建一个excel对象
            HSSFSheet sheet =workbook.createSheet(title); // 创建表格
            sheet.setColumnWidth(0, 20 * 256); // 设置第一列的宽度
            sheet.setColumnWidth(1, 40 * 256); // 设置第二列的宽度
            sheet.setColumnWidth(2, 20 * 256); // 设置第一列的宽度
            sheet.setColumnWidth(3, 15 * 256); // 设置第一列的宽度
            sheet.setColumnWidth(4, 10 * 256); // 设置第一列的宽度
            sheet.setColumnWidth(5, 10 * 256); // 设置第一列的宽度


            // 产生表格标题行
            HSSFRow rowm  =sheet.createRow(0);  // 行
            HSSFCell cellTiltle =rowm.createCell(0);  // 单元格

            // sheet样式定义
            HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook); // 头样式
            HSSFCellStyle style = this.getStyle(workbook);  // 单元格样式
            /**
             * 参数说明
             * 从0开始   第一行 第一列 都是从角标0开始
             * 行 列 行列    (0,0,0,5)  合并第一行 第一列  到第一行 第六列
             * 起始行,起始列,结束行,结束列
             *
             * new Region()  这个方法使过时的
             */
            // 合并第一行的所有列
            sheet.addMergedRegion(new CellRangeAddress(0, (short) 0, 0, (short) (5)));
            cellTiltle.setCellStyle(columnTopStyle);
            cellTiltle.setCellValue(title);

//            int columnNum = rowName.length;  // 表格列的长度
// 第二行====================================================================================
            int r = 1;
//            for(int i = 1 ;i<2+1 ;i++){
            for(OrderStatisticsVo data: dataList){


                HSSFRow rowRowName = getRows(workbook, sheet, r++);

                getCell(columnTopStyle, rowRowName, 0, "供应商名称");
                getCell(style, rowRowName, 1, data.getVsi_VendorName());
                getCell(columnTopStyle, rowRowName, 2, "联系电话");
                getCell(style, rowRowName, 3, data.getVD_TEL1()+" "+data.getVD_CONTACTEL2());
                getCell(columnTopStyle, rowRowName, 4, "地址");
                getCell(style, rowRowName, 5, data.getVD_ADDR());

    // 第三行====================================================================================
                HSSFRow rowRowNameThree = getRows(workbook, sheet, r++);

                getCell(columnTopStyle, rowRowNameThree, 0, "品牌");
                getCell(columnTopStyle, rowRowNameThree, 1, "款号");
                getCell(columnTopStyle, rowRowNameThree, 2, "图片");
                getCell(columnTopStyle, rowRowNameThree, 3, "颜色");
                getCell(columnTopStyle, rowRowNameThree, 4, "尺码");
//                getCell(columnTopStyle, rowRowNameThree, 5, "备注");

                HSSFRow rowRowNameFour = null;
                int brandRow = 0;
                for (OrderStatisticsBrand brand: data.getOrderStatisticsBrandList()){// 渲染品牌
    // 第四行====================================================================================


                    int firstRow = r; // 用以合并单元格

                    rowRowNameFour = getRows(workbook, sheet, r++);

                    getCell(style, rowRowNameFour, 0, brand.getVsi_brandName());
//                    getCell(style, rowRowNameFive, 1, "A280260");
//                    getCell(style, rowRowNameFive, 2, "暂无图片");
//                    getCell(style, rowRowNameFive, 3, "白色");
//                    getCell(style, rowRowNameFive, 4, "120");
//                    getCell(style, rowRowNameFive, 5, "130");
//                    getCell(style, rowRowNameFive, 6, "140");


        // 第五行====================================================================================
                    int brandSize = 0; // 控制尺码行占用品牌行
                    for (OrderStatisticsItem item : brand.getOrderStatisticsItemList()){ // 渲染款
                        brandRow++; // 控制合并 尺码
                        int itemRow = r; // 用以合并单元格

                        brandSize++;
                        if (brandSize != 1) { // 尺码一行,第一次不建,其他都要建
                            rowRowNameFour = getRows(workbook, sheet, r++);// 款号
                            itemRow = r;
                        }
//                        getCell(style, rowRowNameFour, 0, "");
                        getCell(style, rowRowNameFour, 1, item.getVsi_item());

                        int size = 4;
                        if(item.getVsi_sizeName().size() > 1 && brandRow == 1){
                            sheet.addMergedRegion(new CellRangeAddress(r-2, r-2, 4, 4+item.getVsi_sizeName().size()-1)); // 合并 尺码 单元格
                        }
                        for(String sizeName : item.getVsi_sizeName()){ // 渲染尺码名字
                            getCell(columnTopStyle, rowRowNameFour, size++, sizeName);
                        }

                        for(PicColor picColor : item.getPicColors()){ // 颜色
                            int colorCol = 3;
                            rowRowNameFour = getRows(workbook, sheet, r++);
                            getCell(style, rowRowNameFour, 0, "");//品牌一列
                            getCell(style, rowRowNameFour, 1, "");//款号一列

                            // 单元格中写入图片
                            try {
                                ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
                                bufferImg = (BufferedImage) ImageIO.read(getFileFromImageUrl(domain+picColor.getPic()));
                                ImageIO.write(bufferImg, "jpg", byteArrayOut);
                                HSSFPatriarch patri_item = sheet.createDrawingPatriarch();
                                HSSFClientAnchor anchor_item = new HSSFClientAnchor(0, 0, 0, 0, (short) (colorCol-1), r-1, (short) colorCol, r);
                                patri_item.createPicture(anchor_item , workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
                            } catch (Exception e) {
                                e.printStackTrace();
                                getCell(style, rowRowNameFour, colorCol-1, "暂无图片");
                            }

//                            getCell(style, rowRowNameFour, colorCol-1, "暂无图片");
                            getCell(columnTopStyle, rowRowNameFour, colorCol, picColor.getColorName());
                            for(Integer sizeL : picColor.getSizeList()){
                                getCell(style, rowRowNameFour, ++colorCol, sizeL+"");
                            }

                        }



                        sheet.addMergedRegion(new CellRangeAddress(itemRow-1, r-1, 1, 1)); // 合并款号单元格

                    }
                    sheet.addMergedRegion(new CellRangeAddress(firstRow, r-1, 0, 0)); // 合并品牌单元格


//                    rowRowNameFour = getRows(workbook, sheet, r++);
//
//                    getCell(style, rowRowNameFour, 0, "");
//                    getCell(style, rowRowNameFour, 1, "");
//                    getCell(style, rowRowNameFour, 2, "");
//                    getCell(style, rowRowNameFour, 3, "");
//                    getCell(columnTopStyle, rowRowNameFour, 4, "S");
//                    getCell(columnTopStyle, rowRowNameFour, 5, "X");
//                    getCell(columnTopStyle, rowRowNameFour, 6, "L");
                }


    // 第六行,空白行====================================================================================
                HSSFRow rowRowNameSix = getRows(workbook, sheet, r++);

                getCell(null, rowRowNameSix, 0, "");
                getCell(null, rowRowNameSix, 1, "");
                getCell(null, rowRowNameSix, 2, "");
                getCell(null, rowRowNameSix, 3, "");
                getCell(null, rowRowNameSix, 4, "");
                getCell(null, rowRowNameSix, 5, "");
                getCell(null, rowRowNameSix, 6, "");


            }









            // 循环 将列名放进去
//            for (int i = 0; i < columnNum; i++) {
//                HSSFCell  cellRowName = rowRowName.createCell((int)i);
//                cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); // 单元格类型
//
//                HSSFRichTextString text = new HSSFRichTextString(rowName[i]);  // 得到列的值
//                cellRowName.setCellValue(text); // 设置列的值
//                cellRowName.setCellStyle(columnTopStyle); // 样式
//            }

            // 将查询到的数据设置到对应的单元格中
//            for (int i = 0; i < dataList.size(); i++) {
//                Object[] obj = dataList.get(i);//遍历每个对象
//                HSSFRow row = sheet.createRow(i+2);//创建所需的行数
//                for (int j = 0; j < obj.length; j++) {
//                    HSSFCell  cell = null;   //设置单元格的数据类型
//                    if(j==0){
//                        // 第一列设置为序号
//                        cell = row.createCell(j,HSSFCell.CELL_TYPE_NUMERIC);
//                        cell.setCellValue(i+1);
//                    }else{
//                        cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING);
//                        if(!"".equals(obj[j]) && obj[j] != null){
//                            cell.setCellValue(obj[j].toString());                       //设置单元格的值
//                        }else{
//                            cell.setCellValue("  ");
//                        }
//                    }
//                    cell.setCellStyle(style); // 样式
//                }
//            }
            //  让列宽随着导出的列长自动适应
//            sheet.autoSizeColumn((short)0); //调整第一列宽度
//            sheet.autoSizeColumn((short)1); //调整第二列宽度
//            sheet.autoSizeColumn((short)2); //调整第三列宽度
//            sheet.autoSizeColumn((short)3); //调整第四列宽度
//            sheet.autoSizeColumn((short)4); //调整第五列宽度
//            sheet.autoSizeColumn((short)5); //调整第六列宽度

            if(workbook !=null){
                try
                {
                    // excel 表文件名
                    String fileName = title + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls";
                    String fileName11 = URLEncoder.encode(fileName,"UTF-8");
                    String headStr = "attachment; filename="" + fileName11 + """;
                    response.setContentType("APPLICATION/OCTET-STREAM");
                    response.setHeader("Content-Disposition", headStr);
                    OutputStream out = response.getOutputStream();
                    workbook.write(out);
                    out.flush();
                    out.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }

            }

        }catch(Exception e){
            e.printStackTrace();
        }

    }

    /**
    * @Description:    创建一行 i:在第 i 行创建行
    * @Author:         陈铨   2019/10/22 21:05
    */
    public HSSFRow getRows(HSSFWorkbook workbook, HSSFSheet sheet, int i) {
        HSSFRow rowRowName = sheet.createRow(i);  // 在第二行创建行
        HSSFCellStyle cells = workbook.createCellStyle();
        cells.setBottomBorderColor(HSSFColor.BLACK.index);
        rowRowName.setRowStyle(cells);
        return rowRowName;
    }

    /**
    * @Description:    创建一格
    * @Author:         陈铨   2019/10/22 21:04
    */
    public void getCell(HSSFCellStyle columnTopStyle, HSSFRow rowRowName, int i, String str) {
        HSSFCell cellRowNameOne = rowRowName.createCell(i);
        cellRowNameOne.setCellType(HSSFCell.CELL_TYPE_STRING); // 单元格类型
        HSSFRichTextString textOne = new HSSFRichTextString(str);  // 得到列的值
        cellRowNameOne.setCellValue(textOne); // 设置列的值
        cellRowNameOne.setCellStyle(columnTopStyle); // 样式
    }

    public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {

        // 设置字体
        HSSFFont font = workbook.createFont();
        //设置字体大小
        font.setFontHeightInPoints((short)11);
        //字体加粗
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        //设置字体名字
        font.setFontName("Courier New");
        //设置样式;
        HSSFCellStyle style = workbook.createCellStyle();
        //设置底边框;
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        //设置底边框颜色;
        style.setBottomBorderColor(HSSFColor.BLACK.index);
        //设置左边框;
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        //设置左边框颜色;
        style.setLeftBorderColor(HSSFColor.BLACK.index);
        //设置右边框;
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        //设置右边框颜色;
        style.setRightBorderColor(HSSFColor.BLACK.index);
        //设置顶边框;
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        //设置顶边框颜色;
        style.setTopBorderColor(HSSFColor.BLACK.index);
        //在样式用应用设置的字体;
        style.setFont(font);
        //设置自动换行;
        style.setWrapText(false);
        //设置水平对齐的样式为居中对齐;
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //设置垂直对齐的样式为居中对齐;
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 设置单元格颜色
        style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE
                .getIndex());
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        return style;

    }

    public HSSFCellStyle getStyle(HSSFWorkbook workbook) {
        // 设置字体
        HSSFFont font = workbook.createFont();
        //设置字体大小
        //font.setFontHeightInPoints((short)10);
        //字体加粗
        //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        //设置字体名字
        font.setFontName("Courier New");
        //设置样式;
        HSSFCellStyle style = workbook.createCellStyle();
        //设置底边框;
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        //设置底边框颜色;
        style.setBottomBorderColor(HSSFColor.BLACK.index);
        //设置左边框;
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        //设置左边框颜色;
        style.setLeftBorderColor(HSSFColor.BLACK.index);
        //设置右边框;
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        //设置右边框颜色;
        style.setRightBorderColor(HSSFColor.BLACK.index);
        //设置顶边框;
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        //设置顶边框颜色;
        style.setTopBorderColor(HSSFColor.BLACK.index);
        //在样式用应用设置的字体;
        style.setFont(font);
        //设置自动换行;
        style.setWrapText(false);
        //设置水平对齐的样式为居中对齐;
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //设置垂直对齐的样式为居中对齐;
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        return style;
    }


    public static File getFileFromImageUrl(String urlStr) throws Exception {
        //new一个URL对象
        URL url = new URL(urlStr);
        //打开链接
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        //设置请求方式为"GET"
        conn.setRequestMethod("GET");
        //超时响应时间为5秒
        conn.setConnectTimeout(5 * 1000);
        //通过输入流获取图片数据
        InputStream inStream = conn.getInputStream();
        //得到图片的二进制数据,以二进制封装得到数据,具有通用性
        byte[] data = readInputStream(inStream);
        //new一个文件对象用来保存图片,默认保存当前工程根目录
        File imageFile = new File("pic20170419.jpg");
        //创建输出流
        FileOutputStream outStream = new FileOutputStream(imageFile);
        //写入数据
        outStream.write(data);
        //关闭输出流
        outStream.close();
        return imageFile;
    }
    public static byte[] readInputStream(InputStream inStream) throws Exception{
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        //创建一个Buffer字符串
        byte[] buffer = new byte[1024];
        //每次读取的字符串长度,如果为-1,代表全部读取完毕
        int len = 0;
        //使用一个输入流从buffer里把数据读取出来
        while( (len=inStream.read(buffer)) != -1 ){
            //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
            outStream.write(buffer, 0, len);
        }
        //关闭输入流
        inStream.close();
        //把outStream里的数据写入内存
        return outStream.toByteArray();
    }
}