导航:首页 > 文件教程 > java2word读取word

java2word读取word

发布时间:2023-05-26 01:32:59

A. java读取word文件的问题

请贴出代码,谢谢。
请关闭输入流,释放资源,谢谢。调用close()方法。
其他貌似没有发现什么问题。

public static String run(String filename){
WordExtractor extractor=null;
String text=null;
try{
FileInputStream in = new FileInputStream (filename);
extractor = new WordExtractor();
text=extractor.extractText(in);
}catch(Exception ex){
//log
return null;
}
return text;
}
public static void main(String[] args){
try{
FileOutputStream out=new FileOutputStream("result.txt");
out.write(WordProcess.run(args[0]).getBytes());
out.flush();
out.close();
}catch(Exception ex){
System.out.println(ex.toString());
}
}

看看这个。模范这样写,试试看。这个代码我试过,没问题,如果这样写还是有问题,那就不是代码的问题了。

B. 如何使用java操作word 文档

可以考虑ApachePOI,ApachePOI提供API给Java程式对MicrosoftOffice格式档案读和写的功能。

importjava.io.File;
importjava.io.FileInputStream;
importjava.io.InputStream;


importorg.apache.poi.hwpf.extractor.WordExtractor;
/**读取word文本信息*/
publicclassWordDemo{
publicstaticvoidmain(String[]args){
try{
InputStreamis=newFileInputStream(newFile("src/123.doc"));
@SuppressWarnings("resource")
Stringtext123=newWordExtractor(is).getText();
System.out.println(text123);
}catch(Exceptione){
e.printStackTrace();
}
}
}

C. 在java程序中,将一个二进制的word文件读了出来并生成word文件。但是文件有文档保护

如果只是用fileinputstream和fileoutputstream来读取和生成文件的话,只是相当于复制/粘贴。
这样是不可能接触文档保护的。
必须使用poi等包解析旧的word文件,然后重新生成一个新的word文件,才能解除保护。

D. java 中用poi读取word和用docx4j读取word

不知道你是具体读取Word里面的什么元素,下面以读取文字和图片为例吧,两个代码示例,你参考看看:

  1. 读取文本

import com.spire.doc.Document;

import java.io.FileWriter;

import java.io.IOException;

