java导出excel的路径_java中使用poi导出excel表格数据并且可以手动修改导出路径

1 packagecom.yjd.admin.util;2

3 importjava.io.IOException;4 importjava.io.OutputStream;5 importjava.lang.reflect.Field;6 importjava.lang.reflect.InvocationTargetException;7 importjava.lang.reflect.Method;8 importjava.text.SimpleDateFormat;9 importjava.util.Collection;10 importjava.util.Date;11 importjava.util.Iterator;12 importjava.util.regex.Matcher;13 importjava.util.regex.Pattern;14

15

16 importorg.apache.poi.hssf.usermodel.HSSFCell;17 importorg.apache.poi.hssf.usermodel.HSSFCellStyle;18 importorg.apache.poi.hssf.usermodel.HSSFClientAnchor;19 importorg.apache.poi.hssf.usermodel.HSSFComment;20 importorg.apache.poi.hssf.usermodel.HSSFFont;21 importorg.apache.poi.hssf.usermodel.HSSFPatriarch;22 importorg.apache.poi.hssf.usermodel.HSSFRichTextString;23 importorg.apache.poi.hssf.usermodel.HSSFRow;24 importorg.apache.poi.hssf.usermodel.HSSFSheet;25 importorg.apache.poi.hssf.usermodel.HSSFWorkbook;26 importorg.apache.poi.hssf.util.HSSFColor;27

28 /**

29 * 利用开源组件POI3.0.2动态导出EXCEL文档 转载时请保留以下信息,注明出处!30 *31 *@authorleno32 *@versionv1.033 *@param34 * 应用泛型,代表任意一个符合javabean风格的类35 * 注意这里为了简单起见,boolean型的属性xxx的get器方式为getXxx(),而不是isXxx()36 * byte[]表jpg格式的图片数据37 */

38 public class ExportExcel{39 public void exportExcel(Collectiondataset, OutputStream out) {40 exportExcel("测试POI导出EXCEL文档", null, dataset, out, "yyyy-MM-dd");41 }42

43 public void exportExcel(String[] headers, Collectiondataset,44 OutputStream out) {45 exportExcel("测试POI导出EXCEL文档", headers, dataset, out, "yyyy-MM-dd");46 }47

48 public void exportExcel(String[] headers, Collectiondataset,49 OutputStream out, String pattern) {50 exportExcel("测试POI导出EXCEL文档", headers, dataset, out, pattern);51 }52

53 /**

54 * 这是一个通用的方法,利用了JAVA的反射机制,可以将放置在JAVA集合中并且符号一定条件的数据以EXCEL 的形式输出到指定IO设备上55 *56 *@paramtitle57 * 表格标题名58 *@paramheaders59 * 表格属性列名数组60 *@paramdataset61 * 需要显示的数据集合,集合中一定要放置符合javabean风格的类的对象。此方法支持的62 * javabean属性的数据类型有基本数据类型及String,Date,byte[](图片数据)63 *@paramout64 * 与输出设备关联的流对象,可以将EXCEL文档导出到本地文件或者网络中65 *@parampattern66 * 如果有时间数据,设定输出格式。默认为"yyy-MM-dd"67 */

68 @SuppressWarnings("unchecked")69 public voidexportExcel(String title, String[] headers,70 Collectiondataset, OutputStream out, String pattern) {71 //声明一个工作薄

72 HSSFWorkbook workbook = newHSSFWorkbook();73 //生成一个表格

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

76 sheet.setDefaultColumnWidth((short) 15);77 //生成一个样式

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

80 style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);81 style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);82 style.setBorderBottom(HSSFCellStyle.BORDER_THIN);83 style.setBorderLeft(HSSFCellStyle.BORDER_THIN);84 style.setBorderRight(HSSFCellStyle.BORDER_THIN);85 style.setBorderTop(HSSFCellStyle.BORDER_THIN);86 style.setAlignment(HSSFCellStyle.ALIGN_CENTER);87 //生成一个字体

88 HSSFFont font =workbook.createFont();89 font.setColor(HSSFColor.VIOLET.index);90 font.setFontHeightInPoints((short) 12);91 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);92 //把字体应用到当前的样式

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

95 HSSFCellStyle style2 =workbook.createCellStyle();96 style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);97 style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);98 style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);99 style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);100 style2.setBorderRight(HSSFCellStyle.BORDER_THIN);101 style2.setBorderTop(HSSFCellStyle.BORDER_THIN);102 style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);103 style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);104 //生成另一个字体

