fei
9 天以前 a5edca4c62ea99418dcaa5d792947b1bab7fe8b2
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtilManySheetSecond.java
@@ -16,9 +16,12 @@
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -299,42 +302,31 @@
//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)));
                    if (data != null && data.length > 0) {
                        // 创建单元格
                        Cell qrCell = sheet.createRow(2).createCell(1);
                        // 设置行高足够高以显示二维码
                        sheet.getRow(2).setHeightInPoints(120);
                        // 二维码需要较大的列宽来显示,设置之前先保存当前列宽
                        int currentColumnWidth = sheet.getColumnWidth(1);
                        // 设置足够宽的列宽以显示二维码
                        sheet.setColumnWidth(1, 60 * 256);
                        // 创建图片锚点,设置在第2行第2列
                        // 二维码显示在右上角且不填满整个格子
                        // 缩小图片范围:只占用部分单元格空间
                        ClientAnchor anchor = new HSSFClientAnchor(600, 20, 1000, 200, (short) 1, 2, (short) 1, 2);
                        // 设置图片位置和大小
                        anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_DONT_RESIZE);
                        // 添加图片到工作表
                        getDrawingPatriarch(sheet).createPicture(anchor,
                                wb.addPicture(data, getImageType(data)));
                        // 二维码添加完成后,恢复原来的列宽设置
                        sheet.setColumnWidth(1, currentColumnWidth);
                    }
@@ -367,9 +359,12 @@
                        row.setHeightInPoints(40); // 设置行高为20磅
                       // row.createCell(0)
                        cell = row.createCell(0);
                        //设置cell的宽度
                        sheet.setColumnWidth(0, 20 * 256);
                        sheet.setColumnWidth(1, 60 * 256); // 第二列宽度15字符
                        // 根据用户需求设置第二子表格的列宽
                        // 第一列宽度设置为27
                        // 第二列宽度设置为51
                        // 使用与addCell方法相同的计算公式设置列宽
                        sheet.setColumnWidth(0, (int) ((27 + 0.72) * 256)); // 第一列:宽度27
                        sheet.setColumnWidth(1, (int) ((51 + 0.72) * 256)); // 第二列:宽度51
                        CellStyle style = wb.createCellStyle();
                        style.setAlignment(HorizontalAlignment.RIGHT);
@@ -379,7 +374,7 @@
                        Font font = wb.createFont();
                        font.setBold(true);
                        style.setFont(font);
style.setWrapText(true); // 设置自动换行
                        style.setWrapText(true); // 设置自动换行
                        cell.setCellStyle(style);
                        cell.setCellValue(tits[i]);
                        Cell cel = row.createCell(1);
