【Excel & PDF 系列】EasyExcel + iText 库实现 Excel 转换 PDF

时间:2024-03-02 20:09:40
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.metadata.data.ReadCellData; import com.alibaba.excel.util.ConverterUtils; import com.itextpdf.text.*; import com.itextpdf.text.Font; 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.extern.slf4j.Slf4j; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Slf4j public class NoModelDataListener extends AnalysisEventListener<Map<Integer, String>> { // 存储读取到 excel 的每一行 private List<Map<Integer, String>> cachedDataList = new ArrayList<>(); // 存储读取到 excel 的列头 private Map<Integer, String> cachedHead = new HashMap<>(); //自定义返回结果类,也就是与传递给controller的实体类 ExcelDataVo excelDataVo; //重点:通过构造器把 excelDataVo 对象传递过来 public NoModelDataListener(ExcelDataVo excelDataVo) { this.excelDataVo = excelDataVo; } @Override public void invoke(Map<Integer, String> data, AnalysisContext context) { cachedDataList.add(data); } @Override public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) { cachedHead = ConverterUtils.convertToStringMap(headMap, context); } @Override public void doAfterAllAnalysed(AnalysisContext context) { String pdfFilePath = "D:\\对账明细报告.pdf"; try (FileOutputStream fos = new FileOutputStream(pdfFilePath)) { // 创建PDF文档对象 Document document = new Document(PageSize.A2, 50, 50, 50, 50); // 创建PDF输出流 PdfWriter writer = PdfWriter.getInstance(document, fos); // 打开PDF文档 document.open(); // 创建PDF表格对象 PdfPTable table = new PdfPTable(cachedDataList.get(0).size()); table.setHeaderRows(1); //table.setWidths(new float[] {1, 2, 2, 2}); // 设置表格宽度 table.setWidthPercentage(100); // 设置表格标题 //String sheetName = context.readSheetHolder().getSheetName(); String sheetName = excelDataVo.getTitle(); Paragraph title = new Paragraph(sheetName, new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 16, Font.BOLD)); title.setAlignment(Element.ALIGN_CENTER); document.add(title); // 添加表格标题 for (Map.Entry<Integer, String> entry : cachedHead.entrySet()) { String value = entry.getValue(); PdfPCell pdfCell = new PdfPCell(new Paragraph(value, new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 12))); pdfCell.setBorderWidth(1f); pdfCell.setBorderColor(BaseColor.BLACK); pdfCell.setPadding(5f); pdfCell.setBackgroundColor(BaseColor.LIGHT_GRAY); table.addCell(pdfCell); } // 添加表格内容 for (Map<Integer, String> map : cachedDataList) { for (Map.Entry<Integer, String> entry : map.entrySet()) { PdfPCell pdfCell = new PdfPCell(new Paragraph(entry.getValue())); pdfCell.setBorderWidth(1f); pdfCell.setBorderColor(BaseColor.BLACK); pdfCell.setPadding(5f); table.addCell(pdfCell); } } // 添加表格到PDF文档 table.setSpacingBefore(20f); table.setSpacingAfter(20f); table.setKeepTogether(true); document.add(table); // 关闭PDF文档 document.close(); } catch (IOException | DocumentException e) { e.printStackTrace(); } excelDataVo.setPdfFilePath(pdfFilePath); log.info("所有数据解析完成!"); } }