java使用openOffice将excel转为pdf
记录:java使用openOffice将excel转为pdf时,将所有列都放在一页pdf钟
1.首先对已有的excel进行处理
/**
* 通过此处即可将excel中所有列放置在一页显示(打印),转换至pdf后也将在一页显示
* 支持xlsx
* @param oldFilePath
* @param oldType
* @param newFilePath
* @throws IOException
*/
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中所有列放置在一页显示(打印),转换至pdf后也将在一页显示
XSSFPrintSetup printSetup = (XSSFPrintSetup) sheet.getPrintSetup();
printSetup.setFitHeight((short) 0);
sheet.setFitToPage(true);
}
workbook.write(new FileOutputStream(newFilePath));
2.将经过处理的新excel转成pdf
DocumentConverter documentConverter =(DocumentConverter)SpringUtils.getCtx().getBean(DocumentConverter.class);
try { documentConverter.convert(inputFile).to(outputFile).as(DefaultDocumentFormatRegistry.PDF).execute();
return true;
} catch (OfficeException var6) {
var6.printStackTrace();
throw new CommonException("转行pdf异常");
}

携程成长空间 145人发布