在上一次的基础上加上了样式,以及中文列名
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
辅助工具类
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 staticList