如何获取 Word/Excel 文档的页数/页数? [英] How to get Page/Sheet Count of Word/Excel documents?

查看:75
本文介绍了如何获取 Word/Excel 文档的页数/页数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我有一个要求,即显示 Word 文档(.doc、.docx)文件中的页数和 Excel 文档(.xls、.xlsx)中的工作表数.我曾尝试使用 Docx4j 读取 .docx 文件,但性能很差,但我只需要字数并尝试使用 Apache POI.我收到一个错误,例如:

In my project I have one requirement to show the number of pages in Word documents (.doc, .docx) files and number of sheets in Excel documents (.xls, .xlsx). I have tried to read the .docx file using Docx4j but the performance is very poor but I need just the word count and tried using Apache POI. I am getting an error, something like:

"trouble writing output: Too many methods: 94086; max is 65536. By package:" 

我想知道是否有任何可用于 android 的付费/开源库.

I want to know whether there is any paid/open source library available for android.

推荐答案

没有办法在 MS Word 文件中显示确切的页数,因为不同的用户会有所不同.确切数字取决于打印机设置、纸张设置、字体、可用图像等.

There is just no way to show exact number of pages in MS Word file, because it will be different for different users. The exact number depends on printer settings, paper settings, fonts, available images, etc.

不过,您可以对二进制文件执行以下操作:

Still, you can do the following for binary files:

  • 打开文件使用POIFSFileSystem 或 NPOIFSFileSystem
  • 只提取 FileInformationBlock,因为它是在构造函数 HWPFDocumentCore 中完成的
  • 使用 FileInformationBlock 中的信息创建 DocumentProperties,因为它是在 HWPFDocument 的构造函数中完成的
  • 获取 DOP 的属性 cPg 的值:DocumentProperties::getCPg()

该字段的描述是:一个有符号整数值,它指定主文档中最后计算或估计的页数,具体取决于 fExactCWords 和 fIncludeSubdocsInStats 的值."

The description of this field is: "A signed integer value that specifies the last calculated or estimated count of pages in the main document, depending on the values of fExactCWords and fIncludeSubdocsInStats."

对于 DOCX/XLSX 文档,您需要访问相同(我假设)的属性,但使用 SAX 或 StAX 方法.

For DOCX/XLSX documents you will need to access the same (I assume) property but using SAX or StAX methods.

这篇关于如何获取 Word/Excel 文档的页数/页数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