- 浏览: 128891 次
- 性别:
- 来自: 安徽
文章分类
生成Excel效果图如下:
package com.kjlink.util; import java.io.FileOutputStream; import java.io.IOException; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; public class HSSFUtil { private HSSFWorkbook workbook = null; // Excel工作簿 private HSSFSheet sheet = null; private HSSFRow row = null; private HSSFCell cell = null; private HSSFCellStyle style = null; private HSSFFont font = null; private List<?> list = null; private String[] header = null; // 标题行 private String[][] body =null; // body内容 private String[] properties = null; // List集合中的对象属性名集合 private static HSSFUtil hssf = new HSSFUtil(); public static final String FILE_PATH = "D:\\领还记录.xls"; private HSSFUtil() { this.workbook = new HSSFWorkbook(); this.sheet = workbook.createSheet("sheet1"); this.style = workbook.createCellStyle(); this.font = workbook.createFont(); } public void exportExcel(String[] header,List<?> list, String[] properties) { this.header = header; this.list = list; this.properties = properties; this.createHead(); this.createBody(); this.writeXls(); System.out.println("生成Excel成功!"); } private void createHead() { // Excel格式头 if (header!=null&&header.length>0) { row = sheet.createRow(0); for (int i = 0; i<header.length; i++) { sheet.setColumnWidth(i, 5000); cell = row.createCell(i); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellValue(header[i]); this.defaultStyle(); this.defaultFont(); cell.setCellStyle(this.style); } } } private void createBody() { if (this.listToArray()!=null) { int lastnum = sheet.getLastRowNum(); for (int i = 0; i < body.length; i++) { row = sheet.createRow(++lastnum); for (int j = 0; j < body[i].length; j++) { cell = row.createCell(j); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellValue(body[i][j]); } } } } private void writeXls() { FileOutputStream out = null; try { out = new FileOutputStream(FILE_PATH); workbook.write(out); } catch (IOException e) { throw new RuntimeException(e); } finally { if (out!=null) { try { out.flush(); out.close(); } catch (IOException e) { throw new RuntimeException(e); } } } } private void defaultStyle () { style.setFillForegroundColor(HSSFColor.BLUE_GREY.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); } private void defaultFont () { font.setColor(HSSFColor.WHITE.index); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); style.setFont(font); } private String[][] listToArray() { if (list!=null&&!list.isEmpty()) { if (properties!=null&&properties.length>0) { body = new String[list.size()][properties.length]; // 二维数组Body for (int i = 0; i < body.length; i++) { for (int j = 0; j < body[i].length; j++) { Object obj = list.get(i); // 获取当前对象 Class<?> clzz = obj.getClass(); // 获取当前对象的Clas对象 try { String getterName = properties[j]; if (getterName.indexOf(".")!=-1) { // getter名称为【对象.属性】 String[] subPro = getterName.split("\\."); String subName = null; Object subObj = obj; // 子对象 for (int k = 0; k < subPro.length; k++) { subName = "get" + firstWordUpper(subPro[k]); Method method = subObj.getClass().getDeclaredMethod(subName); if (!method.isAccessible()) { subObj = method.invoke(subObj); if (subObj==null) break; if (k==subPro.length-1) { body[i][j] = subObj.toString(); } } } } else { // getter名称为【属性】 getterName = "get" + firstWordUpper(properties[j]); // getter方法名 Method method = clzz.getDeclaredMethod(getterName); // 获取指定方法名的Method对象 if(!method.isAccessible()) { // 表示该方法可以访问 修饰符不是private Object value = method.invoke(obj); // 调用obj对象的getter方法 if (value!=null) { if (value instanceof java.sql.Timestamp||value instanceof java.util.Date) { body[i][j] = timeToStr(value); } else { body[i][j] = value.toString(); } } } } } catch (Exception e) { throw new RuntimeException(e); } } } } } return body; } private String firstWordUpper(String word) { if (word!=null&&word.length()>1) { return word.substring(0,1).toUpperCase() + word.substring(1); } return word; } private String timeToStr(Object obj) { if (obj!=null) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(obj); } return null; } public static void main(String[] args) { List<Consum> list = new ArrayList<Consum>(); Consum consum1 = new Consum(); consum1.setAsset(new Asset("KJ-AA-0001")); consum1.setUser(new User("小兵")); consum1.setOperator(new User("信息中心")); consum1.setDate(new Date()); consum1.setRemark("去北京出差"); Consum consum2 = new Consum(); consum2.setAsset(new Asset("KJ-AA-0002")); consum2.setUser(new User("博海")); consum2.setOperator(new User("信息中心")); consum2.setDate(new Date()); consum2.setRemark("去上海出差"); list.add(consum1); list.add(consum2); String[] header = new String[] {"固定资产名称","领用人","操作人","领用日期","备注"}; String[] properties = new String[] {"asset.name", "user.name", "operator.name", "date", "remark"}; hssf.exportExcel(header, list, properties); } }
- POI_HSSF_-_Excel实例_110724.zip (3.4 KB)
- 下载次数: 22
发表评论
-
Apache commons简介
2012-08-07 14:43 865原文引自:http://www.blogjava.net ... -
Java Runtime
2011-12-12 13:39 1318关于java runtime.exec() 的讲解 ... -
正则表达式参考大全
2011-08-19 16:25 830^\d+$ //匹配非负整数(正整数 + 0) ... -
JDK环境变量的配置
2011-08-18 10:35 705原文引自:一路悠扬-51CTO技术博客JDK环境变量配置的步骤 ... -
iText生成PDF - 实例
2011-07-24 10:18 2231iText是著名的开放源 ... -
c3p0 apparent deadlock / creating emergency threads
2011-07-15 16:47 3528Code: 2005-09-06@06:29:12.38 ... -
高阶Java枚举类型enum使用详解
2011-06-01 10:23 1579java的Enum枚举类型终于在j2se1.5出现了。之前觉得 ... -
斯坦福大学公开课:编程方法学
2011-05-21 11:38 1003介绍了当代程序设计基本思想:面向对象,模块化,封装 ... -
采用CAS原理构建单点登录
2011-05-07 09:45 1063企业的信息化过程 ... -
HttpClient入门
2011-05-07 09:40 948HttpClient简介 HTTP 协议可能是现在 I ... -
分页代码
2011-05-07 09:29 838法一:使用hibernate框架分页。 接口如下: pu ... -
Comet:基于 HTTP 长连接的“服务器推”技术
2011-05-07 09:22 864周 婷 (mailto:zhouting@cn.ibm.com ... -
程序员必知15件事
2011-05-07 09:00 725作为程序员,要取得非凡成就需要记住的15件事。 1.走一条不 ... -
初学者如何开发出高质量J2EE系统
2011-05-06 12:22 687J2EE学习者越来越多,J2EE本身技术不断在发展,涌现出各种 ... -
J2EE学习中一些值得研究的开源项目
2011-05-06 12:21 634这篇文章写在我研究J2SE ... -
Java IO浅析
2011-05-06 12:19 410什么是流: 流是一个抽象的概念。当Java程序需要从数 ... -
字符串数组与字符串之间的互转(join/split)
2011-05-06 11:56 113781、Java 1-1、字符串数组=>字符串: ... -
JSON与JAVA的数据转换
2011-05-06 11:52 762JSON-lib这个Java类包用于把bean,map和XML ... -
[Java][JavaScript]字符串数组与字符串之间的互转(join/split)
2011-05-06 11:43 21、Java 1-1、字符 ...
相关推荐
POI3.5-HSSF-和XSSF-Excel操作快速入门
POI导出-excel表
poi的所有jar包,poi, poi-ooxml,poi-ooxml-schemas的各个版本jar poi的所有jar包,poi, poi-ooxml,poi-ooxml-schemas的各个版本jar
Apache POI - the Java API for Microsoft Documents。...XSSF Excel XLSX poi-ooxml XSLF PowerPoint PPTX poi-ooxml XWPF Word DOCX poi-ooxml OpenXML4J OOXML poi-ooxml-schemas, ooxml-schemas
赠送jar包:poi-scratchpad-4.1.2.jar; 赠送原API文档:poi-scratchpad-4.1.2-javadoc.jar; 赠送源代码:poi-scratchpad-4.1.2-sources.jar; 赠送Maven依赖信息文件:poi-scratchpad-4.1.2.pom; 包含翻译后的API...
内部包括poi-3.8-20120326.jar、poi-ooxml-3.8-20120326.jar、poi-ooxml-schemas-3.8-20120326.jar 测试可用版本,有些下载的不能用
poi--4.导入excelpoi--4.导入excelpoi--4.导入excelpoi--4.导入excelpoi--4.导入excelpoi--4.导入excel
赠送jar包:poi-ooxml-3.17.jar; 赠送原API文档:poi-ooxml-3.17-javadoc.jar; 赠送源代码:poi-ooxml-3.17-sources.jar; 赠送Maven依赖信息文件:poi-ooxml-3.17.pom; 包含翻译后的API文档:poi-ooxml-3.17-...
赠送jar包:poi-ooxml-3.16.jar; 赠送原API文档:poi-ooxml-3.16-javadoc.jar; 赠送源代码:poi-ooxml-3.16-sources.jar; 赠送Maven依赖信息文件:poi-ooxml-3.16.pom; 包含翻译后的API文档:poi-ooxml-3.16-...
赠送jar包:poi-ooxml-4.1.2.jar; 赠送原API文档:poi-ooxml-4.1.2-javadoc.jar; 赠送源代码:poi-ooxml-4.1.2-sources.jar; 赠送Maven依赖信息文件:poi-ooxml-4.1.2.pom; 包含翻译后的API文档:poi-ooxml-...
POI-HSSF和POI-XSSF和SXSSF(自POI 3.8 beta3起)-用于访问Microsoft Excel格式文件
赠送jar包:poi-ooxml-5.2.0.jar; 赠送原API文档:poi-ooxml-5.2.0-javadoc.jar; 赠送源代码:poi-ooxml-5.2.0-sources.jar; 赠送Maven依赖信息文件:poi-ooxml-5.2.0.pom; 包含翻译后的API文档:poi-ooxml-...
赠送jar包:poi-scratchpad-4.1.2.jar; 赠送原API文档:poi-scratchpad-4.1.2-javadoc.jar; 赠送源代码:poi-scratchpad-4.1.2-sources.jar; 赠送Maven依赖信息文件:poi-scratchpad-4.1.2.pom; 包含翻译后的API...
java中读取word文档需要引用apache的poi开源项目...为方便下载提供6个jar包,其中包含:poi-3.8.jar;poi-ooxml-3.8.jar;poi-ooxml-schemas-3.8.jar;poi-scratchpad-3.8.jar;xmlbeans-2.3.0.jar;dom4j-1.6.1.jar。
这里三个poi架包是java程序导出excel文件必须要用到的
poi-3.10.1上传组件jar包 poi-3.10.1.jar poi-ooxml-3.10.1.jar poi-ooxml-schemas-3.10.1.jar
poi-3.17 对work、excel操作,包含有: poi-3.17.jar poi-ooxml-3.17.jar poi-scratchpad-3.17.jar poi-ooxml-schemas-3.17.jar commons-codec-1.10.jar commons-collections4-4.1.jar commons-logging-1.2.jar ...
当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统、银行系统)。或者是:我们已经习惯用...本poi-4.1.2.jar、poi-ooxml-schemas-4.1.2.jar、等