关于SpringBoot将数据导出到excel表格

POI操作Excel
poi简介
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。

1、HSSF:HSSF 是Horrible SpreadSheet Format的缩写,通过HSSF,你可以用纯Java代码来读取、写入、修改Excel文件。

2、POI EXCEL文档结构类

HSSFWorkbook excel文档对象

HSSFSheet excel的sheet

HSSFRow excel的行

HSSFCell excel的单元格

HSSFFont excel字体

HSSFName 名称

HSSFDataFormat 日期格式

HSSFHeader sheet头

HSSFFooter sheet尾

HSSFCellStyle cell样式

HSSFDateUtil 日期

HSSFPrintSetup 打印

HSSFErrorConstants 错误信息表

3、导出Excel常用的方法:

HSSFWorkbook wb = new HSSFWorkbook(); //创建Excel工作簿对象

HSSFSheet sheet = wb.createSheet("new sheet"); //创建Excel工作表对象

HSSFRow row = sheet.createRow((short)0); //创建Excel工作表的行

cellStyle = wb.createCellStyle(); //创建单元格样式

row.createCell((short)0).setCellStyle(cellStyle); //创建Excel工作表指定行的单元格

row.createCell((short)0).setCellValue(1); //设置Excel工作表的值

例如:

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.binshi.common.utils.common.R;
import com.binshi.common.utils.page.PageUtils;
import com.binshi.common.utils.page.Query;
import com.binshi.common.utils.tools.StringUtil;
import com.binshi.store.modules.car.dao.StorePersonEarningsDao;
import com.binshi.store.modules.car.entity.*;
import com.binshi.store.modules.car.service.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

 public R export(Long storeId,HttpServletResponse response) throws IOException {
        //获取表头数据
        String[] header = {"序号","姓名","电话","车牌","车辆详情"};
        //获取数据内容
        List storeUserEntityList = this.selectList(new EntityWrapper().eq("store_id",storeId));

        //声明一个工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        //设置表头样式
        XSSFCellStyle headStyle = setHeadStyle(workbook);
        // 给单元格内容设置另一个样式
        XSSFCellStyle cellStyle = setCellStyle(workbook);

        //生成一个表格,设置表格名称为"门店会员表"
        XSSFSheet sheet = workbook.createSheet("门店会员表");
        //设置表格列宽度为10个字节
        sheet.setDefaultColumnWidth(45);
        //创建第一行表头
        XSSFRow headrow = sheet.createRow(0);
        sheet.setVerticallyCenter(true);
        //遍历添加表头
        for (int i = 0; i < header.length; i++) {
            //创建一个单元格
            XSSFCell cell = headrow.createCell(i);
            //创建一个内容对象
            XSSFRichTextString text = new XSSFRichTextString(header[i]);
            //将内容对象的文字内容写入到单元格中
            cell.setCellStyle(headStyle);
            cell.setCellValue(text);
        }


        //模拟遍历数据内容,把内容加入表格
        for (int i = 0; i < storeUserEntityList.size(); i++) {
            XSSFRow row1 = sheet.createRow(i+1);
            for(int j=0;j

导出后的excel表格如下图所示:


a.jpg

自此,使用POI将数据导出到excel表格完成。

ps:关于导出数据会遇到的问题:poi和poi-ooxml版本不一致可能会出现错误。
把版本该成一致就好啦

     
        
            org.apache.poi
            poi
            3.10
        

        
            org.apache.poi
            poi-ooxml
            3.10
        

你可能感兴趣的:(关于SpringBoot将数据导出到excel表格)