博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 导出Excel 大数据量,自己经验总结!(二)
阅读量:4697 次
发布时间:2019-06-09

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

在上一次的基础上加上了样式,以及中文列名

1 package com.tommy.fundation.util;  2   3 import java.io.OutputStream;  4 import java.util.ArrayList;  5 import java.util.HashMap;  6 import java.util.Iterator;  7 import java.util.List;  8 import java.util.Map;  9 import java.util.Set; 10  11 import javax.servlet.http.HttpServletResponse; 12  13 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 14 import org.apache.poi.hssf.usermodel.HSSFSheet; 15 import org.apache.poi.hssf.usermodel.HSSFCellStyle; 16 import org.apache.poi.hssf.usermodel.HSSFFont; 17 import org.apache.poi.hssf.usermodel.HSSFRow; 18 import org.apache.poi.hssf.usermodel.HSSFCell; 19 import org.apache.poi.ss.usermodel.CellStyle; 20 import org.apache.poi.ss.usermodel.Font; 21 import org.apache.poi.ss.usermodel.IndexedColors; 22  23 public class ExportExcel
{ 24 public void doExcel(HttpServletResponse response, List
list, String fileName) throws Exception { 25 String[] columnNames = new String[0]; 26 doExcel(response, list , columnNames, fileName); 27 } 28 /** 29 * 导出多张excel表,解决xls格式行数65535的限制 30 * @author OnlyOne 31 * @param response 32 * @param list 需要处理的list数据集合 33 * @param columnNames 与实体属性一一对应的列名 34 * @param fileName 文件名 35 * @throws Exception 36 */ 37 public void doExcel(HttpServletResponse response, List
list, String[] columnNames, String fileName) throws Exception { 38 OutputStream os = response.getOutputStream();//获取输出流 39 response.reset(); 40 // 设置下载头部信息。Content-disposition为属性名。attachment表示以附件方式下载,如果要在页面中打开,则改为inline。filename为文件名 41 response.setContentType("application/vnd.ms-excel;charset=utf-8"); 42 response.setHeader("Content-disposition", "attachment;filename="+ new String((fileName + ".xls").getBytes(), "iso-8859-1")); 43 44 //将数据集合按65000个进行分割成多个子集合 45 Map
> sheetMap = doSheets(list); 46 // 创建excel工作簿 47 HSSFWorkbook wb = new HSSFWorkbook(); 48 //将map集合转换成set集合 49 Set
keys = sheetMap.keySet(); 50 51 // 创建两种单元格格式 52 HSSFCellStyle cs = wb.createCellStyle(); 53 HSSFCellStyle cs2 = wb.createCellStyle(); 54 55 // 创建两种字体 56 HSSFFont f = wb.createFont(); 57 HSSFFont f2 = wb.createFont(); 58 59 // 创建第一种字体样式(用于列名) 60 f.setFontHeightInPoints((short) 10); 61 f.setColor(IndexedColors.BLACK.getIndex()); 62 f.setBoldweight(Font.BOLDWEIGHT_BOLD); 63 64 // 创建第二种字体样式(用于值) 65 f2.setFontHeightInPoints((short) 10); 66 f2.setColor(IndexedColors.BLACK.getIndex()); 67 68 // 设置第一种单元格的样式(用于列名) 69 cs.setFont(f); 70 cs.setBorderLeft(CellStyle.BORDER_THIN); 71 cs.setBorderRight(CellStyle.BORDER_THIN); 72 cs.setBorderTop(CellStyle.BORDER_THIN); 73 cs.setBorderBottom(CellStyle.BORDER_THIN); 74 cs.setAlignment(CellStyle.ALIGN_CENTER); 75 76 // 设置第二种单元格的样式(用于值) 77 cs2.setFont(f2); 78 cs2.setBorderLeft(CellStyle.BORDER_THIN); 79 cs2.setBorderRight(CellStyle.BORDER_THIN); 80 cs2.setBorderTop(CellStyle.BORDER_THIN); 81 cs2.setBorderBottom(CellStyle.BORDER_THIN); 82 cs2.setAlignment(CellStyle.ALIGN_CENTER); 83 84 //对set集合进行遍历 85 for (Iterator
iterator = keys.iterator(); iterator.hasNext();) { 86 //获取遍历的下标 87 Integer sheetKey = iterator.next(); 88 // 创建一个sheet(页),并命名 89 HSSFSheet sheet = wb.createSheet("sheet"+sheetKey); 90 //获取一页的数据 91 List
sheetData = sheetMap.get(sheetKey); 92 //创建列名的行 93 HSSFRow firstRow = sheet.createRow(0); 94 //如果columnNames有值,则作为列名;如果没有,则实体的属性名作为列名 95 if(columnNames.length == 0){ 96 T en = (T) sheetData.get(0); 97 List
titleList = RelectUtil.
reflectEntityAttribute(en); 98 for(int m=0; m
dataList = RelectUtil.
reflectEntity(en, en.getClass());122 //创建数据行,以第二行开始,第一行作为列名使用123 HSSFRow row = sheet.createRow(i+1);124 for(int m=0; m
> doSheets(List
list){143 int count = list.size()/65000;144 int yu = list.size() % 65000;145 Map
> map = new HashMap
>();146 for (int i = 0; i <= count; i++) {147 List
subList = new ArrayList
();148 if (i == count) {149 subList = list.subList(i * 65000, 65000 * i + yu);150 } else {151 subList = list.subList(i * 65000, 65000 * (i + 1));152 }153 map.put(i, subList);154 }155 return map;156 }157 }

辅助工具类

1 package com.tommy.fundation.util; 2  3 import java.lang.reflect.Field; 4 import java.lang.reflect.InvocationTargetException; 5 import java.lang.reflect.Method; 6 import java.util.ArrayList; 7 import java.util.Date; 8 import java.util.List; 9 10 public class RelectUtil {11     public static 
List
reflectEntity(T model,Class
cals) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, NoSuchFieldException{12 List list = new ArrayList();13 Field[] field = model.getClass().getDeclaredFields(); //获取实体类的所有属性,返回Field数组 14 for(int j=0 ; j
List
reflectEntityAttribute(T model) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, NoSuchFieldException {83 List list = new ArrayList();84 Field[] field = model.getClass().getDeclaredFields(); //获取实体类的所有属性,返回Field数组85 for (int j = 0; j < field.length; j++) { //遍历所有属性86 String nam = field[j].getName(); //获取属性的名字87 list.add(nam);88 }89 return list;90 }91 }

 

转载于:https://www.cnblogs.com/onlymate/p/5242133.html

你可能感兴趣的文章
HDU-1085 Holding Bin-Laden Captive-母函数
查看>>
php提示undefined index的几种解决方法
查看>>
LRJ
查看>>
Struts2环境搭建
查看>>
Linux: Check version info
查看>>
stl学习之测试stlen,cout等的运行速度
查看>>
魔戒三曲,黑暗散去;人皇加冕,光明归来
查看>>
Error和Exception
查看>>
Python和Singleton (单件)模式[转载]
查看>>
httpclient设置proxy与proxyselector
查看>>
IT常用单词
查看>>
拓扑排序
查看>>
NYOJ--32--SEARCH--组合数
查看>>
JMS
查看>>
gulpfile 压缩模板
查看>>
【34.14%】【BZOJ 3110】 [Zjoi2013]K大数查询
查看>>
【 henuacm2016级暑期训练-动态规划专题 A 】Cards
查看>>
第五篇:白话tornado源码之褪去模板的外衣
查看>>
设备常用框架framework
查看>>
bootstrap模态框和select2合用时input无法获取焦点(转)
查看>>