public class ExtractText {

public static void main(String[] args) throws IOException {

//加载Word文档
Document document = new Document();
document.loadFromFile("C:\Users\Administrator\Desktop\sample.docx");

//获取文档中的文本保存为String
String text=document.getText();

//将String写入Txt文件
writeStringToTxt(text,"ExtractedText.txt");
}

public static void writeStringToTxt(String content, String txtFileName) throws IOException {

FileWriter fWriter= new FileWriter(txtFileName,true);
try {
fWriter.write(content);
}catch(IOException ex){
ex.printStackTrace();
}finally{
try{
fWriter.flush();
fWriter.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

}


2. 读取图片


import com.spire.doc.Document;

import com.spire.doc.documents.DocumentObjectType;

import com.spire.doc.fields.DocPicture;

import com.spire.doc.interfaces.ICompositeObject;

import com.spire.doc.interfaces.IDocumentObject;

import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;

import java.util.LinkedList;

import java.util.List;

import java.util.Queue;

public class ExtractImages {

public static void main(String[] args) throws IOException {

//加载Word文档

Document document = new Document();

document.loadFromFile("C:\Users\Administrator\Desktop\sample.docx");

//创建Queue对象

Queue nodes = new LinkedList();

nodes.add(document);

//创建List对象

List images = new ArrayList();

//遍历文档中的子对象

while (nodes.size() > 0) {

ICompositeObject node = nodes.poll();

for (int i = 0; i < node.getChildObjects().getCount(); i++) {

IDocumentObject child = node.getChildObjects().get(i);

if (child instanceof ICompositeObject) {

nodes.add((ICompositeObject) child);

//获取图片并添加到List

if (child.getDocumentObjectType() == DocumentObjectType.Picture) {

DocPicture picture = (DocPicture) child;

images.add(picture.getImage());

}

}

}

}

//将图片保存为PNG格式文件

for (int i = 0; i < images.size(); i++) {

File file = new File(String.format("output/图片-%d.png", i));

ImageIO.write(images.get(i), "PNG", file);

}

}

}

注意这里使用的jar包是spire.doc.jar,需要在java程序中先导入jar文件。

E. java操作word 的有哪几种方式

java读取word文档时,虽然网上介绍了很多插件poi、java2Word、jacob、itext等等,poi无法读取格式(新的API估
计行好像还在处于研发阶段,不太稳定,做项目不太敢用);java2Word、jacob容易报错找不到注册,比较诡异,我曾经在不同的机器上试过,操作
方法完全一致,有的机器不报错,有的报错,去他们论坛找高人解决也说不出原因,项目部署用它有点玄;itxt好像写很方便但是我查了好久资料没有见到过关
于读的好办法。经过一番选择还是折中点采用rtf最好,毕竟rtf是开源格式,不需要借助任何插件,只需基本IO操作外加编码转换即可。rtf格式文件表
面看来和doc没啥区别,都可以用word打开,各种格式都可以设定。

----- 实现的功能:读取rtf模板内容(格式和文本内容),替换变化部分,形成新的rtf文档。

----- 实现思路:模板中固定部分手动输入,变化的部分用$info$表示,只需替换$info$即可。

1、采用字节的形式读取rtf模板内容

2、将可变的内容字符串转为rtf编码

3、替换原文中的可变部分,形成新的rtf文档

主要程序如下:

public String bin2hex(String bin) {

char[] digital = "0123456789ABCDEF".toCharArray();

StringBuffer sb = new StringBuffer("");

byte[] bs = bin.getBytes();

int bit;

for (int i = 0; i < bs.length;i++) {

bit = (bs[i] & 0x0f0)
>> 4;

sb.append("\\'");

sb.append(digital[bit]);

bit = bs[i] & 0x0f;

sb.append(digital[bit]);

}

return sb.toString();

}

public String readByteRtf(InputStream ins, String path){

String sourcecontent =
"";

try{

ins = new
FileInputStream(path);

byte[] b
= new byte[1024];

if (ins == null) {

System.out.println("源模板文件不存在");

}

int bytesRead = 0;

while (true) {

bytesRead = ins.read(b, 0, 1024); // return final read bytes
counts

if(bytesRead == -1) {// end of InputStream

System.out.println("读取模板文件结束");

break;

}

sourcecontent += new String(b, 0, bytesRead); // convert to string
using bytes

}

}catch(Exception e){

e.printStackTrace();

}

F. 使用java读取word文档中的内容。帮帮举个了例子。 谢谢大家帮助。

第一步:下载tm-extractors-0.4.jar下载地址:http://download.csdn.net/download/zcq87642231/1060382并把它放到你的classpath路径下面。第二步:简单的程序.(WordReader .java) import java.io.File;
import java.io.FileInputStream;

import org.textmining.text.extraction.WordExtractor;
public class WordReader {
public static String readDoc(String doc) throws Exception {
// 创建输入流读取doc文件
FileInputStream in = new FileInputStream(new File(doc));
WordExtractor extractor = null;
String text = null;
// 创建做做WordExtractor
extractor = new WordExtractor();
// 对doc文件进行提取
text = extractor.extractText(in);
return text;
}
/**
* @param args
*/
public static void main(String[] args) {
//亮则 TODO Auto-generated method stub
try{
/敬胡棚/读取文件
String text = WordReader.readDoc("D:/tt/tt.doc");
//得到数据后打印出来(也可用一个流写到txt文件中)
System.out.println(text);
}catch(Exception ex){
ex.printStackTrace();
}
}
}

G. java读取带格式word内容

用jacob吧。。

/**
*@author eyuan
*/
package per.eyuan.word2txt.core;

import com.jacob.*;
import com.jacob.com.*;
import com.jacob.activeX.*;
import java.io.*;
import java.util.Scanner;

public class Core {
/**
* 实现转换的函数
* @param sourceFilesPath
* @param destinationFilesPath
* @param destinationFilesType
* @return void
* @see import com.jacob.activeX.*;
*/
public static void change(String sourceFilesPath,String destinationFilesPath,int destinationFilesType){
//使用word文件所在的目录(源路径)建立目录文件
File sourcePathFile=new File(sourceFilesPath);
//取得word文件(源文件列表)
File sourceFilesList[]=sourcePathFile.listFiles();
System.out.println("共有"+sourceFilesList.length+"个文件(文件夹)");
//指定要转换的文件所在的目录下,如果有子目录,
//则进入子目录,继续查找word文档并将其转换,
//直到将指定目录下的所有word文档转换完。
//子目录名
String sourceChildPath=new String("");
//保持原来的层次关系,将子目录下的文件存放在新建的子目录中
String destiNationChildPath=new String("");
//检索文件,过滤掉非word文件,通过扩展名过滤
for(int i=0;i<sourceFilesList.length;i++){
//排除掉子文件夹
if(sourceFilesList[i].isFile()){
System.out.println("第"+(i+1)+"个文件:");
//取得文件全名(包含扩展名)
String fileName=sourceFilesList[i].getName();
String fileType=new String("");
//取得文件扩展名
fileType=fileName.substring((fileName.length()-4), fileName.length());
//word2007-2010扩展名为docx
//判断是否为word2007-2010文档,及是否以docx为后缀名
if(fileType.equals("docx")){
System.out.println("正在转换。。。");
//输出word文档所在路劲
System.out.println("目录:"+sourceFilesPath);
//输出word文档名
System.out.println("文件名:"+fileName);
//System.out.println(fileName.substring(0, (fileName.length()-5)));
//核心函数
//启动word
ActiveXComponent app=new ActiveXComponent("Word.Application");
//要转换的文档的全路径(所在文件夹+文件全名)
String docPath=sourceFilesPath+"\\"+fileName;
//转换后的文档的全路径(所在文件夹+文件名)
String othersPath=destinationFilesPath+"\\"+fileName.substring(0,(fileName.length()-5));
//
String inFile=docPath;
String outFile=othersPath;
//
boolean flag=false;
//核心代码
try{
//设置word可见性
app.setProperty("Visible", new Variant(false));
//
Dispatch docs=app.getProperty("Documents").toDispatch();
//打开word文档
Dispatch doc=Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[]{inFile,new Variant(false),new Variant(true)}, new int[1]).toDispatch();
//0:Microsoft Word 97 - 2003 文档 (.doc)
//1:Microsoft Word 97 - 2003 模板 (.dot)
//2:文本文档 (.txt)
//3:文本文档 (.txt)
//4:文本文档 (.txt)
//5:文本文档 (.txt)
//6:RTF 格式 (.rtf)
//7:文本文档 (.txt)
//8:HTML 文档 (.htm)(带文件夹)
//9:MHTML 文档 (.mht)(单文件)
//10:MHTML 文档 (.mht)(单文件)
//11:XML 文档 (.xml)
//12:Microsoft Word 文档 (.docx)
//13:Microsoft Word 启用宏的文档 (.docm)
//14:Microsoft Word 模板 (.dotx)
//15:Microsoft Word 启用宏的模板 (.dotm)
//16:Microsoft Word 文档 (.docx)
//17:PDF 文件 (.pdf)
//18:XPS 文档 (.xps)
//19:XML 文档 (.xml)
//20:XML 文档 (.xml)
//21:XML 文档 (.xml)
//22:XML 文档 (.xml)
//23:OpenDocument 文本 (.odt)
//24:WTF 文件 (.wtf)
//另存为指定格式的文档
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[]{outFile,new Variant(destinationFilesType)}, new int[1]);
//
Variant file=new Variant(false);
//关闭文档
Dispatch.call(doc, "Close",file);
//
flag=true;
}catch(Exception e){
e.printStackTrace();
System.out.println("文档转换失败");
}finally{
app.invoke("Quit",new Variant[]{});
}
System.out.println("转换完毕");
}
//word97-2003扩展名为doc
//判断是否为word2003-2007文档,及是否以doc为后缀名
else if(fileType.equals(".doc")){
System.out.println("正在转换。。。");
//输出word文档所在路劲
System.out.println("目录:"+sourceFilesPath);
//输出word文档名
System.out.println("文件名:"+fileName);
//System.out.println(fileName.substring(0, (fileName.length()-4)));
//核心函数
//启动word
ActiveXComponent app=new ActiveXComponent("Word.Application");
//要转换的文档的全路径(所在文件夹+文件全名)
String docPath=sourceFilesPath+"\\"+fileName;
//转换后的文档的全路径(所在文件夹+文件名)
String othersPath=destinationFilesPath+"\\"+fileName.substring(0,(fileName.length()-4));
//
String inFile=docPath;
String outFile=othersPath;
//
boolean flag=false;
//核心代码
try{
//设置word可见性
app.setProperty("Visible", new Variant(false));
//
Dispatch docs=app.getProperty("Documents").toDispatch();
//打开word文档
Dispatch doc=Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[]{inFile,new Variant(false),new Variant(true)}, new int[1]).toDispatch();
//另存为指定格式的文档
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[]{outFile,new Variant(destinationFilesType)}, new int[1]);
//
Variant file=new Variant(false);
//关闭文档
Dispatch.call(doc, "Close",file);
//
flag=true;
}catch(Exception e){
e.printStackTrace();
System.out.println("文档转换失败");
}finally{
app.invoke("Quit",new Variant[]{});
}
System.out.println("转换完毕");
}
//文档的扩展名不是doc或docx
else{
System.out.println("非word文档");
}
}
//如果是子文件夹,则递归遍历,将所有的word文档转换
else{
//
sourceChildPath=sourceFilesPath;
//该文件是目录
sourceChildPath=sourceChildPath+"\\"+sourceFilesList[i].getName()+"\\";
System.out.println("源文件所在路径:"+sourceChildPath);
//修改目标文件夹,保持原来的层级关系
destiNationChildPath=destinationFilesPath;
destiNationChildPath=destinationFilesPath+"\\"+sourceFilesList[i].getName()+"\\";
System.out.println("转换后文件所在路径"+destiNationChildPath);
//
mkdir(destiNationChildPath);
//递归遍历所有目录,查找word文档,并将其转换
change(sourceChildPath, destiNationChildPath,destinationFilesType);
}
}
System.out.println("所有文档转换完毕");
}
/**
* 用于创建文件夹的方法
* @param mkdirName
*/
public static void mkdir(String mkdirName){
try{
//使用指定的路径创建文件对象
File dirFile = new File(mkdirName);
//
boolean bFile = dirFile.exists();
//已经存在文件夹,操作???提醒是否要替换
if( bFile == true ) {
System.out.println("已经存在文件夹"+mkdirName);
}
//不存在该文件夹,则新建该目录
else{
System.out.println("新建文件夹"+mkdirName);
bFile = dirFile.mkdir();
if( bFile == true ){
System.out.println("文件夹创建成功");
}else{
System.out.println(" 文件夹创建失败,清确认磁盘没有写保护并且空件足够");
System.exit(1);
}
}
}catch(Exception err){
System.err.println("ELS - Chart : 文件夹创建发生异常");
err.printStackTrace();
}finally{

}
}
/**
* 判断某个文件夹是否存在
* @param path
*/
public static boolean isPathExist(String path){
boolean isPathExist=false;
try{
File pathFile = new File(path);
if(pathFile.exists())
isPathExist= true;
else
isPathExist= false;
}catch(Exception err){
err.printStackTrace();
}
return isPathExist;
}
/**
* 主函数
*/
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
//源文档所在路径
String sourceFilesPath="";
// String inputSourcePath="";
// boolean sourcePathFlag=true;
// System.out.println("请输入要转换文档所在的文件夹");
// while(sourcePathFlag){
// inputSourcePath=sc.next();
// if(!isPathExist(inputSourcePath))
// System.out.println("源路径不存在,请输入正确的路径");
// else
// sourcePathFlag=false;
// }
// sourceFilesPath=inputSourcePath;
sourceFilesPath="D:\\word";
//目标文档要存放的目录
String destinationFilesPath="";
// String inputdestinationPath="";
// boolean destinationPathFlag=true;
// System.out.println("请输入转换后文档要存放的文件夹");
// while(destinationPathFlag){
// inputdestinationPath=sc.next();
// //目标文件不存在时,是否要提示用户创建文件
// if(!isPathExist(inputdestinationPath))
// System.out.println("目标路径不存在,请输入正确的路径");
// else
// destinationPathFlag=false;
// }
// destinationFilesPath=inputdestinationPath;
destinationFilesPath="D:\\txt";
//选择要转换的类型
int destinationFilesType=0;
int inputNumber=0;
boolean numFlag=true;
System.out.println("您要将word文档转换为哪种文档格式?");
System.out.println("0:doc \t 2:txt \t 8:html \t 9:htm \t 11:xml \t 12:docx \t 17:pdf \t 18:xps");
while(numFlag){
inputNumber=sc.nextInt();
if(inputNumber!=2&&inputNumber!=8&&inputNumber!=9&&inputNumber!=11&&inputNumber!=12&&inputNumber!=17){
System.out.println("您的输入有误,请输入要转换的文档类型前的数字");
}else
numFlag=false;
}
destinationFilesType=inputNumber;
//实行转换
change(sourceFilesPath, destinationFilesPath,destinationFilesType);
//测试各种类型转换
// for(int i=0;i<25;i++){
// destinationFilesType=i;
// System.out.println("文件类型"+destinationFilesType);
// System.out.println("存放目录:"+destinationFilesPath+"\\"+i);
// mkdir(destinationFilesPath+"\\"+i);
// change(sourceFilesPath, destinationFilesPath+"\\"+i,destinationFilesType);
// }
}
}

