/** * * @param response 响应对象 * @param filename 输出文件名 eg:"expot" * @param sheetName 输出工作表名 eg:"sheet1" * @param tableHeadName 工作表中大标题 eg:"this is result sheet!" * @param titlesList 工作表每一列标题 eg:"列1,列2,列3" * @param objFieldList 要显示 list 里面所装的对象的字段列表集合 eg:"id,name,sex" * @param contentList 要写入表格的数据集合 */ public void exportExcel(HttpServletResponse response, String filename, String sheetName, String tableHeadName, String[] titlesList, String[] objFieldList, List<?> contentList) { ServletOutputStream out = null; WritableWorkbook book = null; if (titlesList == null || titlesList.length == 0) { return; } if (objFieldList == null || objFieldList.length == 0) { return; } if (titlesList.length != objFieldList.length) { return; } if (contentList == null || contentList.size() == 0) { return; } try { response.setContentType("application/ms-excel"); response.reset(); response.setHeader("Content-disposition", "attachment; filename="" + new String(filename.getBytes(), "ISO8859-1") + """ + ".xls"); out = response.getOutputStream(); book = Workbook.createWorkbook(out); // 计算将会生成几个Sheet表单 int listSize = contentList.size();// 数据总数 int totalCount = 1;// 默认的Sheet的个数表单 int pageSize = 65000;// 设置个sheet表单的条数 if (listSize % pageSize == 0) { totalCount = listSize / pageSize; } else { totalCount = listSize / pageSize + 1; } // 反射获取数据集合里面的数据对象 Object obj = contentList.get(0); Class objClazz = null; if (obj != null) { objClazz = obj.getClass(); } Field[] filds = null; if (objClazz != null) { filds = objClazz.getDeclaredFields();// 获取对象的所有字段 } int totalCol = titlesList.length; for (int n = 0; n < totalCount; n++) {// 循环执行次数 即需要建立多少个sheet表单 int index = 2;// 行数 int col = 0;// 列数 WritableSheet sheet = null; if (n != 0) { sheet = book.createSheet(sheetName + n, n); } else { sheet = book.createSheet(sheetName, n); } // 表头 Label labelHead = new Label(0, 0, tableHeadName); sheet.addCell(labelHead); sheet.mergeCells(0, 0, titlesList.length - 1, 0);// 合并表头信息 WritableCellFormat wc = new WritableCellFormat(); // 设置居中 wc.setAlignment(Alignment.CENTRE); WritableFont wfont = new WritableFont( WritableFont.createFont("宋体"), 20); wc.setFont(wfont); labelHead.setCellFormat(wc); // 生成Excel表头数据 for (int i = 0, size = titlesList.length; i < size; i++) { Label label = new Label(i, 1, titlesList[i]); sheet.addCell(label); // 设置每一列的字体样式 WritableFont font3 = new WritableFont(WritableFont.ARIAL, 9); WritableCellFormat format3 = new WritableCellFormat(font3); format3.setAlignment(jxl.format.Alignment.CENTRE); // 将每一列的宽度设置为30 sheet.setColumnView(i, 30, format3); } // 生成表格数据 for (int k = n * pageSize; k < n * pageSize + pageSize && k < listSize; k++) { Object objArg = contentList.get(k);// 获取list里面放置的对象 Object content = null;// 值 Object objType = null;// 类型 boolean fg = false; for (String objAttribute : objFieldList) {// 循环取出每个字段 for (Field fd : filds) { String fildName = fd.getName(); if (fildName != null && objAttribute != null && objAttribute.equals(fildName)) { boolean flag = fd.isAccessible(); fd.setAccessible(true); content = fd.get(objArg);// 获取字段的相关属性值 fd.setAccessible(flag); fg = true; // 获取当前字段的类型 objType = fd.getType(); break; } } if (fg) { if (col == totalCol) { index++;// 行数转为下一行 col = 0;// 列数又从0开始 } fg = false; String tempContent = ""; // 特殊数据类型转化 if (objType != null && objType.toString().equals( "class java.util.Date")) { if (content != null) { // 时间 String columnName = titlesList[col]; if (columnName != null && columnName.indexOf("日期") != -1) { tempContent = ConstUtil.dateToStr((Date)content, 0);//日期 2012-09-01 } else { tempContent =ConstUtil.dateToStr((Date)content, 1);//时间 2012-09-01 12:23:55 } } } else if (objType != null && objType.toString().equals( "class java.math.BigDecimal")) { if (content != null) { // 金额格式 BigDecimal 仅仅用于金额 DecimalFormat df = new DecimalFormat( "###,##0.00"); tempContent = df.format(content); } } else {//其他都用string输出 if (content != null) { tempContent = content.toString(); } } Label label = new Label(col++, index, tempContent); sheet.addCell(label); } } } } book.write();// 写出Excel表格 response.flushBuffer(); out.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (book != null) { book.close(); } if (out != null) { out.close(); } } catch (Exception e) { e.printStackTrace(); } } }
jxl 导出excel通用工具方法
最新推荐文章于 2021-02-24 03:11:37 发布