@@ -388,40 +383,52 @@
                        style1.setWrapText(true); // 设置自动换行
                        style1.setVerticalAlignment(VerticalAlignment.CENTER);
                        cel.setCellStyle(style1);
                        cel.setCellValue(fieldValue.toString());
                        // 处理日期格式化,特别是"编  制  日  期:"这一行
                        if (i == 5 && fieldValue instanceof Date) {
                            // 设置日期格式为yyyy-MM-dd
                            String dateStr = DateUtils.parseDateToStr("yyyy-MM-dd", (Date) fieldValue);
                            cel.setCellValue(dateStr);
                        } else {
                            cel.setCellValue(fieldValue.toString());
                        }
                      //  sheet.setColumnWidth(0, 60 * 256);
                        i++;
                        if(i==3)
                        {
                            row = sheet.createRow(8);
                            // 设置行高为自动调整
                            row.setHeight((short) -1);
                            // 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)));
                            if (dat != null && dat.length > 0) {
                                // 创建单元格
                                Cell barcodeCell = sheet.createRow(9).createCell(0);
                                // 设置行高足够高以显示条形码
                                sheet.getRow(9).setHeightInPoints(70);
                                // 设置列宽足够宽以显示条形码,先保存当前列宽
                                int currentColumnWidth0 = sheet.getColumnWidth(0);
                                sheet.setColumnWidth(0, 40 * 256);
                                // 创建图片锚点,设置在第10行第1列
                                // 条形码居中且不超出第二个格子
                                // 调整结束列索引和位置参数
                                // 增加dx1值,使条形码整体向右移动
                                ClientAnchor anchor1 = new HSSFClientAnchor(300, 50, 900, 200, (short) 0, 9, (short) 1, 10);
                                // 设置图片位置和大小
                                anchor1.setAnchorType(ClientAnchor.AnchorType.MOVE_DONT_RESIZE);
                                // 添加图片到工作表
                                getDrawingPatriarch(sheet).createPicture(anchor1,
                                        wb.addPicture(dat, getImageType(dat)));
                                // 条形码添加完成后,恢复原来的列宽设置
                                sheet.setColumnWidth(0, currentColumnWidth0);
                            }
@@ -583,6 +590,8 @@
        CellStyle style = wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        // 设置单元格内容自动换行
        style.setWrapText(true);
        style.setBorderRight(BorderStyle.THIN);
        style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        style.setBorderLeft(BorderStyle.THIN);
@@ -597,23 +606,41 @@
        style.setFont(dataFont);
        styles.put("data", style);
        style = wb.createCellStyle();
        style.cloneStyleFrom(styles.get("data"));
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        Font headerFont = wb.createFont();
        headerFont.setFontName("Arial");
        headerFont.setFontHeightInPoints((short) 10);
        headerFont.setBold(true);
        headerFont.setColor(IndexedColors.WHITE.getIndex());
        style.setFont(headerFont);
        styles.put("header", style);
        // 创建表头样式映射,根据Excel注解动态生成
        Map<String, CellStyle> headerStyles = new HashMap<String, CellStyle>();
        for (Object[] os : fields)
        {
            Excel excel = (Excel) os[1];
            String key = StringUtils.format("header_{}_{}", excel.headerColor(), excel.headerBackgroundColor());
            if (!headerStyles.containsKey(key))
            {
                 style = wb.createCellStyle();
                style.cloneStyleFrom(styles.get("data"));
                style.setAlignment(HorizontalAlignment.CENTER);
                style.setVerticalAlignment(VerticalAlignment.CENTER);
                // 设置单元格内容自动换行
                style.setWrapText(true);
                // 根据注解设置表头背景色 - 已注释,表头不显示背景色
                /*if (excel.headerBackgroundColor() != IndexedColors.WHITE) {
                    style.setFillForegroundColor(excel.headerBackgroundColor().index);
                    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                }*/
                Font headerFont = wb.createFont();
                headerFont.setFontName("Arial");
                headerFont.setFontHeightInPoints((short) 10);
                headerFont.setBold(excel.headerFontBold());
                headerFont.setColor(excel.headerColor().index);
                style.setFont(headerFont);
                headerStyles.put(key, style);
            }
        }
        styles.putAll(headerStyles);
        style = wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        // 设置单元格内容自动换行
        style.setWrapText(true);
        Font totalFont = wb.createFont();
        totalFont.setFontName("Arial");
        totalFont.setFontHeightInPoints((short) 10);
@@ -623,16 +650,22 @@
        style = wb.createCellStyle();
        style.cloneStyleFrom(styles.get("data"));
        style.setAlignment(HorizontalAlignment.LEFT);
        // 设置单元格内容自动换行
        style.setWrapText(true);
        styles.put("data1", style);
        style = wb.createCellStyle();
        style.cloneStyleFrom(styles.get("data"));
        style.setAlignment(HorizontalAlignment.CENTER);
        // 设置单元格内容自动换行
        style.setWrapText(true);
        styles.put("data2", style);
        style = wb.createCellStyle();
        style.cloneStyleFrom(styles.get("data"));
        style.setAlignment(HorizontalAlignment.RIGHT);
        // 设置单元格内容自动换行
        style.setWrapText(true);
        styles.put("data3", style);
        return styles;
@@ -647,7 +680,9 @@
        // 写入列信息
        cell.setCellValue(attr.name());
        setDataValidation(attr, row, column);
        cell.setCellStyle(styles.get("header"));
        // 根据Excel注解动态选择表头样式
        String key = StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor());
        cell.setCellStyle(styles.get(key));
        return cell;
    }