这个我刚用的。。格式都能带过来的。 你自己再下载个 jacob的包和dll文件

H. 在java开发中怎么把word文档读到页面文本框中

1、先用个组件如POI或是纳激笑tika读出word里边的内容到内存程序中。
2、用jsp的变量输出洞含或是el表达式等都可以铅档了。

I. 用java程序如何读取word里面的图片

试试jacob,这个操作word的功能还是蛮多的。

J. java选择读取word文件中的数据

这个有点难了,需要遍历,使用 readline()和substing() 截取关键字!!

阅读全文

与java2word读取word相关的资料

热点内容
dede工具 浏览:507
5g网盟app怎么下载 浏览:486
微信备份老是连接中断 浏览:886
出台多少份文件 浏览:380
鞋子怎么搭配衣服的app 浏览:755
文件名使用的通配符的符号是什么 浏览:916
lol分卷文件损坏怎么办 浏览:276
6分管车螺纹怎么编程 浏览:732
海口农商银行信用卡app是什么 浏览:770
win10任务栏文件夹我的电脑 浏览:14
安卓nba2k18 浏览:776
文件夹密码怎么修改密码 浏览:271
苹果数据中心用什么服务器 浏览:769
省内圆通快递寄文件夹需要多少钱 浏览:740
iphone程序加密 浏览:884
win10文件夹调整文件行高 浏览:681
创意手绘教程 浏览:754
微信删除帐号信息 浏览:596
mysql操作类文件 浏览:649
绕过xp密码 浏览:158

友情链接