Java Excel转PDF(免费)

news2024/9/29 15:34:41

目前市面上 Excel 转 PDF 的组件较多:

  • 收费:aspose、GcExcel、spire
  • 开源:jacob、itextpdf

其中收费的组件封装得比较好,代码简洁,转换的效果也很好,但收费也高得离谱:
874a7fbbd006818eb458b5599059775.png
为了成本考虑,就需要考虑开源的组件了,因为它们都是免费的:

  • jacob:目前没有探索出很好的导出效果。
  • itextpdf:已探索出很好的导出效果,达到了与收费组件一致的效果(推荐)。

以下是 itextpdf 的使用步骤:

  1. 引入依赖

    <!-- POI Excel-->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>4.1.1</version>
    </dependency>
    
    <!-- iText PDF -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.5.13.2</version>
    </dependency>
    
  2. 定义图片信息

    import java.io.Serializable;
    
    /**
     * 图片信息
     */
    public class PicturesInfo implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        /**
         * 最小行
         */
        private int minRow;
    
        /**
         * 最大行
         */
        private int maxRow;
    
        /**
         * 最小列
         */
        private int minCol;
    
        /**
         * 最大列
         */
        private int maxCol;
    
        /**
         * 扩展
         */
        private String ext;
    
        /**
         * 图片数据
         */
        private byte[] pictureData;
    
        public int getMinRow() {
            return minRow;
        }
    
        public PicturesInfo setMinRow(int minRow) {
            this.minRow = minRow;
            return this;
        }
    
        public int getMaxRow() {
            return maxRow;
        }
    
        public PicturesInfo setMaxRow(int maxRow) {
            this.maxRow = maxRow;
            return this;
        }
    
        public int getMinCol() {
            return minCol;
        }
    
        public PicturesInfo setMinCol(int minCol) {
            this.minCol = minCol;
            return this;
        }
    
        public int getMaxCol() {
            return maxCol;
        }
    
        public PicturesInfo setMaxCol(int maxCol) {
            this.maxCol = maxCol;
            return this;
        }
    
        public String getExt() {
            return ext;
        }
    
        public PicturesInfo setExt(String ext) {
            this.ext = ext;
            return this;
        }
    
        public byte[] getPictureData() {
            return pictureData;
        }
    
        public PicturesInfo setPictureData(byte[] pictureData) {
            this.pictureData = pictureData;
            return this;
        }
    }
    
  3. 定义工具类

    import cn.hutool.core.collection.CollUtil;
    import cn.hutool.core.util.StrUtil;
    import com.itextpdf.text.BaseColor;
    import com.itextpdf.text.Document;
    import com.itextpdf.text.DocumentException;
    import com.itextpdf.text.Image;
    import com.itextpdf.text.PageSize;
    import com.itextpdf.text.Phrase;
    import com.itextpdf.text.pdf.BaseFont;
    import com.itextpdf.text.pdf.PdfPCell;
    import com.itextpdf.text.pdf.PdfPTable;
    import com.itextpdf.text.pdf.PdfWriter;
    import lombok.experimental.UtilityClass;
    import org.apache.log4j.Logger;
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
    import org.apache.poi.hssf.usermodel.HSSFPicture;
    import org.apache.poi.hssf.usermodel.HSSFPictureData;
    import org.apache.poi.hssf.usermodel.HSSFShape;
    import org.apache.poi.hssf.usermodel.HSSFShapeContainer;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ooxml.POIXMLDocumentPart;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.CellType;
    import org.apache.poi.ss.usermodel.DataFormatter;
    import org.apache.poi.ss.usermodel.DateUtil;
    import org.apache.poi.ss.usermodel.Font;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.ss.util.CellRangeAddress;
    import org.apache.poi.xssf.usermodel.XSSFCell;
    import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
    import org.apache.poi.xssf.usermodel.XSSFDrawing;
    import org.apache.poi.xssf.usermodel.XSSFPicture;
    import org.apache.poi.xssf.usermodel.XSSFPictureData;
    import org.apache.poi.xssf.usermodel.XSSFShape;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Objects;
    import java.util.Set;
    
    /**
     * Excel转PDF
     * @author 廖航
     * @date 2024-08-29 10:52
     */
    @UtilityClass
    public class ExcelToPdfUtil {
    
        /**
         * 日志输出
         */
        private static final Logger logger = Logger.getLogger(ExcelToPdfUtil.class);
    
        /**
         * 单元格队列
         */
        Set<String> cellSet = new HashSet<>();
    
        /**
         * Excel转PDF
         * 
         * @param excelPath Excel文件路径
         * @param pdfPath PDF文件路径
         * @param excelSuffix Excel文件后缀
         */
        public static void excelToPdf(String excelPath, String pdfPath, String excelSuffix) {
            try (InputStream in = Files.newInputStream(Paths.get(excelPath));
                 OutputStream out = Files.newOutputStream(Paths.get(pdfPath))) {
                ExcelToPdfUtil.excelToPdf(in, out, excelSuffix);
            } catch (Exception e) {
                logger.error(e.getMessage());
            }
        }
    
        /**
         * Excel转PDF并写入输出流
         *
         * @param inStream    Excel输入流
         * @param outStream   PDF输出流
         * @param excelSuffix Excel类型 .xls 和 .xlsx
         * @throws Exception 异常信息
         */
        public static void excelToPdf(InputStream inStream, OutputStream outStream, String excelSuffix) throws Exception {
            // 输入流转workbook,获取sheet
            Sheet sheet = getPoiSheetByFileStream(inStream, 0, excelSuffix);
            // 获取列宽度占比
            float[] widths = getColWidth(sheet);
            PdfPTable table = new PdfPTable(widths);
            table.setWidthPercentage(100);
            int colCount = widths.length;
            //设置基本字体
            BaseFont baseFont = BaseFont.createFont("C:\\Windows\\Fonts\\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            // 遍历行
            for (int rowIndex = sheet.getFirstRowNum(); rowIndex <= sheet.getLastRowNum(); rowIndex++) {
                Row row = sheet.getRow(rowIndex);
                if (Objects.isNull(row)) {
                    // 插入空对象
                    for (int i = 0; i < colCount; i++) {
                        table.addCell(createPdfPCell(null, 0, 13f, null));
                    }
                } else {
                    // 遍历单元格
                    for (int columnIndex = 0; (columnIndex < row.getLastCellNum() || columnIndex < colCount) && columnIndex > -1; columnIndex++) {
                        PdfPCell pCell = excelCellToPdfCell(sheet, row.getCell(columnIndex), baseFont);
                        // 是否合并单元格
                        if (isMergedRegion(sheet, rowIndex, columnIndex)) {
                            int[] span = getMergedSpan(sheet, rowIndex, columnIndex);
                            //忽略合并过的单元格
                            boolean mergedCell = span[0] == 1 && span[1] == 1;
                            if (mergedCell) {
                                continue;
                            }
                            pCell.setRowspan(span[0]);
                            pCell.setColspan(span[1]);
                        }
                        table.addCell(pCell);
                    }
                }
            }
            // 初始化PDF文档对象
            createPdfTableAndWriteDocument(outStream, table);
        }
    
        /**
         * 单元格转换,poi cell 转换为 itext cell
         *
         * @param sheet     poi sheet页
         * @param excelCell poi 单元格
         * @param baseFont  基础字体
         * @return PDF单元格
         */
        private static PdfPCell excelCellToPdfCell(Sheet sheet, Cell excelCell, BaseFont baseFont) throws Exception {
            if (Objects.isNull(excelCell)) {
                return createPdfPCell(null, 0, 13f, null);
            }
            int rowIndex = excelCell.getRowIndex();
            int columnIndex = excelCell.getColumnIndex();
            // 图片信息
            List<PicturesInfo> infos = getAllPictureInfos(sheet, rowIndex, rowIndex, columnIndex, columnIndex, false);
            PdfPCell pCell;
            if (CollUtil.isNotEmpty(infos)) {
                Image image = Image.getInstance(infos.get(0).getPictureData());
                // 调整图片大小
            	image.scaleAbsolute(527, 215);
                pCell = new PdfPCell(image);
            } else {
                Font excelFont = getExcelFont(sheet, excelCell);
                //设置单元格字体
                com.itextpdf.text.Font pdFont = new com.itextpdf.text.Font(baseFont, excelFont.getFontHeightInPoints(), excelFont.getBold() ? 1 : 0, BaseColor.BLACK);
                Integer border = hasBorder(excelCell) ? null : 0;
                String excelCellValue = getExcelCellValue(excelCell);
                pCell = createPdfPCell(excelCellValue, border, excelCell.getRow().getHeightInPoints(), pdFont);
            }
            // 水平居中
            pCell.setHorizontalAlignment(getHorAlign(excelCell.getCellStyle().getAlignment().getCode()));
            // 垂直对齐
            pCell.setVerticalAlignment(getVerAlign(excelCell.getCellStyle().getVerticalAlignment().getCode()));
            return pCell;
        }
    
        /**
         * 创建pdf文档,并添加表格
         *
         * @param outStream 输出流,目标文档
         * @param table     表格
         * @throws DocumentException 异常信息
         */
        private static void createPdfTableAndWriteDocument(OutputStream outStream, PdfPTable table) throws DocumentException {
            //设置pdf纸张大小 PageSize.A4 A4横向
            Document document = new Document(PageSize.B0);
            PdfWriter.getInstance(document, outStream);
            //设置页边距 宽
            document.setMargins(10, 10, 10, 10);
            document.open();
            document.add(table);
            document.close();
        }
    
        /**
         * Excel文档输入流转换为对应的workbook及获取对应的sheet
         *
         * @param inputStream Excel文档输入流
         * @param sheetNo     sheet编号,默认0 第一个sheet
         * @param excelSuffix 文件类型 .xls和.xlsx
         * @return poi sheet
         * @throws IOException 异常
         */
        public static Sheet getPoiSheetByFileStream(InputStream inputStream, int sheetNo, String excelSuffix) throws IOException {
            Workbook workbook;
            if (excelSuffix.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook(inputStream);
            } else {
                workbook = new HSSFWorkbook(inputStream);
            }
            return workbook.getSheetAt(sheetNo);
        }
    
        /**
         * 创建itext pdf 单元格
         *
         * @param content       单元格内容
         * @param border        边框
         * @param minimumHeight 高度
         * @param pdFont        字体
         * @return pdf cell
         */
        private static PdfPCell createPdfPCell(String content, Integer border, Float minimumHeight, com.itextpdf.text.Font pdFont) {
            String contentValue = content == null ? "" : content;
            com.itextpdf.text.Font pdFontNew = pdFont == null ? new com.itextpdf.text.Font() : pdFont;
            PdfPCell pCell = new PdfPCell(new Phrase(contentValue, pdFontNew));
            if (Objects.nonNull(border)) {
                pCell.setBorder(border);
            }
            if (Objects.nonNull(minimumHeight)) {
                pCell.setMinimumHeight(minimumHeight);
            }
    
            return pCell;
        }
    
        /**
         * excel垂直对齐方式映射到pdf对齐方式
         * 
         * @param align 对齐
         * @return 结果
         */
        private static int getVerAlign(int align) {
            switch (align) {
                case 2:
                    return com.itextpdf.text.Element.ALIGN_BOTTOM;
                case 3:
                    return com.itextpdf.text.Element.ALIGN_TOP;
                default:
                    return com.itextpdf.text.Element.ALIGN_MIDDLE;
            }
        }
    
        /**
         * excel水平对齐方式映射到pdf水平对齐方式
         * 
         * @param align 对齐
         * @return 结果
         */
        private static int getHorAlign(int align) {
            switch (align) {
                case 1:
                    return com.itextpdf.text.Element.ALIGN_LEFT;
                case 3:
                    return com.itextpdf.text.Element.ALIGN_RIGHT;
                default:
                    return com.itextpdf.text.Element.ALIGN_CENTER;
            }
        }
    
        /*============================================== POI获取图片及文本内容工具方法 ==============================================*/
    
        /**
         * 获取字体
         *
         * @param sheet excel 转换的sheet页
         * @param cell  单元格
         * @return 字体
         */
        private static Font getExcelFont(Sheet sheet, Cell cell) {
            // xls
            if (sheet instanceof HSSFSheet) {
                Workbook workbook = sheet.getWorkbook();
                return ((HSSFCell) cell).getCellStyle().getFont(workbook);
            }
            // xlsx
            return ((XSSFCell) cell).getCellStyle().getFont();
        }
    
        /**
         * 判断excel单元格是否有边框
         * 
         * @param excelCell 单元格
         * @return 结果
         */
        private static boolean hasBorder(Cell excelCell) {
            short top = excelCell.getCellStyle().getBorderTop().getCode();
            short bottom = excelCell.getCellStyle().getBorderBottom().getCode();
            short left = excelCell.getCellStyle().getBorderLeft().getCode();
            short right = excelCell.getCellStyle().getBorderRight().getCode();
            return top + bottom + left + right > 2;
        }
    
        /**
         * 判断单元格是否是合并单元格
         * 
         * @param sheet 表
         * @param row 行
         * @param column 列
         * @return 结果
         */
        private static boolean isMergedRegion(Sheet sheet, int row, int column) {
            int sheetMergeCount = sheet.getNumMergedRegions();
            for (int i = 0; i < sheetMergeCount; i++) {
                CellRangeAddress range = sheet.getMergedRegion(i);
                int firstColumn = range.getFirstColumn();
                int lastColumn = range.getLastColumn();
                int firstRow = range.getFirstRow();
                int lastRow = range.getLastRow();
                if (row >= firstRow && row <= lastRow) {
                    if (column >= firstColumn && column <= lastColumn) {
                        return true;
                    }
                }
            }
            return false;
        }
    
        /**
         * 计算合并单元格合并的跨行跨列数
         * 
         * @param sheet 表
         * @param row 行
         * @param column 列
         * @return 结果
         */
        private static int[] getMergedSpan(Sheet sheet, int row, int column) {
            int sheetMergeCount = sheet.getNumMergedRegions();
            int[] span = {1, 1};
            for (int i = 0; i < sheetMergeCount; i++) {
                CellRangeAddress range = sheet.getMergedRegion(i);
                int firstColumn = range.getFirstColumn();
                int lastColumn = range.getLastColumn();
                int firstRow = range.getFirstRow();
                int lastRow = range.getLastRow();
                if (firstColumn == column && firstRow == row) {
                    span[0] = lastRow - firstRow + 1;
                    span[1] = lastColumn - firstColumn + 1;
                    break;
                }
            }
            return span;
        }
    
        /**
         * 获取excel中每列宽度的占比
         * 
         * @param sheet 表
         * @return 结果
         */
        private static float[] getColWidth(Sheet sheet) {
            int rowNum = getMaxColRowNum(sheet);
            Row row = sheet.getRow(rowNum);
            int cellCount = row.getPhysicalNumberOfCells();
            int[] colWidths = new int[cellCount];
            int sum = 0;
    
            for (int i = row.getFirstCellNum(); i < cellCount; i++) {
                Cell cell = row.getCell(i);
                if (cell != null) {
                    colWidths[i] = sheet.getColumnWidth(i);
                    sum += sheet.getColumnWidth(i);
                }
            }
    
            float[] colWidthPer = new float[cellCount];
            for (int i = row.getFirstCellNum(); i < cellCount; i++) {
                colWidthPer[i] = (float) colWidths[i] / sum * 100;
            }
            return colWidthPer;
        }
    
        /**
         * 获取excel中列数最多的行号
         * 
         * @param sheet 表
         * @return 结果
         */
        private static int getMaxColRowNum(Sheet sheet) {
            int rowNum = 0;
            int maxCol = 0;
            for (int r = sheet.getFirstRowNum(); r < sheet.getPhysicalNumberOfRows(); r++) {
                Row row = sheet.getRow(r);
                if (row != null && maxCol < row.getPhysicalNumberOfCells()) {
                    maxCol = row.getPhysicalNumberOfCells();
                    rowNum = r;
                }
            }
            return rowNum;
        }
    
        /**
         * poi 根据单元格类型获取单元格内容
         *
         * @param excelCell poi单元格
         * @return 单元格内容文本
         */
        public static String getExcelCellValue(Cell excelCell) {
            if (excelCell == null) {
                return "";
            }
            // 判断数据的类型
            CellType cellType = excelCell.getCellType();
    
            if (cellType == CellType.STRING) {
                return excelCell.getStringCellValue();
            }
            if (cellType == CellType.BOOLEAN) {
                return String.valueOf(excelCell.getBooleanCellValue());
            }
            if (cellType == CellType.FORMULA) {
                return excelCell.getCellFormula();
            }
            if (cellType == CellType.NUMERIC) {
                // 处理日期格式、时间格式
                if (DateUtil.isCellDateFormatted(excelCell)) {
                    SimpleDateFormat sdf;
                    // 验证short值
                    if (excelCell.getCellStyle().getDataFormat() == 14) {
                        sdf = new SimpleDateFormat("yyyy/MM/dd");
                    } else if (excelCell.getCellStyle().getDataFormat() == 21) {
                        sdf = new SimpleDateFormat("HH:mm:ss");
                    } else if (excelCell.getCellStyle().getDataFormat() == 22) {
                        sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                    } else {
                        throw new RuntimeException("日期格式错误!!!");
                    }
                    Date date = excelCell.getDateCellValue();
                    return sdf.format(date);
                } else if (excelCell.getCellStyle().getDataFormat() == 0) {
                    //处理数值格式
                    DataFormatter formatter = new DataFormatter();
                    return formatter.formatCellValue(excelCell);
                }
            }
            if (cellType == CellType.ERROR) {
                return "非法字符";
            }
            return "";
        }
    
        /**
         * 获取sheet内的所有图片信息
         *
         * @param sheet        sheet表
         * @param onlyInternal 单元格内部
         * @return 照片集合
         * @throws Exception 异常
         */
        public static List<PicturesInfo> getAllPictureInfos(Sheet sheet, boolean onlyInternal) throws Exception {
            return getAllPictureInfos(sheet, null, null, null, null, onlyInternal);
        }
    
        /**
         * 根据sheet和单元格信息获取图片
         *
         * @param sheet        sheet表
         * @param minRow       最小行
         * @param maxRow       最大行
         * @param minCol       最小列
         * @param maxCol       最大列
         * @param onlyInternal 是否内部
         * @return 图片集合
         * @throws Exception 异常
         */
        public static List<PicturesInfo> getAllPictureInfos(Sheet sheet, Integer minRow, Integer maxRow, Integer minCol,
                                                            Integer maxCol, boolean onlyInternal) throws Exception {
            if (sheet instanceof HSSFSheet) {
                return getXLSAllPictureInfos((HSSFSheet) sheet, minRow, maxRow, minCol, maxCol, onlyInternal);
            } else if (sheet instanceof XSSFSheet) {
                return getXLSXAllPictureInfos((XSSFSheet) sheet, minRow, maxRow, minCol, maxCol, onlyInternal);
            } else {
                throw new Exception("未处理类型,没有为该类型添加:GetAllPicturesInfos()扩展方法!");
            }
        }
    
        /**
         * 获取XLS图片信息
         * 
         * @param sheet 表
         * @param minRow 最小行
         * @param maxRow 最大行
         * @param minCol 最小列
         * @param maxCol 最大列
         * @param onlyInternal 只在内部
         * @return 图片信息列表
         */
        private static List<PicturesInfo> getXLSAllPictureInfos(HSSFSheet sheet, Integer minRow, Integer maxRow,
                                                                Integer minCol, Integer maxCol, Boolean onlyInternal) {
            List<PicturesInfo> picturesInfoList = new ArrayList<>();
            HSSFShapeContainer shapeContainer = sheet.getDrawingPatriarch();
            if (shapeContainer == null) {
                return picturesInfoList;
            }
            List<HSSFShape> shapeList = shapeContainer.getChildren();
            for (HSSFShape shape : shapeList) {
                if (shape instanceof HSSFPicture && shape.getAnchor() instanceof HSSFClientAnchor) {
                    HSSFPicture picture = (HSSFPicture) shape;
                    HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor();
    
                    if (isInternalOrIntersect(minRow, maxRow, minCol, maxCol, anchor.getRow1(), anchor.getRow2(),
                            anchor.getCol1(), anchor.getCol2(), onlyInternal)) {
                        String item = StrUtil.format("{},{},{},{}", anchor.getRow1(), anchor.getRow2(), anchor.getCol1(), anchor.getCol2());
                        if (cellSet.contains(item)) {
                            continue;
                        }
                        cellSet.add(item);
                        HSSFPictureData pictureData = picture.getPictureData();
                        picturesInfoList.add(new PicturesInfo()
                                .setMinRow(anchor.getRow1())
                                .setMaxRow(anchor.getRow2())
                                .setMinCol(anchor.getCol1())
                                .setMaxCol(anchor.getCol2())
                                .setPictureData(pictureData.getData())
                                .setExt(pictureData.getMimeType()));
                    }
                }
            }
            return picturesInfoList;
        }
    
        /**
         * 获取XLSX图片信息
         * 
         * @param sheet 表
         * @param minRow 最小行
         * @param maxRow 最大行
         * @param minCol 最小列
         * @param maxCol 最大列
         * @param onlyInternal 只在内部
         * @return 图片信息列表
         */
        private static List<PicturesInfo> getXLSXAllPictureInfos(XSSFSheet sheet, Integer minRow, Integer maxRow,
                                                                 Integer minCol, Integer maxCol, Boolean onlyInternal) {
            List<PicturesInfo> picturesInfoList = new ArrayList<>();
    
            List<POIXMLDocumentPart> documentPartList = sheet.getRelations();
            for (POIXMLDocumentPart documentPart : documentPartList) {
                if (documentPart instanceof XSSFDrawing) {
                    XSSFDrawing drawing = (XSSFDrawing) documentPart;
                    List<XSSFShape> shapes = drawing.getShapes();
                    for (XSSFShape shape : shapes) {
                        if (shape instanceof XSSFPicture) {
                            XSSFPicture picture = (XSSFPicture) shape;
                            XSSFClientAnchor anchor = picture.getPreferredSize();
    
                            if (isInternalOrIntersect(minRow, maxRow, minCol, maxCol, anchor.getRow1(), anchor.getRow2(),
                                    anchor.getCol1(), anchor.getCol2(), onlyInternal)) {
                                String item = StrUtil.format("{},{},{},{}", anchor.getRow1(), anchor.getRow2(), anchor.getCol1(), anchor.getCol2());
                                if (cellSet.contains(item)) {
                                    continue;
                                }
                                cellSet.add(item);
                                XSSFPictureData pictureData = picture.getPictureData();
                                picturesInfoList.add(new PicturesInfo()
                                        .setMinRow(anchor.getRow1())
                                        .setMaxRow(anchor.getRow2())
                                        .setMinCol(anchor.getCol1())
                                        .setMaxCol(anchor.getCol2())
                                        .setPictureData(pictureData.getData())
                                        .setExt(pictureData.getMimeType()));
                            }
                        }
                    }
                }
            }
    
            return picturesInfoList;
        }
    
        /**
         * 是内部的或相交的
         * 
         * @param rangeMinRow 最小行范围
         * @param rangeMaxRow 最大行范围
         * @param rangeMinCol 最小列范围
         * @param rangeMaxCol 最大列范围
         * @param pictureMinRow 图片最小行
         * @param pictureMaxRow 图片最大行
         * @param pictureMinCol 图片最小列
         * @param pictureMaxCol 图片最大列
         * @param onlyInternal 只在内部
         * @return 结果
         */
        private static boolean isInternalOrIntersect(Integer rangeMinRow, Integer rangeMaxRow, Integer rangeMinCol,
                                                     Integer rangeMaxCol, int pictureMinRow, int pictureMaxRow, int pictureMinCol, int pictureMaxCol,
                                                     Boolean onlyInternal) {
            int _rangeMinRow = rangeMinRow == null ? pictureMinRow : rangeMinRow;
            int _rangeMaxRow = rangeMaxRow == null ? pictureMaxRow : rangeMaxRow;
            int _rangeMinCol = rangeMinCol == null ? pictureMinCol : rangeMinCol;
            int _rangeMaxCol = rangeMaxCol == null ? pictureMaxCol : rangeMaxCol;
    
            if (onlyInternal) {
                return (_rangeMinRow <= pictureMinRow && _rangeMaxRow >= pictureMaxRow && _rangeMinCol <= pictureMinCol
                        && _rangeMaxCol >= pictureMaxCol);
            } else {
                return ((Math.abs(_rangeMaxRow - _rangeMinRow) + Math.abs(pictureMaxRow - pictureMinRow) >= Math
                        .abs(_rangeMaxRow + _rangeMinRow - pictureMaxRow - pictureMinRow))
                        && (Math.abs(_rangeMaxCol - _rangeMinCol) + Math.abs(pictureMaxCol - pictureMinCol) >= Math
                        .abs(_rangeMaxCol + _rangeMinCol - pictureMaxCol - pictureMinCol)));
            }
        }
    }
    
  4. 调用工具类

    ExcelToPdfUtil.excelToPdf("原始的Excel文件", "要导出的PDF文件", ".xlsx");
    

如此,即可很好的实现 Excel 转 PDF。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2087985.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

共享单车|基于SprinBoot+vue的共享单车数据储存系统(源码+数据库+文档)

共享单车数据储存系统 基于SprinBootvue的共享单车数据储存系统 一、前言 二、系统设计 三、系统功能设计 系统登录注册实现 管理员模块实现 用户模块实现 四、数据库设计 五、核心代码 六、论文参考 七、最新计算机毕设选题推荐 八、源码获取&#xff1a; 博主介…

STM32和TMSF28335定时器的系统时钟问题

用户可通过多个预分频器配置AHB、高速APB(APB2)和低速APB(APB1)域的频率。AHB和APB2域的最大频率是72MHz。APB1域的最大允许频率是36MHz。 定时器时钟频率分配由硬件按以下2种情况自动设置&#xff1a; 如果相应的APB预分频系数是1&#xff0c;定时器的时钟频率与所在APB总线…

俄罗斯OZON真的适合小白做吗,有哪些坑需要知道

OZON 对于小白来说有一定的可行性&#xff0c;但也存在一些需要注意的 “坑”&#xff1a; OZON 平台适合小白的原因&#xff1a; 入驻门槛较低&#xff1a;目前中国卖家入驻 OZON 是免费的&#xff0c;具备企业营业执照&#xff08;暂不支持个体工商户注册&#xff09;&#…

Part4-DOM学习笔记-获取元素属性及节点操作

6.获取元素属性 6.1 获取元素属性 获取元素的属性有两种方式&#xff1a; element.属性&#xff1a; 获取内置属性值&#xff0c;元素本身自带的属性 不能获取自定义属性 代码示例如 console.log(div.id)element.getAttribute(‘属性’)&#xff1a; 可以获取内置属性值 可…

高带宽云服务器有什么用处?这几种用处要知道

高带宽云服务器的主要用途包括提供更快的网络连接速度、支持高并发访问和大规模数据传输、提高应用程序的性能和用户体验等。在当今数字化时代&#xff0c;随着大数据、云计算、流媒体等技术的飞速发展&#xff0c;高带宽云服务器已成为许多企业和个人用户的首选。以下将详细分…

C++ PCL求解法向量及可视化

【版权声明】本文为博主原创文章&#xff0c;未经博主允许严禁转载&#xff0c;我们会定期进行侵权检索。 参考书籍&#xff1a;《人工智能点云处理及深度学习算法》 本文为专栏《Python三维点云实战宝典》系列文章&#xff0c;专栏介绍地址“【python三维深度学习】python…

C语言sprintf函数使用

1 其函数原型为&#xff1a;int sprintf(char *str, const char *format,...)。 具体用法如下&#xff1a; 基本语法&#xff1a; str&#xff1a;目标字符串的指针&#xff0c;用于存储格式化后的结果。format&#xff1a;格式化字符串&#xff0c;用于指定输出的格式。后续是…

IP地址SSL证书要怎么申请?

申请IP地址SSL证书的过程可以简化为以下四个步骤&#xff1a; 1. 选择证书提供商 确定需求&#xff1a;首先&#xff0c;明确你的需求&#xff0c;包括所需的证书类型&#xff08;如IP地址证书&#xff09;和加密算法等。选择CA机构&#xff1a;选择一个受信任的证书颁发机构…

有了这款低代码工具,从此让你告别烦人的996报表制作模式

一、低代码平台兴起的背景 近年来&#xff0c;低代码与零代码平台的兴起&#xff0c;无疑是IT领域的一股强劲风潮&#xff0c;它们依托互联网技术的飞速进步&#xff0c;致力于简化软件开发流程&#xff0c;推动工具向更加易用、高效的方向演进。在这一浪潮中&#xff0c;尽管…

重塑工程机械设备市场:创新网络推广方案引领潮流

在重塑工程机械设备领域&#xff0c;网络营销推广已日益凸显为企业拓展市场版图、提升品牌知名度不可或缺的关键手段。鉴于互联网时代的海量信息与激烈竞争&#xff0c;关键词搜索排名优化更是成为了衡量网络推广成效的重要标尺。为了在这场数字战役中脱颖而出&#xff0c;我们…

二、测试的基本概念

文章目录 一、需求&#xff08;一&#xff09;需求的概念&#xff08;二&#xff09;用户需求&#xff08;三&#xff09;软件需求 二、从软件测试人员角度看需求&#xff08;一&#xff09;为什么需求对软件测试人员如此重要&#xff08;二&#xff09;如何才可以深入理解被测…

【论文速读】| ARVO: 开源软件可重现漏洞的全景图

本次分享论文&#xff1a;ARVO: Atlas of Reproducible Vulnerabilities for Open Source Software 基本信息 原文作者&#xff1a;Xiang Mei, Pulkit Singh Singaria, Jordi Del Castillo, Haoran Xi, Abdelouahab (Habs) Benchikh, Tiffany Bao, Ruoyu Wang, Yan Shoshitai…

Java Email发送:如何配置SMTP服务器发信?

Java Email发送性能如何优化&#xff1f;怎么实现Java发信功能&#xff1f; Java Email发送功能使得开发者能够轻松集成邮件服务到他们的应用程序中。通过配置SMTP服务器&#xff0c;Java Email发送可以实现高效、可靠的邮件传递。AokSend将详细介绍如何配置SMTP服务器以进行J…

10、ollama启动LLama_Factory微调大模型(llama.cpp)

在前面章节中介绍了如何使用LLama_Factory微调大模型&#xff0c;并将微调后的模型文件合并导出&#xff0c;本节我们我们看下如何使用ollama进行调用。 1、llama.cpp LLama_Factory训练好的模型&#xff0c;ollama不能直接使用&#xff0c;需要转换一下格式&#xff0c;我们…

Java面试——Spring

优质博文&#xff1a;IT-BLOG-CN 一、Spring Bean 作用域 【1】singleton&#xff1a;该属性在 IOC容器仅创建一个 Bean实例&#xff08;单例&#xff09;&#xff0c;IOC容器每次返回的是同一个 Bean实例。 【2】prototype&#xff1a;该属性在 IOC容器可以创建多个 Bean实例…

MT3608L 2.5A,高效率1.2MHz电流模式升压转换器芯片IC

一般描述 MT3608L是一款恒频、6针SOT23电流模式升压转换器&#xff0c;适用于小型、低功率应用。MT3608L开关频率为1.2 MHz&#xff0c;允许使用高度小于2mm的微型、低成本电容器和电感器。内部软启动可产生小浪涌电流&#xff0c;延长电池寿命。 MT3608L具有在…

事半功倍:利用增强现实提高工作效率

人们通常认为增强现实只是游戏中的一个强大的功能&#xff0c;然而&#xff0c;研究表明&#xff0c;增强现实在提高工厂的效率和生产力方面也发挥着重要作用。不管增强现实、虚拟现实还是混合现实都能很好地模拟工厂的工作场景&#xff0c;这对于培训、运营、安全和研发方面的…

ip地址暴露有什么危害?怎么隐藏ip地址

在数字化时代&#xff0c;‌IP地址作为网络世界的“门牌号”&#xff0c;‌扮演着连接用户与互联网的重要角色。‌然而&#xff0c;‌随着网络环境的日益复杂&#xff0c;‌IP地址的暴露可能成为个人隐私泄露和网络攻击的风向标。‌本文将深入探讨IP地址暴露的危害&#xff0c;…

【pgAdmin4】Windows 上安装 PostgreSQL(图文详情)

目录 0.背景 1.简介 2.安装步骤 1&#xff09;下载地址 2&#xff09;双击安装包进行下载【请以管理员身份打开并安装】 3&#xff09;安装步骤 4&#xff09;安装中 5&#xff09;安装完成 0.背景 最近在学习大数据规模处理的相关课程&#xff0c;需要安装这个数据库以…

快速安全部署 Tomcat

主机环境 IP域名用途172.25.254.10tomcat1.orgTomcat172.25.254.20tomcat2.orgTomcat172.25.254.30nginx-server.orgNginx 负载均衡 Tomcat 安装 172.25.254.10 # 安装JAVA [roottomcat1 ~]# yum install java-1.8.0-openjdk.x86_64 -y # 下载Tomcat,推荐在Windows主机下载…