Java读取计算 PPT,Word,excel的页数

本文介绍如何使用Java批量读取PPT、Word和Excel文档的页数,涉及Apache POI和Jacob库的使用,包括依赖配置、代码实现及Word转PDF的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java读取计算 PPT,Word,excel的页数

本为参考了多篇文档,在此表示感谢,使用maven加载依赖文档,有啥问题可以多加讨论
依赖pom:

  <dependencies>
  	 <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.8</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-scratchpad</artifactId>
        <version>3.8</version>
    </dependency>
    <dependency>
	    <groupId>org.apache.pdfbox</groupId>
	    <artifactId>pdfbox</artifactId>
	    <version>2.0.11</version>
	</dependency>
	<dependency>
	    <groupId>com.itextpdf</groupId>
	    <artifactId>itextpdf</artifactId>
	    <version>5.0.6</version>
	</dependency>
  </dependencies>

目录结构
在这里插入图片描述

压缩包 将Jacob-1.19-x64放到配置的Java环境变量的目录下,Jacob.jar添加依赖在这里插入图片描述

在这里插入图片描述
代码如下:

package doc;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.itextpdf.text.pdf.PdfReader;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
/**
 * 计算ppt,Word,excel的页数 
 * @author
 *
 */
public class Main {
 
	static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
	static final int wdFormatPDF = 17;// word转PDF 格式
	
	public static void main(String[] args) throws IOException {
//		word2pdf("xxx.doc",
//				"xxx.pdf");
		int pdfPage = getPdfPage("xxx.pdf");
		System.out.println(pdfPage);
		int excelPage = countExcelPage("xxx.xlsx");
		System.out.println(excelPage);
		int PPTPage = countPPTPage("xxx.ppt");
		System.out.println(PPTPage);
	}

	/**
	 * 计算Word转化成的pdf格式文档的页数
	 * @param filepath 文件路径
	 * @return pagecount页数
	 */
	public static int getPdfPage(String filepath){
		int pagecount = 0;	
		PdfReader reader;
		try {
			reader = new PdfReader(filepath);
			pagecount= reader.getNumberOfPages(); 
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(pagecount);
		return pagecount;
	}
	
	/**
	 * 计算Excel格式文档的页数
	 * @param filepath 文件路径
	 * @return result 页数
	 */
	public static int countExcelPage(String filePath){ 
		  try{ 
		    InputStream myxls = new FileInputStream(filePath); 
		    XSSFWorkbook wb     = new XSSFWorkbook(myxls); 
		    
		    int result = wb.getNumberOfSheets() ; 
		      return result ; 
		    
		  }catch(Exception e){ 
		    e.printStackTrace() ; 
		    return -1 ; 
		  } 
		}
	/**
	 * 计算PPT格式文档的页数
	 * @param filePath 文件路径
	 * @return pages 页数
	 */
	public static int countPPTPage(String filePath) throws IOException{ 
		FileInputStream fis = new FileInputStream(filePath);
		XMLSlideShow pptxfile = new XMLSlideShow(fis);
		int pages = pptxfile.getSlides().length;
		return pages;
		}
 
	/**
	 * 将Word文件装换为PDF格式的
	 * @param source 源文件路径
	 * @param target 目标文件路径
	 * @return 表示转换是否成功
	 */
	public static boolean word2pdf(String source, String target) {
		System.out.println("Word转PDF开始启动...");
		long start = System.currentTimeMillis();
		ActiveXComponent app = null;
		try {
			app = new ActiveXComponent("Word.Application");
			app.setProperty("Visible", false);
			Dispatch docs = app.getProperty("Documents").toDispatch();
			System.out.println("打开文档:" + source);
			Dispatch doc = Dispatch.call(docs, "Open", source, false, true).toDispatch();
			System.out.println("转换文档到PDF:" + target);
			File tofile = new File(target);
			if (tofile.exists()) {
				tofile.delete();
			}
			Dispatch.call(doc, "SaveAs", target, wdFormatPDF);
			Dispatch.call(doc, "Close", false);
			long end = System.currentTimeMillis();
			System.out.println("转换完成,用时:" + (end - start) + "ms");
			return true;
		} catch (Exception e) {
			System.out.println("Word转PDF出错:" + e.getMessage());
			return false;
		} finally {
			if (app != null) {
				app.invoke("Quit", wdDoNotSaveChanges);
			}
		}
	}
}

参考
https://blog.youkuaiyun.com/tiandixuanwuliang/article/details/71298406

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值