1.接上文,格式轉(zhuǎn)換的基礎(chǔ)問題已解決,但還有些細(xì)節(jié)問題需要單獨(dú)處理,如excel轉(zhuǎn)換至pdf時(shí),如何將所有列顯示在一頁的問題,此問題大家都有遇到,解決方案也比較多,我也嘗試過重寫某類,來實(shí)現(xiàn)自定義pdf頁面篇幅大小問題,但現(xiàn)在有個(gè)更優(yōu)的方案給到大家,利用逆向思維,在excel轉(zhuǎn)換pdf前,優(yōu)先對(duì)excel進(jìn)行預(yù)處理,如使用poi組件,將excel的所有列設(shè)置在一頁,即可。
2.首先在build.gradle中引入POI依賴
implementation group: 'org.apache.poi', name: 'poi', version: '5.2.3'
implementation group: 'org.apache.poi', name: 'poi-ooxml', version: '5.2.3'
implementation group: 'org.apache.poi', name: 'poi-scratchpad', version: '5.2.3'
3.書寫工具類,優(yōu)先對(duì)excel進(jìn)行預(yù)處理,即可。
/**
* change page size.
*/
public void refresh(String oldFilePath, String oldType, String newFilePath) throws IOException {
Workbook workbook = null;
if (oldType.equals("xls")) {
workbook = new HSSFWorkbook(new FileInputStream(oldFilePath));
} else if (oldType.equals("xlsx")) {
workbook = new XSSFWorkbook(new FileInputStream(oldFilePath));
}
Integer sheetCount = workbook.getNumberOfSheets();
for (int i = 0; i < sheetCount; i++) {
Sheet sheet = workbook.getSheetAt(i);
//通過此處即可將excel中所有列放置在一頁顯示(打?。D(zhuǎn)換至pdf后也將在一頁顯示
XSSFPrintSetup printSetup = (XSSFPrintSetup) sheet.getPrintSetup();
printSetup.setFitHeight((short) 0);
sheet.setFitToPage(true);
}
workbook.write(new FileOutputStream(newFilePath));
}
效果預(yù)覽:
xlsx文件原來多頁效果:
轉(zhuǎn)換為PDF后效果:
?文章來源地址http://www.zghlxwxcb.cn/news/detail-626734.html文章來源:http://www.zghlxwxcb.cn/news/detail-626734.html
?
到了這里,關(guān)于java使用openOffice將excel轉(zhuǎn)換pdf時(shí),將所有列顯示在一頁的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!