博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java使用jxl导出Excel表格源码
阅读量:4293 次
发布时间:2019-05-27

本文共 4923 字,大约阅读时间需要 16 分钟。

/**	 * 导出Excel	 * 	 * @param list	 *            :结果集合	 * @param filePath	 *            :指定的路径名	 * @param out	 *            :输出流对象 通过response.getOutputStream()传入	 * @param mapFields	 *            :导出字段 key:对应实体类字段 value:对应导出表中显示的中文名	 * @param sheetName	 *            :工作表名称	 */	public static void createExcel(List list, String filePath,			OutputStream out, Map
mapFields, String sheetName) { sheetName = sheetName != null && !sheetName.equals("") ? sheetName : "sheet1"; WritableWorkbook wook = null;// 可写的工作薄对象 Object objClass = null; try { if (filePath != null && !filePath.equals("")) { wook = Workbook.createWorkbook(new File(filePath));// 指定导出的目录和文件名 // 如:D:\\test.xls } else { wook = Workbook.createWorkbook(out);// jsp页面导出用 } // 设置头部字体格式 WritableFont font = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.RED); // 应用字体 WritableCellFormat wcfh = new WritableCellFormat(font); // 设置其他样式 wcfh.setAlignment(Alignment.CENTRE);// 水平对齐 wcfh.setVerticalAlignment(VerticalAlignment.CENTRE);// 垂直对齐 wcfh.setBorder(Border.ALL, BorderLineStyle.THIN);// 边框 wcfh.setBackground(Colour.YELLOW);// 背景色 wcfh.setWrap(false);// 不自动换行 // 设置内容日期格式 DateFormat df = new DateFormat("yyyy-MM-dd HH:mm:ss"); // 应用日期格式 WritableCellFormat wcfc = new WritableCellFormat(df); wcfc.setAlignment(Alignment.CENTRE); wcfc.setVerticalAlignment(VerticalAlignment.CENTRE);// 垂直对齐 wcfc.setBorder(Border.ALL, BorderLineStyle.THIN);// 边框 wcfc.setWrap(false);// 不自动换行 // 创建工作表 WritableSheet sheet = wook.createSheet(sheetName, 0); SheetSettings setting = sheet.getSettings(); setting.setVerticalFreeze(1);// 冻结窗口头部 int columnIndex = 0; // 列索引 List
methodNameList = new ArrayList
(); if (mapFields != null) { String key = ""; Map
getMap = null; Method method = null; // 开始导出表格头部 for (Iterator
i = mapFields.keySet().iterator(); i .hasNext();) { key = i.next(); // 应用wcfh样式创建单元格 sheet.addCell(new Label(columnIndex, 0, mapFields.get(key), wcfh)); // 记录字段的顺序,以便于导出的内容与字段不出现偏移 methodNameList.add(key); columnIndex++; } if (list != null && list.size() > 0) { // 导出表格内容 for (int i = 0, len = list.size(); i < len; i++) { objClass = list.get(i); getMap = getAllMethod(objClass);// 获得对象所有的get方法 // 按保存的字段顺序导出内容 for (int j = 0; j < methodNameList.size(); j++) { // 根据key获取对应方法 method = getMap.get("GET" + methodNameList.get(j).toString() .toUpperCase()); if (method != null) { // 从对应的get方法得到返回值 String value = method.invoke(objClass, null).toString(); // 应用wcfc样式创建单元格 sheet.addCell(new Label(j, i + 1, value, wcfc)); } else { // 如果没有对应的get方法,则默认将内容设为"" sheet.addCell(new Label(j, i + 1, "", wcfc)); } } } } /** **********将以上缓存中的内容写到EXCEL文件中******** */ wook.write(); //System.out.println("导出Excel成功!"); } else { throw new Exception("传入参数不合法"); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (wook != null) { wook.close(); } if (out != null) { out.close(); } } catch (Exception e2) { e2.printStackTrace(); } } } /** * 获取类的所有get方法 * * @param cls * @return */ public static HashMap
getAllMethod(Object obj) throws Exception { HashMap
map = new HashMap
(); Method[] methods = obj.getClass().getMethods();// 得到所有方法 String methodName = ""; for (int i = 0; i < methods.length; i++) { methodName = methods[i].getName().toUpperCase();// 方法名 if (methodName.startsWith("GET")) { map.put(methodName, methods[i]);// 添加get方法至map中 } } return map; }
/**	 * 根据指定路径导出Excel	 * 	 * @param list	 * @param filePath	 * @param mapFields	 * @param sheetName	 */	public static void ImportExcel(List list, String filePath,			Map
mapFields, String sheetName) { createExcel(list, filePath, null, mapFields, sheetName); } /** * 从Jsp页面导出Excel * * @param list * @param filePath * @param out * @param mapFields * @param sheetName */ public static void ImportExcel(List list, OutputStream out, Map
mapFields, String sheetName) { createExcel(list, null, out, mapFields, sheetName); }
public static void main(String[] args) {				// 测试		Store user = new Store();		user.setName("张三");		user.setAddress("德政北路");		user.setTime(new Date());		List
list = new ArrayList
(); list.add(user); // 将user实体添加到list Map
map = new LinkedHashMap
(); // 设置导出的字段。其中map中的key必须与实体类中的字段一致,不区分大小写,这里需要注意的是:请确保实体类中给导出的字段提供了set、get方法,不然会取不到值 map.put("name", "姓名"); map.put("address", "地址"); map.put("Time", "创建时间"); ImportExcel(list, "E:\\xxsl\\test.xls", map, "员工信息"); }

效果图:

JSP效果图:

WEB环境下直接调用ImportExcel的方法就可以了。

你可能感兴趣的文章
python正则表达式入门二
查看>>
scrapy运行
查看>>
XPATH入门
查看>>
python爬虫 CSS选择器
查看>>
正常关闭java程序
查看>>
查看linux核心数
查看>>
数据结构与算法三: 数组
查看>>
Activiti工作流会签二 启动流程
查看>>
Activiti工作流会签三 撤销,审批,驳回
查看>>
Oauth2方式实现单点登录
查看>>
CountDownLatch源码解析加流程图详解--AQS类注释翻译
查看>>
ES相关度评分
查看>>
我们一起做一个可以商用的springboot脚手架
查看>>
idea在搭建ssm框架时mybatis整合问题 无法找到mapper
查看>>
java设计基本原则----单一职责原则
查看>>
HashMap的实现
查看>>
互斥锁 synchronized分析
查看>>
java等待-通知机制 synchronized和waity()的使用实践
查看>>
win10 Docke安装mysql8.0
查看>>
docker 启动已经停止的容器
查看>>