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, Listtitles, 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
5.springboot 调用导出方法
@Controller@RequestMapping("/demo")public class DemoController { @Autowired private DemoService demoService; /*根据查询条件导出数据到excel*/ @RequestMapping("/exportExcel") public ResponseEntityexportExcel(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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
不错!
[***.144.177.141]2024年03月29日 04时23分11秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Java笔记—语法
2019-04-28
Java笔记—数组
2019-04-28
Java笔记—继承
2019-04-28
MySQL的数据类型
2019-04-28
洛谷 P1886 滑动窗口 /【模板】单调队列
2019-04-28
洛谷 P3367 【模板】并查集
2019-04-28
【算法学习】高级数据结构2 种类并查集
2019-04-28
洛谷 P1525 关押罪犯【种类并查集】
2019-04-28
洛谷 P2024 [NOI2001]食物链【种类并查集】
2019-04-28
POJ 1703 Find them, Catch them【种类并查集】
2019-04-28
POJ 2492 A Bug‘s Life【种类并查集】
2019-04-28
POJ 2236 Wireless Network【并查集】
2019-04-28
LeetCode C++ 214. Shortest Palindrome【字符串】困难
2019-04-28
洛谷 P2580 于是他错误的点名开始了【字典树/Map】
2019-04-28
HDU 3336 Count the string【KMP的next数组性质】
2019-04-28
洛谷 P1196 [NOI2002]银河英雄传说【带权并查集】
2019-04-28
HDU 4825 Xor Sum【01字典树/贪心】(两数最大/最小异或和)
2019-04-28
洛谷 P4551 最长异或路径【01字典树/贪心】
2019-04-28
LeetCode C++ 506. Relative Ranks【Sort】简单
2019-04-28