Java Excel转PDF itextpdf,即取即用
- 工具方法
- 一、使用方式
- 1、本地转换
- 2、网络下载
- 二、pom依赖引入
- 三、工具方法
- 三、引文
本篇主要为工具方法整理,参考学习其他博主文章做了整理,方便使用。
(图片来源网络,侵删)工具方法
一、使用方式
1、本地转换
- 导入依赖
- 创建工具方法
- 传入输入输出流或文档地址即可。
2、网络下载
通过POI或者easyExcel生成或填充,再由后端转换PDF响应前端
思路:将网络下载拆分为本地转换,再响应前端即可。
(图片来源网络,侵删)- 现在服务器创建临时文件目录(临时目录可在每次下载请求开始先进行清空);
- 将生成的Excel写入本地临时文件;
- 获取Excel文件输入流,获取响应的输出流(response.getOutputStream(););
- 调取公共方法传入输入输出流即可。
二、pom依赖引入
4.1.1 5.5.13.2 org.Apache.poi poi ${poi.version} com.itextpdf itextpdf ${itextpdf.version}
三、工具方法
package com.xxx.tool.util; import cn.hutool.core.collection.CollUtil; import com.itextpdf.text.*; 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.poi.hssf.usermodel.*; import org.apache.poi.ooxml.POIXMLDocumentPart; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.*; 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.List; import java.util.Objects; @UtilityClass public class ExcelToPdfUtil { 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) { e.printStackTrace(); } } /** * 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 Row row = sheet.getRow(rowIndex); if (Objects.isNull(row)) { // 插入空对象 for (int i = 0; i 2; } /** * 判断单元格是否是合并单元格 */ private static boolean isMergedRegion(Sheet sheet, int row, int column) { int sheetMergeCount = sheet.getNumMergedRegions(); for (int i = 0; i = firstRow && row if (column = firstColumn && column return true; } } } return false; } /** * 计算合并单元格合并的跨行跨列数 */ private static int[] getMergedSpan(Sheet sheet, int row, int column) { int sheetMergeCount = sheet.getNumMergedRegions(); int[] span = {1, 1}; for (int i = 0; i = Math .abs(_rangeMaxCol + _rangeMinCol - pictureMaxCol - pictureMinCol))); } } /** * 图片基本信息 */ private class PicturesInfo { private int minRow; private int maxRow; private int minCol; private int maxCol; private String ext; private byte[] pictureData; public PicturesInfo() { } public byte[] getPictureData() { return pictureData; } public PicturesInfo setPictureData(byte[] pictureData) { this.pictureData = pictureData; return this; } 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; } } }
三、引文
- java高效实现excel转pdf,支持excel中带有图片的转换(支持.xls和.xlsx两种格式)
- Java Poi 读取excel 对所有类型进行处理