JAVA使用poi导出EXCEL

2 篇文章 0 订阅
2 篇文章 0 订阅

前端:如果你使用的是ajax类型的跨域导出,则无法出现弹框,所以直接选择window.location.href,具体操作看代码

后端:上面的地址为图片中的地址,如图

@GetMapping(value = "/export")
    public void exportSysTest(HttpServletRequest request, HttpServletResponse response) {
        try {
            List<Map<String,Object>> rows = sysTestService.selectRows();
            String [][] columnNames = {
                    {"序号","名字","手机号码","地址","性别","说明"},   {"ID","NAME","MOBILE","ADDRESS","SEX","EXPLAIN"}};
            String [] columnWidth ={"5","20","20","20","20","20"};
            String excelName = "测试管理";
            HSSFWorkbook wb = ExportExcelUtil.createSXSSFWorkbook(columnNames,columnWidth, rows, excelName);
            OutputStream ouputStream = response.getOutputStream();
            response.reset();
            response.setHeader("Content-disposition", "attachment;filename=book.xls");
            response.setContentType("application/octet-stream; charset=utf-8");
            wb.write(ouputStream);
            ouputStream.flush();
            ouputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
}

Excel工具类:

package com.brilliance.core.utils;

import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFSheet;

/**
 * 导出到excel表工具
 * wuzk
 * 2020-04-25
 */

public class ExportExcelUtil  {


    public ExportExcelUtil() {
        super();
    }

    public static HSSFWorkbook createSXSSFWorkbook(String[][] columnNames, String[] columnWidth, List<Map<String, Object>> rows, String excelName){
        HSSFWorkbook workbook = new HSSFWorkbook(); // 创建工作薄,相当于一个文件

        Sheet sheet = workbook.createSheet(); // 创建一个表
        //sheet.setDefaultColumnWidth((short) 3); // 设置默认列宽
        //sheet.setColumnWidth(0, 18 * 256); // 设置单位列列宽

        sheet.setMargin(XSSFSheet.TopMargin, 0.64); // 页边距(上)
        sheet.setMargin(XSSFSheet.BottomMargin, 0.64); // 页边距(下)
        sheet.setMargin(XSSFSheet.LeftMargin, 0.64); // 页边距(左)
        sheet.setMargin(XSSFSheet.RightMargin, 0.64); // 页边距(右)

        PrintSetup ps = sheet.getPrintSetup();
        ps.setPaperSize(PrintSetup.A4_PAPERSIZE); // 设置纸张大小
        ps.setLandscape(true); // 打印方向,true:横向,false:纵向(默认)

        // 标题样式
        CellStyle titleStyle = workbook.createCellStyle();
        titleStyle.setAlignment(HorizontalAlignment.CENTER); // 水平居中
        titleStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中
        // 标题字体
        Font titleFont = workbook.createFont();
        titleFont.setFontHeightInPoints((short) 12); // 字体大小
        titleFont.setFontName("宋体");
        titleStyle.setFont(titleFont);

        // 填报单位的样式
        CellStyle titleStyle_2 = workbook.createCellStyle();
        titleStyle_2.setAlignment(HorizontalAlignment.RIGHT); // 水平居右
        titleStyle_2.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中
        // 标题字体
        Font titleFont_2 = workbook.createFont();
        titleFont_2.setFontHeightInPoints((short) 11);
        titleFont_2.setFontName("宋体");
        titleStyle_2.setFont(titleFont_2);

        // 填报单位的样式
        CellStyle titleStyle_u = workbook.createCellStyle();
        titleStyle_u.setAlignment(HorizontalAlignment.LEFT); // 水平居左
        titleStyle_u.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中
        // 标题字体
        Font titleFont_u = workbook.createFont();
        titleFont_u.setUnderline(XSSFFont.U_SINGLE);
        titleFont_u.setFontHeightInPoints((short) 11);
        titleFont_u.setFontName("宋体");
        titleStyle_u.setFont(titleFont_u);

        // 表头样式
        CellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setAlignment(HorizontalAlignment.CENTER); // 水平居中
        headerStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中
        headerStyle.setBorderBottom(BorderStyle.THIN); // 下边框
        headerStyle.setBorderLeft(BorderStyle.THIN); // 左边框
        headerStyle.setBorderTop(BorderStyle.THIN); // 上边框
        headerStyle.setBorderRight(BorderStyle.THIN); // 右边框
        headerStyle.setWrapText(true); // 设置多行显示
        //这两句话是表示将表头单元格格式设置为文本型,在后面只要调用-----.setDataFormat(format.getFormat("@"))的方法就可以将数据设置为文本型。
        DataFormat format = workbook.createDataFormat();
        headerStyle.setDataFormat(format.getFormat("@"));
        // 表头字体
        Font headerFont = workbook.createFont();
        headerFont.setFontHeightInPoints((short) 9);
        headerFont.setFontName("宋体");
        headerStyle.setFont(headerFont);

        // 数据样式
        CellStyle dataStyle = workbook.createCellStyle();
        dataStyle.setAlignment(HorizontalAlignment.CENTER); // 水平居中
        dataStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中
        dataStyle.setBorderBottom(BorderStyle.THIN); // 下边框
        dataStyle.setBorderLeft(BorderStyle.THIN); // 左边框
        dataStyle.setBorderTop(BorderStyle.THIN); // 上边框
        dataStyle.setBorderRight(BorderStyle.THIN); // 右边框
        dataStyle.setDataFormat(format.getFormat("@"));      //将数据单元格格式设置为文本类型
        // 数据字体
        Font dataFont = workbook.createFont();
        dataFont.setFontHeightInPoints((short) 9);
        dataFont.setFontName("宋体");
        dataStyle.setFont(dataFont);

        // 尾部样式
        CellStyle footStyle = workbook.createCellStyle();
        footStyle.setAlignment(HorizontalAlignment.CENTER); // 水平居中
        footStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中
        // 尾部字体
        Font footFont = workbook.createFont();
        footFont.setFontHeightInPoints((short) 11);
        footFont.setFontName("宋体");
        footStyle.setFont(footFont);

        CellStyle commonStyle = workbook.createCellStyle();
        commonStyle.setBorderBottom(BorderStyle.THIN); // 下边框
        commonStyle.setBorderLeft(BorderStyle.THIN); // 左边框
        commonStyle.setBorderTop(BorderStyle.THIN); // 上边框
        commonStyle.setBorderRight(BorderStyle.THIN); // 右边框

        // 表格标题行
        Row row0 = sheet.createRow(0);
        row0.setHeight((short)(3 * 256));
        Cell cell0_0 = row0.createCell(0); // 创建单元格,参数说明的是第几个单元格
        cell0_0.setCellStyle(titleStyle);
        cell0_0.setCellValue(excelName); // 设置单元格 和里面的内容

        if(columnWidth.length>0){
            Integer clWidth;
            for(int i =0;i<columnWidth.length;i++){
                if(columnWidth[i]!=null &&!"".equals(columnWidth[i])){
                    clWidth = Integer.valueOf(columnWidth[i]);
                    sheet.setColumnWidth(i, clWidth*256);
                }
            }
        }



        Row row = null;
        Cell cell = null;
        for(int i = 1 ; i<=columnNames.length ; i++){
            row = sheet.createRow(i);
            row.setHeight((short)(2 * 256));
            for(int j = 0 ;j < columnNames[i-1].length;j++){
                cell = row.createCell(j);
                cell.setCellValue(columnNames[i-1][j]);
                cell.setCellStyle(headerStyle);

            }
        }

        sheet.getRow(columnNames.length).setZeroHeight(true);
        // 合并单元格
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, columnNames[0].length-1)); // 合并大标题行
        String[] names = columnNames[columnNames.length-1];

        // 数据填充,标题占一行,columnNames占columnNames.length行,之后才到数据行
        Object obj = null;

        for (int i = 0; i < rows.size(); i++) {
            Row dataRow = sheet.createRow(columnNames.length+1+ i);
            Map<String,Object> project = rows.get(i);
            for (int j = 0; j <names.length; j++) {
                Cell dataCell = dataRow.createCell(j);
                dataCell.setCellStyle(dataStyle);
                obj = project.get(names[j]);
                dataCell.setCellValue(obj==null?"":obj.toString());
            }
        }

        return workbook;
    }
}

页面原图:

点击导出:

Excel:

  • 4
    点赞
  • 66
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值