java poi 生成excel_java poi导出Excel 总结

这是一个用于将数据导出为Excel文件的Java工具类。它支持设置表格标题、属性列名、数据集合以及日期格式,并能处理各种数据类型,包括日期、图片等。在生成Excel时,还提供了样式定制,如背景颜色、边框样式、文字居中等。

1 packagecn.xujingyang.util;2

3 importjava.lang.reflect.Method ;4 importjava.text.SimpleDateFormat ;5 importjava.util.Collection ;6 importjava.util.Date ;7 importjava.util.Iterator ;8 importjava.util.Map ;9 importjava.util.regex.Matcher ;10 importjava.util.regex.Pattern ;11 importorg.apache.poi.hssf.usermodel.HSSFCell ;12 importorg.apache.poi.hssf.usermodel.HSSFCellStyle ;13 importorg.apache.poi.hssf.usermodel.HSSFClientAnchor ;14 importorg.apache.poi.hssf.usermodel.HSSFFont ;15 importorg.apache.poi.hssf.usermodel.HSSFPatriarch ;16 importorg.apache.poi.hssf.usermodel.HSSFRichTextString ;17 importorg.apache.poi.hssf.usermodel.HSSFRow ;18 importorg.apache.poi.hssf.usermodel.HSSFSheet ;19 importorg.apache.poi.hssf.usermodel.HSSFWorkbook ;20 importorg.apache.poi.hssf.util.HSSFColor ;21

22 public class ExcelUtil{23 /**

24 *25 *@paramtitle26 * 表格标题名27 *@paramheaders28 * 表格属性列名数组 (第一行标题)29 *@paramCol30 * 需要显示的表格属性列名数组 如果是javabean 必须和字段名字一直 如果为Map 必须为Map的key名字对应31 *@paramdataset32 * 需要显示的数据集合,集合泛型支持两种,1:符合javabean风格的类的对象 2:Map类型。此方法支持的33 * javabean属性的数据类型有基本数据类型及String,Date,byte[](图片数据)34 *@parampattern35 * 如果有时间数据,设定输出格式。默认为"yyy-MM-dd"36 */

37 public HSSFWorkbook exportExcel(String title, String[] headers,String[] Col,Collectiondataset, String pattern) {38 if(pattern == null || pattern.equals("")) pattern = "yyy-MM-dd";39 //声明一个工作薄

40 HSSFWorkbook workbook = newHSSFWorkbook();41 //生成一个表格

42 HSSFSheet sheet =workbook.createSheet(title);43 //设置表格默认列宽度为15个字节

44 sheet.setDefaultColumnWidth(15);45 //生成一个样式

46 HSSFCellStyle style =workbook.createCellStyle();47 //设置这些样式

48 style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);49 style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);50 style.setBorderBottom(HSSFCellStyle.BORDER_THIN);51 style.setBorderLeft(HSSFCellStyle.BORDER_THIN);52 style.setBorderRight(HSSFCellStyle.BORDER_THIN);53 style.setBorderTop(HSSFCellStyle.BORDER_THIN);54 style.setAlignment(HSSFCellStyle.ALIGN_CENTER);55 //生成一个字体

56 HSSFFont font =workbook.createFont();57 font.setColor(HSSFColor.VIOLET.index);58 font.setFontHeightInPoints((short) 12);59 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);60 //把字体应用到当前的样式

61 style.setFont(font);62 //生成并设置另一个样式

63 HSSFCellStyle style2 =workbook.createCellStyle();64 style2.setFillForegroundColor(HSSFColor.WHITE.index);65 style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);66 style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);67 style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);68 style2.setBorderRight(HSSFCellStyle.BORDER_THIN);69 style2.setBorderTop(HSSFCellStyle.BORDER_THIN);70 style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);71 style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);72 //生成另一个字体

73 HSSFFont font2 =workbook.createFont();74 font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);75 //把字体应用到当前的样式

76 style2.setFont(font2);77 //声明一个画图的顶级管理器

78 HSSFPatriarch patriarch =sheet.createDrawingPatriarch();79 //产生表格标题行

80 HSSFRow row = sheet.createRow(0);81 int Cell = 0;82 for (short i = 0; i < headers.length; i++) {83 HSSFCell cell =row.createCell(Cell);84 cell.setCellStyle(style);85 HSSFRichTextString text = newHSSFRichTextString(headers[i]);86 cell.setCellValue(text);87 Cell ++;88 }89 //遍历集合数据,产生数据行

90 Iterator it =dataset.iterator();91 int index = 0;92 while(it.hasNext()) {93 index++;94 row =sheet.createRow(index);95 T t =(T) it.next();96 String[] fields =Col;97 Cell = 0;98 for (short i = 0; i < fields.length; i++) {99 String fieldName =fields[i];100 HSSFCell cell =row.createCell(Cell);101 cell.setCellStyle(style2);102 try{103 Object value = "";104 Class tCls = null;105 Map map = null;106 if(t instanceofMap){107 map =(Map)t;108 value =map.get(fieldName);109 } else{110 String getMethodName = "get"

111 + fieldName.substring(0, 1).toUpperCase()112 + fieldName.substring(1);113 tCls =t.getClass();114 Method getMethod = tCls.getMethod(getMethodName,newClass[] {});115 value = getMethod.invoke(t, newObject[] {});116 }117 if(value == null ) value = "";118 //判断值的类型后进行强制类型转换

119 String textValue = null;120 if (value instanceofDate) {121 Date date =(Date) value;122 SimpleDateFormat sdf = newSimpleDateFormat(pattern);123 textValue =sdf.format(date);124 } else if (value instanceof byte[]) {125 //有图片时,设置行高为60px;

126 row.setHeightInPoints(60);127 //设置图片所在列宽度为80px,注意这里单位的一个换算

128 sheet.setColumnWidth(Cell, (short) (35.7 * 80));129 //sheet.autoSizeColumn(i);

130 byte[] bsValue = (byte[]) value;131 HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0,132 1023, 255, (short) 6, index, (short) 6, index);133 anchor.setAnchorType(2);134 patriarch.createPicture(anchor, workbook.addPicture(135 bsValue, HSSFWorkbook.PICTURE_TYPE_JPEG));136 } else{137 //其它数据类型都当作字符串简单处理

138 textValue =value.toString();139 }140 //如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成

141 if (textValue != null) {142 Pattern p = Pattern.compile("^//d+(//.//d+)?$");143 Matcher matcher =p.matcher(textValue);144 if(matcher.matches()) {145 //是数字当作double处理

146 cell.setCellValue(Double.parseDouble(textValue));147 } else{148 HSSFRichTextString richString = newHSSFRichTextString(149 textValue);150 HSSFFont font3 =workbook.createFont();151 font3.setColor(HSSFColor.BLUE.index);152 richString.applyFont(font3);153 cell.setCellValue(richString);154 }155 }156 Cell ++;157 } catch(Exception e) {158 e.printStackTrace();159 }160 }161 }162 returnworkbook;163 }164 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值