Java 导出Excel方法三
2016-03-07 15:07
561 查看
/** * 功能描述:导出Excel方法三(多行表头导出) * * * @param fileName * 文件名 * @param list * 数据 * @param keys * map key 值 * @param headers * Excel 列名 * @param title * 标题栏 传空则不需要 * @param listhander * 头部json hander:名称 size 跨行 * @param first * 表头从第几行开始 */ @SuppressWarnings("rawtypes") public static void exportMapExcel(HttpServletResponse response, String fileName, List<Map> list, String[] keys, String dateFormat, String title, ArrayList<JSONObject> listhander, int begin, int end) throws Exception { int rowIndex = 0; Workbook workbook = new HSSFWorkbook(); // 创建一个工作簿 Sheet sheet = workbook.createSheet(); // 创建一个Sheet页 sheet.autoSizeColumn((short) 0); // 单元格宽度自适应 CreationHelper helper = workbook.getCreationHelper(); CellStyle style = workbook.createCellStyle(); // 设置单元格样式 style.setDataFormat(helper.createDataFormat().getFormat(dateFormat)); // 格式化日期类型 style.setAlignment(CellStyle.ALIGN_CENTER); // 水平居中 style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 垂直居中 Row row = null; if (EmptyUtils.isNotEmpty(title)) { row = sheet.createRow(rowIndex++); // 创建第一行(标题) sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, keys.length - 1)); row.setHeight((short) 600); // 设置字体 Font font = workbook.createFont(); font.setFontHeightInPoints((short) 16); style.setFont(font); row.createCell(0).setCellValue(title); row.getCell(0).setCellStyle(style); for (int i = 0; i < begin - 2; i++) { row = sheet.createRow(rowIndex++); } } else { for (int i = 0; i < begin - 1; i++) { row = sheet.createRow(rowIndex++); } } CellStyle style1 = workbook.createCellStyle(); // 设置单元格样式 style1.setAlignment(CellStyle.ALIGN_CENTER); // 水平居中 style1.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 垂直居中 if (EmptyUtils.isNotEmpty(listhander)) { for (int i = 0; i < (end - begin + 1); i++) { row = sheet.createRow(rowIndex++); // 创建头部 for (JSONObject json1 : listhander) { int rowBegin = StringUtil.StringToInt(json1 .getString("rowBegin")); int colBegin = StringUtil.StringToInt((String) json1 .get("colBegin")); int rowEnd = StringUtil.StringToInt(json1 .getString("rowEnd")); int colEnd = StringUtil.StringToInt((String) json1 .get("colEnd")); if (((rowBegin + 1) - begin) == i) { sheet.addMergedRegion(new CellRangeAddress(rowBegin, rowEnd, colBegin, colEnd)); row.createCell(colBegin).setCellValue( (String) json1.get("hander")); row.getCell(colBegin).setCellStyle(style1); } } } } for (Map map : list) { row = sheet.createRow(rowIndex++); for (int i = 0; i < keys.length; i++) { row.createCell(i).setCellValue(getValue(map.get(keys[i]))); row.getCell(i).setCellStyle(style1); } } String ddate = new SimpleDateFormat("yyyyMMddhhmmss").format(Calendar .getInstance().getTime()); response.setContentType("application/vnd.ms-excel;charset=UTF-8"); response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("gb2312"), "iso8859-1") + "_" + ddate + ".xls");// 设定输出文件头 OutputStream output = response.getOutputStream(); workbook.write(output); output.flush(); output.close(); }
相关文章推荐
- Java中的可变参数
- Java 导出Excel方法二
- java程序设计基础_陈国君版第五版_第三章习题
- Spring XML中<beans>中属性概述
- Struts2的类型转换及输入校验(数组类型的使用)
- Java 导出Excel方法一
- Java5实现阻塞队列
- eclipse运行Android项目出现“The connection to adb is down, and a severe error has occured. You must restart adb and Eclipse. ”
- java XML转JSON格式
- java的定时器功能
- mac os intellij如何快路查看一个java类的所有方法,结构
- java中的继承
- java解析xml的几种方式
- java抽象类和接口区别
- 升级hibernate>4,spring>3.1笔记
- R.Java文件报错
- Java JPA知识的理解与总结
- java环境配置正确,但是ecplise打不开,提示java虚拟机找不到
- java实现的第三方线程池框架
- Spring配置 <context:component-scan/> <mvc:annotation-driven />