餐饮网站网页设计代码微信推广平台哪里找
接上篇,给出了完整的 jodconverter 基于 OpenOffice 转换 Excel 为 PDF 的代码,并且示例了如何自定义 PDF 的页面大小。
本文更完美的实现:多列 Excel 在转换为 PDF 后如何不折行的问题(也就是所有列都显示在一页中)
思路和方法:
如下图所示,我们只需要将 Excel 的打印缩放选项设置为 “将所有列调整为一页” 即可!
对于如何设置这个选项本文给出代码,如下:
本文只给出相关代码,整体工程完整运行的代码,请详见上一篇文章
相比上一篇文章,将下面的类 JodConvertServiceImpl 覆盖上一篇文章的这个类即可(主要在接口中添加新的方法并修改 Controller 中调用新的方法)
JodConvertServiceImpl.java
package com.example.office2pdf.office2pdf.service.impl;import com.example.office2pdf.office2pdf.service.JodConvertService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jodconverter.core.DocumentConverter;
import org.jodconverter.core.office.OfficeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;/*** Office转换为PDF** @author shanhy* @date 2020/12/16 16:58*/
@Service
@Slf4j
public class JodConvertServiceImpl implements JodConvertService {/*** 转换器注入*/@Autowiredprivate DocumentConverter converter;@Overridepublic boolean convert(File sourceFile, File targetFile) {try {converter.convert(sourceFile).to(targetFile).execute();return true;} catch (OfficeException e) {log.error("转换office文档失败", e);}return false;}@Overridepublic boolean convertExcelToPDFByFitColumn(File sourceFile, File targetFile) {String uuid = UUID.randomUUID().toString();File tempExcel = new File(sourceFile.getParentFile(), uuid.concat("_").concat(sourceFile.getName()));try {setExcelPrintParameter(sourceFile, tempExcel);return this.convert(tempExcel, targetFile);} catch (IOException | InvalidFormatException e) {log.error("转换office文档失败", e);} finally {if (tempExcel.exists() && !tempExcel.delete())log.warn("删除临时文件失败 file={}", tempExcel.getPath());}return false;}/*** 设置Excel打印参数** @param sourceFile* @param targetFile* @throws IOException* @throws InvalidFormatException*/private void setExcelPrintParameter(File sourceFile, File targetFile) throws IOException, InvalidFormatException {Workbook workbook = new XSSFWorkbook(sourceFile);for (int i = 0, j = workbook.getNumberOfSheets(); i < j; i++) {Sheet sheet = workbook.getSheetAt(i);sheet.getPrintSetup().setPaperSize(PrintSetup.A4_PAPERSIZE);// FitHeight=1, 将所有行都缩放显示在一页上(设置1表示一页显示完,如果设置2表示分2页显示完)// FitWidth=1, 将所有列都缩放显示在一页上// 两个都等于1时,如果行数太多则会挤压列,一般来说只设置一个FitWidth=1,让行数自动换页// 要使这两个参数有效,则需要设置FitToPage=truesheet.setFitToPage(true);sheet.getPrintSetup().setFitWidth((short) 1);
// sheet.getPrintSetup().setFitHeight((short)1);// 是否显示自动换页符sheet.setAutobreaks(true);}try (FileOutputStream out = new FileOutputStream(targetFile)) {workbook.write(out);workbook.close();}}}
本文方案不使用 Filter 过滤器,所以相比上一篇文章的代码 JodConverterConfiguration
,需要注释掉过滤器,如下:
@Configuration
public class JodConverterConfiguration {@BeanDocumentConverter localDocumentConverter(OfficeManager localOfficeManager, DocumentFormatRegistry documentFormatRegistry) {return LocalConverter.builder().officeManager(localOfficeManager).formatRegistry(documentFormatRegistry).build();}}
整体思路就是先输出一个修改参数的Excel,然后基于设置了打印参数的新Excel 去生成 PDF。
注意 XSSFWorkbook 和 HSSFWorkbook 的区别,后者是针对 2007 之前的 xls,所以结合实际情况,方法 setExcelPrintParameter 中的代码你可能需要修改一下。
已经过测试,完美,请放心使用。
(END)