| | |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | |
| | | import javax.imageio.ImageIO; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.io.OutputStream; |
| | |
| | | * @return 结果 |
| | | * @throws IOException |
| | | */ |
| | | public void exportExcelManySheet(HttpServletResponse response, List<ExcelExp> list) throws IOException { |
| | | response.setContentType("application/vnd.ms-excel"); |
| | | response.setCharacterEncoding("utf-8"); |
| | | public void exportExcelManySheet(ByteOutputStream response, List<ExcelExp> list) throws IOException { |
| | | // response.setContentType("application/vnd.ms-excel"); |
| | | // response.setCharacterEncoding("utf-8"); |
| | | try { |
| | | createWorkbook(); |
| | | for (int index = 0; index <list.size(); index++) { |
| | |
| | | // 取出一共有多少个sheet. |
| | | // double sheetNo = Math.ceil(list.size() / sheetSize); |
| | | createSheetManySheet(index); |
| | | // 产生一行 |
| | | Row row = sheet.createRow(0); |
| | | int column = 0; |
| | | // 写入各个字段的列头名称 |
| | | for (Object[] os : fields) { |
| | | Excel excel = (Excel) os[1]; |
| | | this.createCell(excel, row, column++); |
| | | // 产生一行 if(index == 1) { |
| | | if(index==1) |
| | | { |
| | | Row row = sheet.createRow(0); |
| | | row.setHeightInPoints(20); // 设置行高为20磅 |
| | | int column = 0; |
| | | row = sheet.createRow(0); |
| | | sheet.addMergedRegion(new CellRangeAddress(2, 3, 0, 1)); |
| | | row = sheet.createRow(2); |
| | | row.setHeightInPoints(120); // 设置行高为20磅 |
| | | |
| | | //拿到图片 |
| | | // 创建单元格并添加图片 |
| | | Cell cell = row.createCell(0); |
| | | //sheet.setColumnWidth(0, 20 * 256); // 第一列宽度20字符 |
| | | //sheet.setColumnWidth(1, 15 * 256); // 第二列宽度15字符 |
| | | //sheet.setColumnWidth(2, 25 * 256); // 第三列宽度25字符 // 设置第一列宽度为20个字符 |
| | | |
| | | ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) (cell.getColumnIndex()+1), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() +2), |
| | | cell.getRow().getRowNum() + 1); |
| | | // 计算居中位置 |
| | | // int col1 = 0; // 中间列 |
| | | // int col2 = col1 + 2; |
| | | // anchor.setCol1(col1); |
| | | // anchor.setCol2(col2); |
| | | // anchor.setDx1(100); |
| | | // anchor.setDy1(0); |
| | | // anchor.setDx2(255); // 宽度 |
| | | // anchor.setDy2(255); // 高度 |
| | | anchor.setRow1(2); |
| | | anchor.setRow2(3); |
| | | byte[] data = list.get(index).getSedcode(); |
| | | // 设置图片大小和位置 |
| | | anchor.setDx1(2400000); |
| | | anchor.setDy1(100); |
| | | |
| | | anchor.setDy2(-600000); |
| | | // 获取图片原始尺寸 |
| | | BufferedImage image = ImageIO.read(new ByteArrayInputStream(data)); |
| | | double widthInEMU = image.getWidth() * 9525 * 0.2; |
| | | double heightInEMU = image.getHeight() * 9525; |
| | | |
| | | // 设置图片原始尺寸 |
| | | anchor.setDx2(-100000); // 原始宽度 |
| | | // anchor.setDy2((int)heightInEMU); // 原始高度 |
| | | //anchor.setCol1(5); // 从第6列开始显示 |
| | | //anchor.setCol2(10); // 到第11列结束 |
| | | |
| | | //Files.readAllBytes(Paths.get(RuoYiConfig.getProfile() + "/upload/2025/08/14/30_20250814212128A031.jpg")); |
| | | // |
| | | // |
| | | // System.out.println(data.length); |
| | | getDrawingPatriarch(cell.getSheet()).createPicture(anchor, |
| | | cell.getSheet().getWorkbook().addPicture(data, getImageType(data))); |
| | | |
| | | |
| | | |
| | | String [] tits = {"档 号:","档案馆(室)号:","缩 微 号: ","发 文 号:", |
| | | "案 卷 题 名:","编 制 日 期:","编 制 单 位:","保 管 期 限:","密 级:"}; |
| | | |
| | | List tmp = list.get(index).getDataset(); |
| | | Object obj = tmp.get(0); |
| | | Field[] declaredFields = this.clazz.getDeclaredFields(); |
| | | System.out.println(declaredFields.length); |
| | | int i = 0; |
| | | for (Field field : declaredFields) { |
| | | // 设置字段的访问权限,以便于访问私有字段 |
| | | field.setAccessible(true); |
| | | |
| | | // 获取字段的名称和值 |
| | | String fieldName = field.getName(); |
| | | // Object fieldValue = field.get(person); |
| | | Class<?> clazz = obj.getClass(); |
| | | Field field1 =clazz.getDeclaredField(fieldName); |
| | | field1.setAccessible(true); |
| | | Object fieldValue = field1.get(obj); |
| | | if(fieldValue==null) |
| | | fieldValue=""; |
| | | if(i <= 3) |
| | | row = sheet.createRow(i+4); |
| | | if(i > 3) |
| | | row = sheet.createRow(i+7); |
| | | // row.setHeight((short) 30); |
| | | row.setHeightInPoints(40); // 设置行高为20磅 |
| | | // row.createCell(0) |
| | | cell = row.createCell(0); |
| | | //设置cell的宽度 |
| | | sheet.setColumnWidth(0, 20 * 256); |
| | | sheet.setColumnWidth(1, 60 * 256); // 第二列宽度15字符 |
| | | |
| | | CellStyle style = wb.createCellStyle(); |
| | | style.setAlignment(HorizontalAlignment.RIGHT); |
| | | style.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | style.setVerticalAlignment(VerticalAlignment.CENTER); // 设置垂直居中 |
| | | // style.setAlignment(VerticalAlignment.CENTER); |
| | | Font font = wb.createFont(); |
| | | font.setBold(true); |
| | | style.setFont(font); |
| | | style.setWrapText(true); // 设置自动换行 |
| | | cell.setCellStyle(style); |
| | | cell.setCellValue(tits[i]); |
| | | Cell cel = row.createCell(1); |
| | | CellStyle style1 = wb.createCellStyle(); |
| | | style1.setBorderBottom(BorderStyle.THIN); // 添加下划线 |
| | | style1.setWrapText(true); // 设置自动换行 |
| | | style1.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | cel.setCellStyle(style1); |
| | | cel.setCellValue(fieldValue.toString()); |
| | | // sheet.setColumnWidth(0, 60 * 256); |
| | | i++; |
| | | if(i==3) |
| | | { |
| | | row = sheet.createRow(8); |
| | | // row.setHeight((short) 30); |
| | | row.setHeightInPoints(40); // 设置行高为20磅 |
| | | row = sheet.createRow(9); |
| | | //设置条形码 |
| | | Cell cell1 = row.createCell(0); |
| | | //sheet.setColumnWidth(0, 20 * 256); // 第一列宽度20字符 |
| | | //sheet.setColumnWidth(1, 15 * 256); // 第二列宽度15字符 |
| | | //sheet.setColumnWidth(2, 25 * 256); // 第三列宽度25字符 // 设置第一列宽度为20个字符 |
| | | |
| | | ClientAnchor anchor1 = new XSSFClientAnchor(0, 0, 0, 0, (short) (cell1.getColumnIndex()), cell1.getRow().getRowNum(), (short) (cell1.getColumnIndex() +2), |
| | | cell1.getRow().getRowNum() + 1); |
| | | // 计算居中位置 |
| | | //// 设置图片大小和位置 |
| | | anchor1.setDx1(600000); |
| | | anchor1.setDy1(0); |
| | | anchor1.setDx2(-600000); // 宽度 |
| | | anchor1.setDy2(255); // 高度 |
| | | //anchor1.setCol1(0.5); // 从第6列开始显示 |
| | | //anchor1.setCol2(10); // 到第11列结束 |
| | | anchor1.setRow1(9); |
| | | anchor1.setRow2(10); |
| | | byte[] dat = list.get(index).getImgr(); |
| | | //Files.readAllBytes(Paths.get(RuoYiConfig.getProfile() + "/upload/2025/08/14/30_20250814212128A031.jpg")); |
| | | // |
| | | // |
| | | // System.out.println(data.length); |
| | | getDrawingPatriarch(cell1.getSheet()).createPicture(anchor1, |
| | | cell.getSheet().getWorkbook().addPicture(dat, getImageType(dat))); |
| | | |
| | | |
| | | |
| | | |
| | | row.setHeightInPoints(70); // 设置行高为20磅 |
| | | row = sheet.createRow(10); |
| | | row.setHeightInPoints(40); // 设置行高为20磅 |
| | | } |
| | | // 打印字段名称和值 |
| | | System.out.println(fieldName + ": " + fieldValue); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | } |
| | | if (Excel.Type.EXPORT.equals(type)) { |
| | | fillExcelData(index, row); |
| | | addStatisticsRow(); |
| | | else { |
| | | |
| | | Row row = sheet.createRow(0); |
| | | int column = 0; |
| | | // 写入各个字段的列头名称 |
| | | for (Object[] os : fields) { |
| | | Excel excel = (Excel) os[1]; |
| | | this.createCell(excel, row, column++); |
| | | } |
| | | if (Excel.Type.EXPORT.equals(type)) { |
| | | fillExcelData(index, row); |
| | | addStatisticsRow(); |
| | | } |
| | | } |
| | | } |
| | | wb.write(response.getOutputStream()); |
| | | wb.write(response); |
| | | } catch (IOException e) { |
| | | log.error("导出Excel异常{}", e.getMessage()); |
| | | } catch (NoSuchFieldException e) { |
| | | throw new RuntimeException(e); |
| | | } catch (IllegalAccessException e) { |
| | | throw new RuntimeException(e); |
| | | } finally { |
| | | if (wb != null) { |
| | | try { |
| | |
| | | e1.printStackTrace(); |
| | | } |
| | | } |
| | | if (response.getOutputStream() != null) { |
| | | try { |
| | | response.getOutputStream().close(); |
| | | } catch (IOException e1) { |
| | | e1.printStackTrace(); |
| | | } |
| | | } |
| | | // if (response.getOutputStream() != null) { |
| | | // try { |
| | | // response.getOutputStream().close(); |
| | | // } catch (IOException e1) { |
| | | // e1.printStackTrace(); |
| | | // } |
| | | // } |
| | | } |
| | | // exportExcel(response.getOutputStream()); |
| | | } |
| | |
| | | row = sheet.createRow(i + 4 ); |
| | | else |
| | | row = sheet.createRow(i + 1 ); |
| | | |
| | | row.setHeightInPoints(40); // 设置行高为20磅 |
| | | // row.createCell(0) |
| | | |
| | | // 得到导出对象. |
| | | T vo = (T) list.get(i); |
| | | int column = 0; |
| | |
| | | Cell cell = null; |
| | | try { |
| | | // 设置行高 |
| | | row.setHeight(maxHeight); |
| | | row.setHeight((short) 2000); |
| | | // 根据Excel中设置情况决定是否导出,有些情况需要保持为空,希望用户填写这一列. |
| | | if (attr.isExport()) { |
| | | // 创建cell |
| | | cell = row.createCell(column); |
| | | //设置cell的宽度 |
| | | sheet.setColumnWidth(0, 20 * 256); |
| | | sheet.setColumnWidth(1, 60 * 256); // 第二列宽度15字符 |
| | | |
| | | CellStyle style = wb.createCellStyle(); |
| | | // style.setAlignment(HorizontalAlignment.RIGHT); |
| | | style.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | style.setVerticalAlignment(VerticalAlignment.CENTER); // 设置垂直居中 |
| | | // style.setAlignment(VerticalAlignment.CENTER); |
| | | Font font = wb.createFont(); |
| | | font.setBold(true); |
| | | style.setFont(font); |
| | | style.setWrapText(true); // 设置自动换行 |
| | | |
| | | cell.setCellStyle(style); |
| | | // attr.align(); |
| | | // int align = attr.align().value; |
| | | // cell.setCellStyle(styles.get("data" + (align >= 1 && align <= 3 ? align : ""))); |