浏览 9651 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2013-05-01
使用Aspose.cells for java 完成对复杂EXCEL的导出功能 使用Aspose.cells导出Excel非常方便,而且可以自定义EXCEL样式. 官方下载地址:http://www.aspose.com/java/excel-component.aspx 我使用的是最新版本的7.4.3 需要jar包如下 aspose-cells-7.4.3.jar bcprov-jdk16-146.jar dom4j-1.6.1.jar stax2-api-3.0.2.jar woodstox-core-asl-4.1.1.jar 第一步需要自定义模板 定义模板 excel样式完全自定义,比用程序直接生成excel要方便. 在固定的位置使用 &=Person.name(bean)占位即可 导出后 1定义实体类,用来填充模板 package com.epkj.bean; import java.text.SimpleDateFormat; import java.util.Date; /** * 该实体类用于填充excel模板 */ public class Person { private String name; private Date birthday; private int age; private double salary; private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); public Person() {} public Person(String name, Date birthday, int age, double salary) { super(); this.name = name; this.birthday = birthday; this.age = age; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } /** * 值得注意的是 * 由于不知道怎么在Aspose框架上处理导出时间格式的问题 * 只好在这里处理了,算是一个变通的解决方案吧 */ public String getBirthday() { return format.format(birthday); } public void setBirthday(Date birthday) { this.birthday = birthday; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } } package com.epkj.bean; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; public class InitData { public static List<Person> getPersons() { List<Person> persons = new ArrayList<Person>(); persons.add(new Person("张三", new Date(), 25, 4022.34)); persons.add(new Person("李四", new Date(), 56, 3580.3)); persons.add(new Person("王宇", new Date(), 45, 5454.34)); persons.add(new Person("小强", new Date(), 57, 5876.24)); persons.add(new Person("微微", new Date(), 54, 4022.68)); persons.add(new Person("丽丽", new Date(), 12, 5878.12)); persons.add(new Person("张娜", new Date(), 37, 5454.00)); persons.add(new Person("王华", new Date(), 60, 2221)); persons.add(new Person("阿超", new Date(), 55, 4587)); persons.add(new Person("黑子", new Date(), 22, 9788)); persons.add(new Person("小王", new Date(), 37, 2212.24)); persons.add(new Person("剌剌", new Date(), 27, 8785.24)); persons.add(new Person("荷花", new Date(), 18, 5454.34)); return persons; } public static Map<String, Object> getHashMap() { Map<String, Object> data = new HashMap<String, Object>(); data.put("name", "张三"); data.put("age", 35); data.put("birthday", "1985-01-03"); data.put("address", "辽宁沈阳"); data.put("phone", "13988888888"); return data; } } package com.epkj.cells; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.aspose.cells.Workbook; /** * 自定义模板导出基类 */ public abstract class DemoBase { protected abstract Workbook createReport(HttpServletRequest request) throws Exception; /** * 设置HTTP响应头 * @param response * @param fileName * @param formatSuffix */ protected void setResponseHeader(HttpServletResponse response, String fileName, String formatSuffix) { String contentType = "application/vnd.ms-excel"; formatSuffix = formatSuffix.toLowerCase(); if(formatSuffix.endsWith(".xlsx") || formatSuffix.endsWith(".xlsb") || formatSuffix.endsWith(".xlsm") || formatSuffix.endsWith(".xltm") || formatSuffix.endsWith(".xltx")) { contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; } response.setContentType(contentType); response.addHeader("content-disposition", "inline;filename=" + fileName); } /** * 向客户端返回excel * @param request * @param response * @param wb * @throws Exception */ protected void sendReport(HttpServletRequest request, HttpServletResponse response, Workbook wb) throws Exception { wb.save(response.getOutputStream(), wb.getFileFormat()); } } package com.epkj.cells; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.aspose.cells.Workbook; import com.aspose.cells.WorkbookDesigner; import com.epkj.bean.InitData; /** * 根据自定义的Excel模板进行导出 */ @Controller @RequestMapping("/SmartMarkerController.do") public class SmartMarkerController extends DemoBase { //存放Excel模板的位置 private static final String TEMPLATE_FILE_PATH_PART = "/Designer/SmartMarkerDesigner.xls"; /** * 对客户端开发的方法 */ @RequestMapping(params = "method=getReport") public Object getReport(HttpServletRequest request, HttpServletResponse response) { try { //创建工作薄 Workbook wb = createReport(request); //设置输出响应头 setResponseHeader(response, "SmartMarker.xls", ".xlsx"); //向客户端输出 sendReport(request, response, wb); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 向自定义模板中填充数据 */ protected Workbook createReport(HttpServletRequest request) throws Exception { ServletContext sc = request.getSession().getServletContext(); String template_file_path = sc.getRealPath(TEMPLATE_FILE_PATH_PART); //创建工作薄加载模板(SmartMarkerDesigner.xls) Workbook wb = new Workbook(template_file_path); createSmart(wb); return wb; } private static void createSmart(Workbook wb) throws Exception { WorkbookDesigner designer = new WorkbookDesigner(); designer.setWorkbook(wb); designer.setDataSource("Person", InitData.getPersons()); //这个框架默认不支持HashMap(查了好多资料都没找到直接支持的方法) //所以我实现了ICellsDataTable接口 使其可以支持HashMap //designer.setDataSource("Map", new HashMapDataTable(InitData.getHashMap())); designer.process(true); } } 实现ICellsDataTable接口,支持HashMap package com.epkj.cells; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import com.aspose.cells.ICellsDataTable; /** * 实现ICellsDataTable接口 * 以便支持对HashMap的支持 */ public class HashMapDataTable implements ICellsDataTable { //数据集合 private List<Map<String, Object>> dataList = null; //索引 private int index; //存放dataList当中Map<String, Object>的key private String[] columns = null; public HashMapDataTable(Map<String, Object> data) { if(this.dataList == null) { this.dataList = new ArrayList<Map<String,Object>>(); } dataList.add(data); } public HashMapDataTable(List<Map<String, Object>> data) { this.dataList = data; } /** * 初始化方法 */ @Override public void beforeFirst() { index = -1; columns = this.getColumns(); } /** * WorkbookDesigner自动调用 * 会将this.getColumns()方法所返回的列 按照顺序调用改方法 */ @Override public Object get(int columnIndex) { if(index < 0 || index >= this.getCount()) { return null; } Map<String, Object> record = this.dataList.get(index); String columnName = this.columns[columnIndex]; return record.get(columnName); } /** * 根据columnName返回数据 */ @Override public Object get(String columnName) { Map<String, Object> record = this.dataList.get(index); return record.get(columnName); } /** * 获得列集合 */ @Override public String[] getColumns() { Map<String, Object> temp = this.dataList.get(0); Set<Entry<String, Object>> entrys = temp.entrySet(); List<String> columns = new ArrayList<String>(); for (Entry<String, Object> e : entrys) { columns.add(e.getKey()); } String[] s = new String[entrys.size()]; columns.toArray(s); return s; } @Override public int getCount() { return this.dataList.size(); } @Override public boolean next() { index += 1; if(index >= this.getCount()) { return false; } return true; } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2013-05-03
免费吗 ?
|
|
返回顶楼 | |
发表时间:2013-05-03
不免费可以破解
|
|
返回顶楼 | |
发表时间:2013-05-07
FirstBlood 写道 不免费可以破解
朋友, 如何破解,麻烦把破解方法发出来呗。 |
|
返回顶楼 | |
发表时间:2013-05-07
最近该网站出现了好几篇这样的文章,但是却没见有人分析,该工具与POI,JXL的异同,以及相应的效率、适用场景?楼主根据鄙人的意见,完善一下,为后人乘凉之用!
|
|
返回顶楼 | |
发表时间:2013-07-01
请问楼主,通过excel转化成的html、pdf如何破解呢?
|
|
返回顶楼 | |