POI导出数据到Excel
发布日期:2021-07-27 12:52:54 浏览次数:3 分类:技术文章

本文共 6579 字,大约阅读时间需要 21 分钟。

1.poi封装类

import org.apache.poi.hssf.usermodel.*;import org.apache.poi.ss.usermodel.*;import org.apache.poi.ss.util.CellRangeAddress;import org.apache.poi.ss.util.CellRangeAddressList;import org.apache.poi.ss.util.RegionUtil;import org.apache.poi.xssf.usermodel.*;import org.springframework.http.HttpHeaders;import org.springframework.http.MediaType;import javax.servlet.http.HttpServletResponse;import java.io.ByteArrayOutputStream;import java.io.UnsupportedEncodingException;import java.math.BigDecimal;import java.util.Date;import java.util.List;/** * excel下载公共类 */public class OutExcelUtil {    /**     * @param sheetName 工作表名称     * @param titles 数据表 标题     * @param values 数据     * @param wb     * @param style 单元格样式     * @return     */    public static HSSFWorkbook getHSSFWorkbook(String sheetName, List
titles, Object values[][], HSSFWorkbook wb, HSSFCellStyle style,int width){ //1 创建webbook对象,对应一个excel文件对象 if(wb == null) wb = new HSSFWorkbook(); //2 在wb中添加一个sheet,对应excel中的sheet HSSFSheet sheet = wb.createSheet(sheetName); //3 在sheet中添加标题行,第一行从0开始 HSSFRow row = sheet.createRow(0); //创建excel的单元格 HSSFCell cell = null; //循环title,创建标题 for(int i=0; i
titles, Object values[][], XSSFWorkbook wb, HSSFCellStyle style, int width){ //1 创建webbook对象,对应一个excel文件对象 if(wb == null) wb = new XSSFWorkbook(); //2 在wb中添加一个sheet,对应excel中的sheet XSSFSheet sheet = wb.createSheet(sheetName); //3 在sheet中添加标题行,第一行从0开始 XSSFRow row = sheet.createRow(0); //创建excel的单元格 XSSFCell cell = null; //循环title,创建标题 for(int i=0; i

2.集合处理类

import java.util.ArrayList;import java.util.List;public class ListUtil
{ /** * 将list集合分割成多个子集合 * @param list 要分割的集合 * @param subRow 子集合包含元素个数 */ public static
List
> getSubListByRow(List
list,int subRow){ int count = list.size(); int subNum = count % subRow == 0 ? count / subRow : count / subRow + 1; List
> resList = new ArrayList<>(subNum); for(int i=0;i
count) e = count; List
sonList = list.subList(s,e); resList.add(sonList); } return resList; } /** * 将list集合分割成subCount个子集合 * @param list 要分割的集合 * @param subCount 分割成subCount个子集合 10 3 */ public static
List
> getSubListByCount(List
list,int subCount){ int size = list.size(); int subRow = size / subCount; if(subRow >= 2){ if(size%subCount != 0){ subRow = subRow+1; } }else{ if (size > subCount) { subRow = subRow+1;; }else { subRow = size; } } return getSubListByRow(list,subRow); }}

3.定义接口

public interface DemoService {    /*导出excel*/    byte[] exportExcel(String ids, int cate);}

4.接口实现类

import com.rhx.framework.produce.common.util.ListUtil;import com.rhx.framework.produce.core.db.mapper.primary.center.SysToolsMapper;import com.rhx.framework.produce.core.service.demo.DemoService;import com.rhx.framework.produce.upload.excel.*;import org.apache.commons.lang3.StringUtils;import org.apache.poi.hssf.usermodel.*;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import org.springframework.transaction.interceptor.TransactionAspectSupport;import org.springframework.web.multipart.MultipartFile;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.util.*;@Service@Transactionalpublic class DemoServiceImpl implements DemoService {    @Autowired    private SysToolsMapper toolsMapper;    @Override    public byte[] exportExcel(String codes,int cate) {        //创建HSSFWorkbook对象        HSSFWorkbook wb = new HSSFWorkbook();        if(cate==1){            exportCadreInfo(codes,wb);//干部基本信息报表        }else if(cate==2){            //exportCadrePosition(codes,wb);//职位信息报表        }        return OutExcelUtil.toByteArray(wb);    }    public void exportCadreInfo(String idStr, HSSFWorkbook wb) {        //根据条件从数据库查询数据集合        String[] idArr = idStr.split(",");        List list = this.toolsMapper.selectAllTools();        //设置excel文件中sheet名        String sheetName = "基本信息报表";        //设置标题名        List
titles = new ArrayList
(){ { //分别添加名称,单元格长度 add(new String[]{"名称","12"}); add(new String[]{"链接","50"}); add(new String[]{"图标","15"}); } }; //创建sheet对象 HSSFSheet sheet = wb.createSheet(sheetName); //创建excel的单元格 HSSFCell cell = null; HSSFCellStyle titleStyle = OutExcelUtil.setTitleHSSCellStyle(wb);//标题单元格演示 HSSFCellStyle style = OutExcelUtil.setHSSCellStyle(wb);//普通样式 //HSSFCellStyle styleRegion = OutExcelUtil.setHSSCellStyleRegion(wb);//合并单元格样式 HSSFRow title = sheet.createRow(0); cell = title.createCell(0); title.setHeight((short)(15*30)); OutExcelUtil.setAddMergedRegion(sheet,cell,titleStyle,sheetName,0,0,0,titles.size()-1); //循环title,创建标题 HSSFRow row = sheet.createRow(1); for(int i=0; i
> subList = ListUtil.getSubListByCount(list,2); for(int s=0;s
sub = subList.get(s); Object[][] values = new Object[sub.size()][titles.size()]; for(int i=0; i
0){ startRow = startRow + subList.get(s-1).size(); } int dataRow = startRow; Thread thread = new Thread(new Runnable() { @Override public void run() { System.out.println("start"); OutExcelUtil.getMergedHSSFWorkbook(style,sheet,values,dataRow); System.out.println("end"); } },"thread-"+s); try { thread.start(); thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } }}

5.springboot 调用导出方法

@Controller@RequestMapping("/demo")public class DemoController {        @Autowired    private DemoService demoService;        /*根据查询条件导出数据到excel*/    @RequestMapping("/exportExcel")    public ResponseEntity
exportExcel(HttpServletRequest request, String ids){ //设置excel文件名 String fileName="信息报表"; //设置HttpHeaders,设置fileName编码,排除导出文档名称乱码问题 HttpHeaders headers = OutExcelUtil.setDownResponseHeader(fileName); byte[] value = null; try { //获取要导出的数据 value = this.demoService.exportExcel(ids,1); }catch (Exception e){ e.printStackTrace(); } return new ResponseEntity
(value,headers, HttpStatus.OK); }}

 

转载地址:https://blog.csdn.net/rhx_1989/article/details/88424905 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:SpringBoot JAVA导出数据到CSV文件
下一篇:JAVA读取FTP服务器文件内容

发表评论

最新留言

不错!
[***.144.177.141]2024年03月29日 04时23分11秒