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异常");
            }

全部评论

相关推荐

政委qqq:这道题在算法竞赛里唯一考的就是高精度,但是只能难住C++这类语言,Python直接a+b秒天秒地
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务