本篇主要为工具方法整理,参考学习其他博主文章做了整理,方便使用。
工具方法
一、使用方式
1、本地转换
- 导入依赖
- 创建工具方法
- 传入输入输出流或文档地址即可。
2、网络下载
通过POI或者easyExcel生成或填充,再由后端转换PDF响应前端
思路:将网络下载拆分为本地转换,再响应前端即可。
- 现在服务器创建临时文件目录(临时目录可在每次下载请求开始先进行清空);
- 将生成的Excel写入本地临时文件;
- 获取Excel文件输入流,获取响应的输出流(response.getOutputStream(););
- 调取公共方法传入输入输出流即可。
二、pom依赖引入
<!-- pom相关依赖 -->
<poi.version>4.1.1</poi.version>
<itextpdf.version>5.5.13.2</itextpdf.version>
<!-- POI Excel-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${
poi.version}</version>
</dependency>
<!-- iText PDF -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>${
itextpdf.version}</version>
</dependency>
三、工具方法
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 <= 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[