105 HSSFFont font2 =workbook.createFont();106 font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);107 //把字体应用到当前的样式

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

110 HSSFPatriarch patriarch =sheet.createDrawingPatriarch();111 //定义注释的大小和位置,详见文档

112 HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0,113 0, 0, 0, (short) 4, 2, (short) 6, 5));114 //设置注释内容

115 comment.setString(new HSSFRichTextString("可以在POI中添加注释!"));116 //设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容.

117 comment.setAuthor("leno");118 //产生表格标题行

119 HSSFRow row = sheet.createRow(0);120 for (short i = 0; i < headers.length; i++) {121 HSSFCell cell =row.createCell(i);122 cell.setCellStyle(style);123 HSSFRichTextString text = newHSSFRichTextString(headers[i]);124 cell.setCellValue(text);125 }126 //遍历集合数据,产生数据行

127 Iterator it =dataset.iterator();128 int index = 0;129 while(it.hasNext()) {130 index++;131 row =sheet.createRow(index);132 T t =(T) it.next();133 //利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值

134 Field[] fields =t.getClass().getDeclaredFields();135 for (short i = 0; i < fields.length; i++) {136 HSSFCell cell =row.createCell(i);137 cell.setCellStyle(style2);138 Field field =fields[i];139 String fieldName =field.getName();140 String getMethodName = "get"

141 + fieldName.substring(0, 1).toUpperCase()142 + fieldName.substring(1);143 try{144 Class tCls =t.getClass();145 Method getMethod =tCls.getMethod(getMethodName,146 newClass[] {});147 Object value = getMethod.invoke(t, newObject[] {});148 //判断值的类型后进行强制类型转换

149 String textValue = null;150 //if (value instanceof Integer) {151 //int intValue = (Integer) value;152 //cell.setCellValue(intValue);153 //} else if (value instanceof Float) {154 //float fValue = (Float) value;155 //textValue = new HSSFRichTextString(156 //String.valueOf(fValue));157 //cell.setCellValue(textValue);158 //} else if (value instanceof Double) {159 //double dValue = (Double) value;160 //textValue = new HSSFRichTextString(161 //String.valueOf(dValue));162 //cell.setCellValue(textValue);163 //} else if (value instanceof Long) {164 //long longValue = (Long) value;165 //cell.setCellValue(longValue);166 //}

167 if (value instanceofBoolean) {168 boolean bValue =(Boolean) value;169 textValue = "男";170 if (!bValue) {171 textValue = "女";172 }173 } else if (value instanceofDate) {174 Date date =(Date) value;175 SimpleDateFormat sdf = newSimpleDateFormat(pattern);176 textValue =sdf.format(date);177 } else if (value instanceof byte[]) {178 //有图片时,设置行高为60px;

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

181 sheet.setColumnWidth(i, (short) (35.7 * 80));182 //sheet.autoSizeColumn(i);

183 byte[] bsValue = (byte[]) value;184 HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0,185 1023, 255, (short) 6, index, (short) 6, index);186 anchor.setAnchorType(2);187 patriarch.createPicture(anchor, workbook.addPicture(188 bsValue, HSSFWorkbook.PICTURE_TYPE_JPEG));189 } else{190 //其它数据类型都当作字符串简单处理

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

194 if (textValue != null) {195 Pattern p = Pattern.compile("^//d+(//.//d+)?$");196 Matcher matcher =p.matcher(textValue);197 if(matcher.matches()) {198 //是数字当作double处理

199 cell.setCellValue(Double.parseDouble(textValue));200 } else{201 HSSFRichTextString richString = newHSSFRichTextString(202 textValue);203 HSSFFont font3 =workbook.createFont();204 font3.setColor(HSSFColor.BLUE.index);205 richString.applyFont(font3);206 cell.setCellValue(richString);207 }208 }209 } catch(SecurityException e) {210 e.printStackTrace();211 } catch(NoSuchMethodException e) {212 e.printStackTrace();213 } catch(IllegalArgumentException e) {214 e.printStackTrace();215 } catch(IllegalAccessException e) {216 e.printStackTrace();217 } catch(InvocationTargetException e) {218 e.printStackTrace();219 } finally{220 //清理资源

221 }222 }223 }224 try{225 workbook.write(out);226 } catch(IOException e) {227 e.printStackTrace();228 }229 }230

231 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值