1. 添加maven依赖
<poi.version>3.11</poi.version>

<!-- Excel解析工具类  -->

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>${poi.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>${poi.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>${poi.version}</version>
</dependency>

2.POI导出的步骤:

          1 创建工作簿
          2 创建工作表
          3 创建行
          4 创建单元格
          5 设置内容
          6 设置内容格式
          7   下载 
  1. demo1 普通导入
    注:下载上传都是同步请求 AJAX无法页面下载
    解决方案:使用页面同步请求
       // $.ajax({
       //     url:"/user/exportXls",
       //     type:"GET"
       // })
  //正确请求路径
  location.href="/user/exportXls";
    @Resource
    private UserService userService;
    
    @GetMapping("exportXls")
    public void contextLoads(HttpServletResponse response) throws Exception{

        //准备要报表的数据
        List<User> all = userService.findAll();
        // 1 创建工作簿  xls  HSSFWorkbook    xlsx XSSFWorkbook
        Workbook wb = new XSSFWorkbook();
        // 2 创建工作表
        Sheet sheet = wb.createSheet();
        //定义公共变量
        //行号 和 列号
        int rowNo=0,cellNo=0;
        // 行对象通用变量
        Row nRow = null;
        // 单元格对象通用变量
        Cell nCell = null;
        /****************内容打印****************/
        for (User user:all){
            //3 创建行 行号需要+1
            nRow = sheet.createRow(rowNo++);
            //4 创建单元格
            nCell = nRow.createCell(cellNo++);
            //5 设置内容 uid
            nCell.setCellValue(user.getUid());
            //6 设置内容格式
            nCell.setCellStyle(contentStyle(wb));
            //Telephone
            nCell = nRow.createCell(cellNo++);
            nCell.setCellValue(user.getTelephone());
            nCell.setCellStyle(contentStyle(wb));
            //Mail
            nCell = nRow.createCell(cellNo++);
            nCell.setCellValue(user.getMail());
            nCell.setCellStyle(contentStyle(wb));
            //Password
            nCell = nRow.createCell(cellNo++);
            nCell.setCellValue(user.getPassword());
            nCell.setCellStyle(contentStyle(wb));
            //Type
            nCell = nRow.createCell(cellNo++);
            nCell.setCellValue(user.getType());
            nCell.setCellStyle(contentStyle(wb));
            //cellNo归0
            cellNo = 0;
        }
        // 7 下载
        DownloadUtil downloadUtil = new DownloadUtil();
        //ByteArrayOutputStream byteArrayOutputStream -- 输出流
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        // 将wb写进流
        wb.write(byteArrayOutputStream);
        // HttpServletResponse response -- response
        // String returnName -- 下载的名字
        downloadUtil.download(byteArrayOutputStream,response,"运单表.xlsx");

    }

    public CellStyle contentStyle(Workbook wb){
        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setBorderTop(CellStyle.BORDER_THIN);
        cellStyle.setBorderRight(CellStyle.BORDER_THIN);
        cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
        cellStyle.setBorderLeft(CellStyle.BORDER_THIN);

        Font font = wb.createFont();
        font.setFontHeight((short)200);

        cellStyle.setFont(font);
        return cellStyle;
    }

下载结果

Excel格式报表生成(POI技术)同步下载问题解决
4. demo2 加入标题 +单元格合并+字体设置

package com.czxy.bos.controller.print;

import com.czxy.bos.domain.take_delivery.WayBill;
import com.czxy.bos.service.take_delivery.WayBillService;
import com.czxy.bos.util.DownloadUtil;
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.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/report")
public class ReportController {


    @Autowired
    private WayBillService wayBillService;

    @GetMapping("exportXls")
    public void exportXls(HttpServletResponse response) throws Exception{
        /**
         * 查找数据之后,下面只需要将内容写进xls中,然后下载
         */
        List<WayBill> wayBillList = wayBillService.findAllWayBill();

        //1 创建工作簿  xls  HSSFWorkbook xlsx XSSFWorkbook
        Workbook wb = new XSSFWorkbook();
        //2 创建工作表
        Sheet sheet = wb.createSheet();

        // 设置列宽---1/256 一个字符的宽度
        sheet.setColumnWidth(0,15*256);
        sheet.setColumnWidth(1,15*256);
        sheet.setColumnWidth(2,15*256);

        sheet.setColumnWidth(3,25*256);
        sheet.setColumnWidth(4,25*256);
        sheet.setColumnWidth(5,25*256);
        sheet.setColumnWidth(6,25*256);
        sheet.setColumnWidth(7,25*256);
        sheet.setColumnWidth(8,25*256);
        /**
         * 定义公共变量
         */
        int rowNo=0,cellNo=0;//行号  和  列号
        Row nRow = null;// 行对象通用变量
        Cell nCell = null;// 单元格对象通用变量

        /****************大标题打印****************/
        //3 创建行
        nRow = sheet.createRow(rowNo);
        //4 创建单元格
        nCell = nRow.createCell(cellNo);
        //5 设置内容
        nCell.setCellValue("bos项目运单表统计"+new Date().toLocaleString());
        //6 设置内容格式
        // 合并单元格  //参数1:起始行 参数2:终止行 参数3:起始列 参数4:终止列
        sheet.addMergedRegion(new CellRangeAddress(0, 0, (short) 0, (short) 9));

        // 垂直居中  +   水平居中  +  加粗
        CellStyle bigTitleCellStyle = bigTitleStyle(wb);
        nCell.setCellStyle(bigTitleCellStyle);

        /****************小标题打印****************/
        String[] titles={"编号id","运单编号","订单编号","寄件人姓名","寄件人电话","寄件人地址","收件人姓名","收件人电话","收件人地址"};

        // 进入小标题打印的时候,行号变化吗?rowNo=0
        rowNo++;
        // 进入小标题打印的时候,列号需要变化吗?cellNo = 0;


        //3 创建行
        nRow = sheet.createRow(rowNo);

        for (String title:titles){
            //4 创建单元格
            nCell = nRow.createCell(cellNo++);// 先创建单元格,然后在新增
            //5 设置内容
            nCell.setCellValue(title);
            //6 设置内容格式
            nCell.setCellStyle(titleStyle(wb));
        }



         /****************内容打印****************/
         // 单元格需要变化吗
        rowNo++;
        cellNo=0;

        for(WayBill wayBill:wayBillList){
            //3 创建行
            nRow = sheet.createRow(rowNo++);
            //4 创建单元格
            //id 对象遍历 需要多少写多少
            nCell = nRow.createCell(cellNo++);
            nCell.setCellValue(wayBill.getId());
            nCell.setCellStyle(contentStyle(wb));
            //wayBillNum
            nCell = nRow.createCell(cellNo++);
            nCell.setCellValue(wayBill.getWayBillNum());
            nCell.setCellStyle(contentStyle(wb));
            //cellNo归0
            cellNo = 0;
        }
        /****************下载****************/
        DownloadUtil downloadUtil = new DownloadUtil();
        //ByteArrayOutputStream byteArrayOutputStream -- 输出流
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        // 将wb写进流
        wb.write(byteArrayOutputStream);
        // HttpServletResponse response -- response
        // String returnName -- 下载的名字
        downloadUtil.download(byteArrayOutputStream,response,"运单表.xlsx");
        System.out.println("okokokok....");
    }

    /**
     * 垂直居中  +   水平居中  +  加粗
     * @param wb
     * @return
     */
    public CellStyle bigTitleStyle(Workbook wb){
        // 创建格式
        CellStyle cellStyle = wb.createCellStyle();
        // 水平对齐方式
        cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
        // 垂直居中
        cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        // 设置字体
        Font font = wb.createFont();
        // 是数值的1/20 大小
        font.setFontHeight((short) 480);
        font.setBold(true);
        font.setColor(Font.COLOR_RED);
        cellStyle.setFont(font);
        
        return cellStyle;
    }

    public CellStyle titleStyle(Workbook wb){
        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setBorderTop(CellStyle.BORDER_THIN);
        cellStyle.setBorderRight(CellStyle.BORDER_THIN);
        cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
        cellStyle.setBorderLeft(CellStyle.BORDER_THIN);

        Font font = wb.createFont();
        font.setFontHeight((short)300);

        cellStyle.setFont(font);
        return cellStyle;
    }


    public CellStyle contentStyle(Workbook wb){
        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setBorderTop(CellStyle.BORDER_THIN);
        cellStyle.setBorderRight(CellStyle.BORDER_THIN);
        cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
        cellStyle.setBorderLeft(CellStyle.BORDER_THIN);

        Font font = wb.createFont();
        font.setFontHeight((short)200);

        cellStyle.setFont(font);
        return cellStyle;
    }

}

祝你幸福
送你一首歌《城南花已开》三亩地
附图:插画师LOST7_的《睡了吗?摘颗星星给你》Excel格式报表生成(POI技术)同步下载问题解决

相关文章:

  • 2021-12-12
  • 2022-12-23
  • 2022-01-11
  • 2021-07-31
  • 2022-12-23
  • 2021-10-19
  • 2021-11-18
  • 2022-02-11
猜你喜欢
  • 2021-07-21
  • 2021-08-24
  • 2021-07-21
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
相关资源
相似解决方案