Java+LibreOffice實現(xiàn)Excel轉(zhuǎn)PDF并橫向一頁顯示所有列
背景需求
在實際業(yè)務(wù)場景中,用戶往往會提供格式不一的 Excel 文件(尤其列非常多),希望將其轉(zhuǎn)換為 PDF 并橫向顯示,所有列壓縮在一頁內(nèi)。
用戶不會手動設(shè)置打印參數(shù),因此希望通過 Java 代碼實現(xiàn)自動化轉(zhuǎn)換,保證視覺效果統(tǒng)一。
技術(shù)方案概覽
技術(shù)棧
工具 | 用途 |
---|---|
Apache POI | 修改 Excel 頁設(shè)置(橫向、一頁寬) |
LibreOffice | 使用 headless 模式導(dǎo)出 PDF |
Java | 實現(xiàn)邏輯控制和流程管理 |
頁面設(shè)置關(guān)鍵代碼
Apache POI 5.2.5
import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public static void adjustExcelPageSetup(String inputPath, String tempPath) throws IOException { FileInputStream fis = new FileInputStream(inputPath); Workbook workbook = new XSSFWorkbook(fis); for (int i = 0; i < workbook.getNumberOfSheets(); i++) { Sheet sheet = workbook.getSheetAt(i); PrintSetup printSetup = sheet.getPrintSetup(); printSetup.setLandscape(true); // 橫向打印 sheet.setAutobreaks(true); // 自動分頁 sheet.setFitToPage(true); // 啟用適應(yīng)頁面 printSetup.setFitWidth((short) 1); // 一頁寬度 printSetup.setFitHeight((short) 0); // 高度不限(0 = 自動) } FileOutputStream fos = new FileOutputStream(tempPath); workbook.write(fos); workbook.close(); fis.close(); fos.close(); }
LibreOffice 命令行調(diào)用
public static void convertToPdf(String libreOfficePath, String inputPath, String outputDir) throws IOException, InterruptedException { List<String> command = Arrays.asList( libreOfficePath, “–headless”, “–norestore”, “–convert-to”, “pdf”, “–outdir”, outputDir, inputPath ); ProcessBuilder pb = new ProcessBuilder(command); pb.inheritIO(); Process process = pb.start(); int exitCode = process.waitFor(); if (exitCode == 0) { System.out.println("轉(zhuǎn)換成功: " + inputPath); } else { System.err.println("轉(zhuǎn)換失敗: " + inputPath); } }
常見問題 FAQ
1.setFitToWidth() 報錯:方法不存在?
是早期示例誤導(dǎo),正確方法是:
printSetup.setFitWidth((short) 1); printSetup.setFitHeight((short) 0);
2.temp_wide_excel.xlsx 是否需要預(yù)創(chuàng)建?
不需要,只要目錄存在,程序會自動創(chuàng)建并寫入該文件。
3.文件路徑有空格導(dǎo)致 LibreOffice 轉(zhuǎn)換失???
請使用 “路徑” 包含引號或使用 new File(path).getAbsolutePath() 避免錯誤。
4.Excel 很寬時 PDF 仍分頁?
請務(wù)必:
使用 printSetup.setFitWidth((short) 1) 設(shè)置一頁寬
啟用 sheet.setFitToPage(true)
使用 LibreOffice 轉(zhuǎn)換前,先保存好設(shè)置
完整流程
接收原始 Excel 文件(.xlsx)
使用 Apache POI 設(shè)置打印參數(shù)(橫向、一頁寬)
輸出為臨時文件(如 temp_wide_excel.xlsx)
使用 LibreOffice 命令行導(dǎo)出 PDF
輸出 PDF 橫向顯示、列不分頁
示例目錄結(jié)構(gòu)
D:\input\wide_excel.xlsx // 原始文件
D:\input\temp_wide_excel.xlsx // 臨時設(shè)置后文件
D:\output\wide_excel.pdf // 最終 PDF 輸出
擴展建議
支持批量處理整個文件夾 Excel 文件
自動清理臨時文件
包裝為 CLI 工具或 Sp
到此這篇關(guān)于Java+LibreOffice實現(xiàn)Excel轉(zhuǎn)PDF并橫向一頁顯示所有列的文章就介紹到這了,更多相關(guān)Java Excel轉(zhuǎn)PDF內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot controller參數(shù)注入方式
這篇文章主要介紹了springboot controller參數(shù)注入方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05java實現(xiàn)可視化界面肯德基(KFC)點餐系統(tǒng)代碼實例
這篇文章主要介紹了java肯德基點餐系統(tǒng),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05springboot本地調(diào)試沒問題,打包運行報錯原因及分析
這篇文章主要介紹了springboot本地調(diào)試沒問題,打包運行報錯原因及分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05