@@ -664,7 +699,7 @@
        } else if (Excel.ColumnType.NUMERIC == attr.cellType()) {
            cell.setCellValue(StringUtils.contains(Convert.toStr(value), ".") ? Convert.toDouble(value) : Convert.toInt(value));
        } else if (Excel.ColumnType.IMAGE == attr.cellType()) {
            ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1),
            ClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1),
                    cell.getRow().getRowNum() + 1);
            String imagePath = Convert.toStr(value);
            if (StringUtils.isNotEmpty(imagePath)) {
@@ -711,12 +746,12 @@
        // 如果设置了提示信息则鼠标放上去提示.
        if (StringUtils.isNotEmpty(attr.prompt())) {
            // 这里默认设了2-101列提示.
            setXSSFPrompt(sheet, "", attr.prompt(), 1, 100, column, column);
            setHSSFPrompt(sheet, "", attr.prompt(), 1, 100, column, column);
        }
        // 如果设置了combo属性则本列只能选择不能输入
        if (attr.combo().length > 0) {
            // 这里默认设了2-101列只能选择不能输入.
            setXSSFValidation(sheet, attr.combo(), 1, 100, column, column);
            setHSSFValidation(sheet, attr.combo(), 1, 100, column, column);
        }
    }
@@ -726,15 +761,20 @@
    public Cell addCell(Excel attr, Row row, T vo, Field field, int column) {
        Cell cell = null;
        try {
            // 设置行高
            row.setHeight((short) 2000);
            // 设置行高为自动调整
            row.setHeight((short) -1);
            // 根据Excel中设置情况决定是否导出,有些情况需要保持为空,希望用户填写这一列.
            if (attr.isExport()) {
                // 创建cell
                cell = row.createCell(column);
                //设置cell的宽度
                sheet.setColumnWidth(0, 20 * 256);
                sheet.setColumnWidth(1, 60 * 256); // 第二列宽度15字符
                // 设置列宽,使用@Excel注解中配置的宽度
                if (column == 0) {
                    // 第一列使用固定宽度
                    sheet.setColumnWidth(0, 20 * 256);
                } else if (column == 1) {
                    // 第二列使用@Excel注解中配置的宽度
                    sheet.setColumnWidth(1, (int) ((attr.width() + 0.72) * 256));
                }
                CellStyle style = wb.createCellStyle();
//                style.setAlignment(HorizontalAlignment.RIGHT);
@@ -742,7 +782,7 @@
                style.setVerticalAlignment(VerticalAlignment.CENTER); // 设置垂直居中
                //  style.setAlignment(VerticalAlignment.CENTER);
                Font font = wb.createFont();
                font.setBold(true);
              //  font.setBold(true);
                style.setFont(font);
                style.setWrapText(true); // 设置自动换行
@@ -775,7 +815,7 @@
    }
    /**
     * 设置 POI XSSFSheet 单元格提示
     * 设置 POI HSSFSheet 单元格提示
     *
     * @param sheet         表单
     * @param promptTitle   提示标题
@@ -785,7 +825,7 @@
     * @param firstCol      开始列
     * @param endCol        结束列
     */
    public void setXSSFPrompt(Sheet sheet, String promptTitle, String promptContent, int firstRow, int endRow,
    public void setHSSFPrompt(Sheet sheet, String promptTitle, String promptContent, int firstRow, int endRow,
                              int firstCol, int endCol) {
        DataValidationHelper helper = sheet.getDataValidationHelper();
        DataValidationConstraint constraint = helper.createCustomConstraint("DD1");
@@ -807,7 +847,7 @@
     * @param endCol   结束列
     * @return 设置好的sheet.
     */
    public void setXSSFValidation(Sheet sheet, String[] textlist, int firstRow, int endRow, int firstCol, int endCol) {
    public void setHSSFValidation(Sheet sheet, String[] textlist, int firstRow, int endRow, int firstCol, int endCol) {
        DataValidationHelper helper = sheet.getDataValidationHelper();
        // 加载下拉列表内容
        DataValidationConstraint constraint = helper.createExplicitListConstraint(textlist);
@@ -816,11 +856,9 @@
        // 数据有效性对象
        DataValidation dataValidation = helper.createValidation(constraint, regions);
        // 处理Excel兼容性问题
        if (dataValidation instanceof XSSFDataValidation) {
            dataValidation.setSuppressDropDownArrow(true);
            dataValidation.setShowErrorBox(true);
        } else {
        if (dataValidation instanceof HSSFDataValidation) {
            dataValidation.setSuppressDropDownArrow(false);
            dataValidation.setShowErrorBox(true);
        }
        sheet.addValidationData(dataValidation);
@@ -1017,7 +1055,7 @@
     * 创建一个工作簿
     */
    public void createWorkbook() {
        this.wb = new SXSSFWorkbook(500);
        this.wb = new HSSFWorkbook();
    }
    /**