java 读取doc文件_如何在java中读取Doc或Docx文件?
我想在
java中读一个word文件
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hwpf.*;
import org.apache.poi.hwpf.extractor.*;
import org.apache.poi.hwpf.usermodel.HeaderStories;
import java.io.*;
public class ReadDocFileFromJava {
public static void main(String[] args) {
/**This is the document that you want to read using Java.**/
String fileName = "C:\\Path to file\\Test.doc";
/**Method call to read the document (demonstrate some useage of POI)**/
readMyDocument(fileName);
}
public static void readMyDocument(String fileName){
POIFSFileSystem fs = null;
try {
fs = new POIFSFileSystem(new FileInputStream(fileName));
HWPFDocument doc = new HWPFDocument(fs);
/** Read the content **/
readParagraphs(doc);
int pageNumber=1;
/** We will try reading the header for page 1**/
readHeader(doc, pageNumber);
/** Let's try reading the footer for page 1**/
readFooter(doc, pageNumber);
/** Read the document summary**/
readDocumentSummary(doc);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void readParagraphs(HWPFDocument doc) throws Exception{
WordExtractor we = new WordExtractor(doc);
/**Get the total number of paragraphs**/
String[] paragraphs = we.getParagraphText();
System.out.println("Total Paragraphs: "+paragraphs.length);
for (int i = 0; i < paragraphs.length; i++) {
System.out.println("Length of paragraph "+(i +1)+": "+ paragraphs[i].length());
System.out.println(paragraphs[i].toString());
}
}
public static void readHeader(HWPFDocument doc, int pageNumber){
HeaderStories headerStore = new HeaderStories( doc);
String header = headerStore.getHeader(pageNumber);
System.out.println("Header Is: "+header);
}
public static void readFooter(HWPFDocument doc, int pageNumber){
HeaderStories headerStore = new HeaderStories( doc);
String footer = headerStore.getFooter(pageNumber);
System.out.println("Footer Is: "+footer);
}
public static void readDocumentSummary(HWPFDocument doc) {
DocumentSummaryInformation summaryInfo=doc.getDocumentSummaryInformation();
String category = summaryInfo.getCategory();
String company = summaryInfo.getCompany();
int lineCount=summaryInfo.getLineCount();
int sectionCount=summaryInfo.getSectionCount();
int slideCount=summaryInfo.getSlideCount();
enter code here
System.out.println("---------------------------");
System.out.println("Category: "+category);
System.out.println("Company: "+company);
System.out.println("Line Count: "+lineCount);
System.out.println("Section Count: "+sectionCount);
System.out.println("Slide Count: "+slideCount);
}
}
我想用Java阅读doc或docx文件
java 读取doc文件_如何在java中读取Doc或Docx文件?相关推荐
- python读取bmp文件_如何在python中读取bmp文件头?
您可以使用imghdr module(在python stdlib中):>>> import imghdr >>> print(imghdr.what(input_ ...
- mac命令行将输出写入文件_如何在Linux中使用命令行将PDF文件转换为可编辑文本...
mac命令行将输出写入文件 There are various reasons why you might want to convert a PDF file to editable text. M ...
- 如何在前端删除项目中的文件_如何在macOS上恢复已删除的文件
有时候不小心删除了我们不打算删除的MacBook上的文件.在MacBook上恢复永久删除的文件非常困难,但这并非不可能.在本文中,我们将指导您如何恢复MacBook上最近恢复的文件或永久删除的文件.从 ...
- mysql数据库存歌_如何在mysql中存储音乐和图片文件
标签: 如何在mysql中存储音乐和图片文件? 果你想把二进制的数据,比如说图片文件和HTML文件,直接保存在你的MySQL数据库,那么这篇文章就是为你而写的! 我将告诉你怎样通过HTML表单来储存这 ...
- matlab分析xml文件_如何在Java中读取XML文件(DOM分析器)
matlab分析xml文件 Today we will learn how to read the XML file in Java. We will also learn how to parse ...
- java怎么引入html文件路径_如何在public_html中读取文件但在域外?使用相对路径...
我正在尝试从我的(附加组件)域目录之外的目录中读取文件 . 这是我的目录结构: public_html / domain / file_read.php file_write.php sensitiv ...
- java将输出结果写入csv文件_如何在Java中将数据写入.csv文件?
名为OpenCSV的库提供API来从.CSV文件读取数据或将数据写入.CSV文件.此处说明了如何使用Java程序写入.csv文件的内容. Maven依赖 com.opencsv opencsv 4.4 ...
- java读取hadoop数据_从Hadoop URL中读取数据_hadoop 数据读取_hadoop读取文件
3.5 Java接口 在本小节,我们要深入探索Hadoop的Filesystem类:与Hadoop的文件系统交互的API. 虽然我们主要关注的是HDFS的实现DistributedFileSyste ...
- java 检测目录下的文件_如何在Java中检查文件是目录还是文件
java 检测目录下的文件 java.io.File class contains two methods using which we can find out if the file is a d ...
最新文章
- java本地方法封装库_JNA-Java本地方法封装库
- Fckeditor插入视频或视频文件
- Error starting userland proxy: listen tcp 0.0.0.0:5601: bind: address already in use
- java中各种流的详细使用
- python局部变量函数_python函数局部变量用法实例分析
- windows phone开发环境搭建
- LTE网络架构 学习整理
- 全网目前最全python例子(附源码)八、Python实战
- php微信上传图文素材,php使用curl 上传微信公共平台素材文件
- 【BJOI2019】勘破神机(下降幂转自然幂)(第一类斯特林数)(特征方程)
- 从两年一次到一年三次,百度频繁调整架构为哪般?
- 密码锁 Locker
- Linkflow+微信公众号:如何提升首购率
- 《弟子规》全集译文,为孩子的明天存下来!
- trc20地址监听php,Tron/USDT-TRC20 PHP开发包
- 大一计算机策划案怎么做,计算机类大一社会实践活动策划书
- 深度学习相关公开数据集
- python类的使用的生物学应用_Python 类的使用
- 阅文 java面试题,阅文笔试:讲讲servlet的生命周期
- 计算机中右移一位和除以二的区别
热门文章
- 国内外最好用的6款Bug跟踪管理软件,测试员不可不知!
- 手机老是显示微信连接不到服务器,手机微信无法连接到服务器提示1237怎么办...
- js 获取所有class相同的元素对象(简单实用)
- 图像低频、高频信息的理解
- 一次探索:基于香农熵预测DNA中编码序列,python实现。
- Python爬虫(scrapy模块、bs4模块) 爬取笔趣阁全本小说(三级页面)
- js 字符串截取 slice 的小bug 以及处理方式
- MySQL数据库技术
- 24 直面配分函数Confronting Partition Function
- 【Unity+MySQL】实现简单的注册登录系统