导出excel
2016-02-29 16:15
267 查看
———-
String ExcelFilePath = exportExcelService.exportExcelFile("hosDocVitalityStats", "医院,总医生数,登录医生数,活跃度 ", "30,20,20,20", list, total.get(0));// 返回导出的Excel文件路径
/** * 导出excel * * @param statsType * @param headers * @param widths * @param pageList * @param object * @param path */ public String exportExcelFile(String statsType, String headers, String widths, List list, Object object) { String filename = ""; try { filename = createFile(statsType, headers, widths, list, object); } catch (SysRuntimeException e) { throw new SysRuntimeException(e.getCode(), e.getMsg()); } catch (Exception e) { e.printStackTrace(); } return filename; }
/*创建excel文件*/ private String createFile(String statsType, String headers, String widths, List list, Object object) throws IOException, RowsExceededException, WriteException { String separator = File.separator; String path = this.getClass().getResource("/").getPath(); path = path.replace("WEB-INF/classes/", "downloadFile" + separator); System.out.println("----------------------------------------------------------" + path); String filename = statsType + "_" + DateUtil.dateFormate(new Date(), "yyyy-MM-dd") + ".xls"; FileOutputStream fo = new FileOutputStream(path + filename); WritableWorkbook book = Workbook.createWorkbook(fo); WritableSheet sheet = book.createSheet("AA", 0); try { addSheetTitle(sheet, statsType); addSheetHead(sheet, headers, widths); addSheetBody(sheet, list, object, statsType); } catch (SysRuntimeException e) { throw new SysRuntimeException(e.getCode(), e.getMsg()); } catch (Exception e) { e.printStackTrace(); } finally { book.write(); book.close(); fo.close(); } return filename; }
protected void addSheetTitle(WritableSheet sheet, String statsType) throws WriteException { WritableCellFormat headerFormat = new WritableCellFormat(NumberFormats.TEXT); // 添加字体设置 WritableFont font = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE); headerFormat.setFont(font); // 设置单元格背景色:表头为黄色 headerFormat.setBackground(Colour.YELLOW); // 设置表头表格边框样式 headerFormat.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.GRAY_25); // 表头内容水平居中显示 headerFormat.setAlignment(Alignment.CENTRE); try { sheet.addCell(new Label(0, 0, statsType, headerFormat)); sheet.setColumnView(0, 30); sheet.addCell(new Label(1, 0, "制单日期:", headerFormat)); sheet.addCell(new Label(2, 0, DateUtil.dateFormate(new Date(), "yyyy-MM-dd HH:mm:ss".toString()), headerFormat)); sheet.setColumnView(2, 30); } catch (Exception e) { e.printStackTrace(); throw new SysRuntimeException("-1", "创建标题内容失败"); } }
private void addSheetHead(WritableSheet sheet, String headers, String widths) { String[] headdata = headers.split(","); String[] widthdata = widths.split(","); WritableFont fBlue = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLUE); WritableCellFormat wcfF = new jxl.write.WritableCellFormat(fBlue); try { for (int i = 0; i < headdata.length; i++) { sheet.addCell(new Label(i, 1, headdata[i], wcfF)); int width = Integer.parseInt(StringUtils.isBlank(widthdata[i].toString()) ? "" : widthdata[i].toString()); sheet.setColumnView(i, width); } } catch (Exception e) { e.printStackTrace(); throw new SysRuntimeException("-1", "创建字段名行失败"); } }
private void addSheetBody(WritableSheet sheet, List list, Object object, String statsType) { int r = 1; try { if (list != null) { Iterator it = list.iterator(); while (it.hasNext()) { r++; addRow(sheet, r, it, null, statsType); } r++; addRow(sheet, r, null, object, statsType); } } catch (Exception e) { e.printStackTrace(); throw new SysRuntimeException("-1", "创建内容行失败"); } }
private void addRow(WritableSheet sheet, int r, Iterator it, Object object, String statsType) throws WriteException, RowsExceededException { WritableFont fRed = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED); WritableCellFormat wcfF = new jxl.write.WritableCellFormat(fRed); if ("hosDocVitalityStats".equals(statsType)) { if (it != null) { SaHosDocVitalityStats ob = (SaHosDocVitalityStats) it.next(); sheet.addCell(new Label(0, r, ob.getHosName())); sheet.addCell(new Label(1, r, ob.getTotalDocQuantity().toString())); sheet.addCell(new Label(2, r, ob.getLoginDocQuantity().toString())); sheet.addCell(new Label(3, r, ob.getVitalityRate().toString())); } if (object != null) { SaHosDocVitalityStats total = (SaHosDocVitalityStats) object; sheet.addCell(new Label(0, r, "汇总", wcfF)); sheet.addCell(new Label(1, r, total.getAlltotalDocQuantity().toString(), wcfF)); sheet.addCell(new Label(2, r, total.getAllloginDocQuantity().toString(), wcfF)); sheet.addCell(new Label(3, r, total.getAllvitalityRate().toString(), wcfF)); } }
相关文章推荐
- 广度优先搜索
- 一个适用于CentOS下的Redis启动脚本
- meteor settings
- 字符串装换成数字
- MyBatis(3.2.3) - Configuring MyBatis using XML, Mappers
- Android4.2.2自增物理按键
- js中小数向上取整数,向下取整数,四舍五入取整数的实现。
- android的ndk开发简介-android学习之旅(93)
- AutoCAD学习笔记——常用快捷方式及家装设计相关概念
- Salesforce开发工具之Metaforce - 同步/编辑代码
- php 调用银联接口
- android的ndk开发简介-android学习之旅(93)
- android的ndk开发简介-android学习之旅(93)
- 细谈 oracle备份的方法
- leetcode--Number of 1 Bits
- AIX5.3(64位)安装tomcat(包括安装JDK)全过程
- javascript函数与事件
- android的ndk开发简介-android学习之旅(93)
- TabLayout和ViewPager简单实现页卡的滑动
- java Executors源码简析