| | |
| | | int column = 0; |
| | | if(index == 0) { |
| | | // 合并第一行的前5个单元格 |
| | | sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 8)); |
| | | |
| | | // 必须先设置为true |
| | | // 页面布局设置 |
| | |
| | | { |
| | | row.setHeight((short)(55.8 * 20)); |
| | | |
| | | Cell cell = row.createCell(6); |
| | | Cell cell = row.createCell(0); |
| | | // for(int cl = 1 ; cl <=8; cl++) |
| | | // cell = row.createCell(cl); |
| | | // |
| | | // |
| | | // for (int col = 4; col <= 7; col++) { |
| | | // row.getSheet().setColumnWidth(col, 15 * 256); // 每列15字符宽 |
| | | // } |
| | | |
| | | int margin = -40* 9525; |
| | | int mary = -11 * 9525; |
| | | XSSFClientAnchor anchor = new XSSFClientAnchor( |
| | | 0, // dx1 |
| | | 0, // dy1 |
| | | margin, // dx2:右边界左移10像素 |
| | | mary, // dy2 |
| | | (short)5, // col1 |
| | | 0, // row1 |
| | | (short)9, // col2 |
| | | 1 // row2 |
| | | ); |
| | | // 设置图片大小和位置 |
| | | ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) (cell.getColumnIndex()+1), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() +2), |
| | | cell.getRow().getRowNum() + 1); |
| | | // ClientAnchor anchor = new XSSFClientAnchor(0, 0, 600, 150, (short)0, 0, (short)6, 1); |
| | | // anchor.setDx2(488); |
| | | // anchor.setDy2(10); |
| | | // anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE); |
| | | // 计算居中位置 |
| | | // 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.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE); |
| | | |
| | | |
| | | // 计算居中位置 |
| | | int col1 = 4; // 中间列 |
| | | int col2 = col1 + 3; |
| | | anchor.setCol1(col1); |
| | | anchor.setCol2(col2); |
| | | anchor.setRow1(0); |
| | | anchor.setRow2(1); |
| | | // int col1 = 4; // 中间列 |
| | | // int col2 = col1 + 3; |
| | | // anchor.setCol1(col1); |
| | | // anchor.setCol2(col2); |
| | | // anchor.setRow1(0); |
| | | // anchor.setRow2(1); |
| | | |
| | | byte[] data = bt; |
| | | |
| | | // 获取图片原始尺寸 |
| | | BufferedImage image = ImageIO.read(new ByteArrayInputStream(data)); |
| | | double widthInEMU = image.getWidth() * 9525 * 0.2; |
| | | double heightInEMU = image.getHeight() * 9525; |
| | | // double widthInEMU = image.getWidth() * 9525 * 0.2; |
| | | // double heightInEMU = image.getHeight() * 9525; |
| | | |
| | | // 设置图片原始尺寸 |
| | | // anchor.setDx2(-100000); // 原始宽度 |
| | |
| | | } |
| | | else |
| | | row.setHeight((short)(33 * 20)); |
| | | // sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 8)); |
| | | |
| | | // sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 7)); |
| | | if(firow==1) |
| | |
| | | Cell cell = null; |
| | | try { |
| | | // 设置行高为自动调整 |
| | | row.setHeight((short) -1); |
| | | // row.setHeight((short) -1); |
| | | // 根据Excel中设置情况决定是否导出,有些情况需要保持为空,希望用户填写这一列. |
| | | if (attr.isExport()) { |
| | | // 创建cell |
| | |
| | | } else if (align == HorizontalAlignment.RIGHT) { |
| | | styleKey = "data3"; |
| | | } |
| | | cell.setCellStyle(styles.get(styleKey)); |
| | | // 获取并修改样式 |
| | | CellStyle style = styles.get(styleKey); |
| | | Workbook workbook = row.getSheet().getWorkbook(); |
| | | CellStyle newStyle = workbook.createCellStyle(); |
| | | newStyle.cloneStyleFrom(style); |
| | | newStyle.setWrapText(true); // 关键:启用自动换行 |
| | | cell.setCellStyle(newStyle); |
| | | |
| | | // 用于读取对象中的属性 |
| | | Object value = getTargetValue(vo, field, attr); |
| | |
| | | // 设置列类型 |
| | | setCellVo(value, attr, cell); |
| | | } |
| | | adjustRowHeightAfterSetValue(row, cell, value); |
| | | addStatisticsData(column, Convert.toStr(value), attr); |
| | | } |
| | | } catch (Exception e) { |
| | |
| | | } |
| | | return cell; |
| | | } |
| | | /** |
| | | * 设置值后调整行高 |
| | | */ |
| | | private void adjustRowHeightAfterSetValue(Row row, Cell cell, Object value) { |
| | | if (value == null) return; |
| | | |
| | | String text = value.toString(); |
| | | Sheet sheet = row.getSheet(); |
| | | |
| | | // 1. 计算自动高度(基于内容) |
| | | short autoHeight = calculateSimpleAutoHeight(text, cell); |
| | | |
| | | // 2. 获取当前行高 |
| | | short currentHeight = row.getHeight(); |
| | | if (currentHeight == -1) { |
| | | currentHeight = sheet.getDefaultRowHeight(); |
| | | } |
| | | |
| | | // 3. 使用较大的高度(自动计算的高度或当前高度) |
| | | short baseHeight = (short) Math.max(currentHeight, autoHeight); |
| | | System.out.println(baseHeight+"aaaaaaaaatttttttt"); |
| | | // 4. 在基础上增加额外高度(100单位 = 5点) |
| | | short extraHeight = 80; |
| | | short newHeight = (short) (baseHeight + extraHeight); |
| | | |
| | | // 5. 限制最大高度 |
| | | short maxHeight = (short) 4000; // 100点 |
| | | row.setHeight((short) Math.min(maxHeight,newHeight)); |
| | | } |
| | | |
| | | /** |
| | | * 简化的自动高度计算 |
| | | */ |
| | | private short calculateSimpleAutoHeight(String text, Cell cell) { |
| | | if (text == null || text.isEmpty()) return 0; |
| | | |
| | | Sheet sheet = cell.getSheet(); |
| | | int colIndex = cell.getColumnIndex(); |
| | | |
| | | // 获取列宽(字符数) |
| | | int colWidthChars = sheet.getColumnWidth(colIndex) / 256; |
| | | if (colWidthChars <= 0) colWidthChars = 10; |
| | | |
| | | // 计算文本行数 |
| | | int lines = 1; |
| | | if (text.contains("\n")) { |
| | | // 有显式换行 |
| | | String[] parts = text.split("\n"); |
| | | for (String part : parts) { |
| | | lines += Math.max(1, (int) Math.ceil(part.length() * 1.5 / colWidthChars)); |
| | | } |
| | | } else { |
| | | // 自动换行 |
| | | lines = (int) Math.ceil(text.length() * 1.5 / colWidthChars); |
| | | } |
| | | |
| | | // 每行高度:假设18点(360 POI单位) |
| | | return (short) (lines * 360); |
| | | } |
| | | /** |
| | | * 设置单元格提示 |
| | | * |