Ⅰ 如何在java中读取word文件
java读取word文档,获取文本内容,保留基本的换行格式。
java用POI对word进行解析。所需jar包,用maven引入
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.2-FINAL</version>
</dependency>
前端用webuploader上传控件,限制上传文件类型仅支持text和word.
txt为word的文本内容
Ⅱ java 中用poi读取word和用docx4j读取word
不知道你是具体读取Word里面的什么元素,下面以读取文字和图片为例吧,两个代码示例,你参考看看:
读取文本
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文件。
Ⅲ java想要对已有word文档追加图片,应该怎么做
以前做过其他office的没做过微软的,不过大同小异
这个你就需要在java中获取到word的某种对象,方法有很多,但是要看你用什么方式调用的office了,如果是某个牌子的中间件,那就用那个中间件的二次开发文档里面应该就有提到这些方法,如果是第三方的jar包,应该也不麻烦,通过jar包中的方法,可能要有一个word的实例创建的过程吧,然后获取到range或者textrange等这些对象(应该是这俩对象……)通过这对象然后找到插入图片的方法,然后传入位置参数(可能是两个数用逗号隔开)
Ⅳ java怎么识别word内的图片
这个问题和语言无关哦,你要用到 java 上解析 word 文件的第三方包来实现,有很多,搜索一下就好了。
Ⅳ 我想要实现的功能是用java读取word中的自定义图片
建议使用PageOffice很简单就能实现这些。
Ⅵ 用java怎么将word文档转成图片格式
使用 java 操作 openoffice 实现格式转换 辛苦了几天。 不敢独享成果
首先,要安装 openoffice (废话- -)
创建JAVA项目的时候 需要至少以下4个包
juh.jar,jurt.jar,ridl.jar,unoil.jar
以下是路径
..\OpenOffice.org 2.3\program\classes
我只实现了我要的功能,就是把一种文件格式转成另一种
Java代码
package testh;
import java.io.*;
import com.sun.star.uno.UnoRuntime;
public class testcls {
public static void readdoc(String paths, String savepaths)
{
File d = new File(paths);
//取得当前文件夹下所有文件和目录的列表
File lists[] = d.listFiles();
String pathss = new String("");
//对当前目录下面所有文件进行检索
for(int i = 0; i < lists.length; i ++)
{
if(lists[i].isFile())
{
String filename = lists[i].getName();
String filetype = new String("");
//取得文件类型
filetype = filename.substring((filename.length() - 3), filename.length());
//判断是否为doc文件
if(filetype.equals("doc"))
{
System.out.println("当前正在检索....");
//打印当前目录路径
System.out.println(paths);
//打印doc文件名
String fname=filename.substring(0, (filename.length() - 4));
System.out.println("检索到文件"+fname);
try
{
//指定文件路径和名称
String path = savepaths+fname+".html";
File outfilename = new File(path);
/** *//**
* 检查文件是否存在.
* @throws IOException
*
*/
if (!outfilename.exists()) {
System.err.println("目标路径无同名文件,开始转换");
System.out.print("正在转换文件:"+fname);
Dump(paths,fname,savepaths);
}
else
{
System.out.print("文件已存在,放弃创建,请处理存在文件后再运行...\n");
continue;
}
RandomAccessFile mm = null;
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
}
public static void Dump(String FilePath,String FileName,String OutPath)
{
com.sun.star.uno.XComponentContext xContext = null;
try {
// get the remote office component context
xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
System.out.println("Connected to a running office ...");
// get the remote office service manager
com.sun.star.lang.XMultiComponentFactory xMCF =
xContext.getServiceManager();
Object oDesktop = xMCF.createInstanceWithContext(
"com.sun.star.frame.Desktop", xContext);
com.sun.star.frame.XComponentLoader xCompLoader =
(com.sun.star.frame.XComponentLoader)
UnoRuntime.queryInterface(
com.sun.star.frame.XComponentLoader.class, oDesktop);
java.io.File sourceFile = new java.io.File(FilePath+FileName+".doc");//读取的文件
StringBuffer sLoadUrl = new StringBuffer("file:///");
sLoadUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
sourceFile = new java.io.File(OutPath+FileName+".html");//输出的文件
System.out.print(OutPath+" "+FileName);
StringBuffer sSaveUrl = new StringBuffer("file:///");
sSaveUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
com.sun.star.beans.PropertyValue[] propertyValue =
new com.sun.star.beans.PropertyValue[1];
propertyValue[0] = new com.sun.star.beans.PropertyValue();
propertyValue[0].Name = "Hidden";
propertyValue[0].Value = new Boolean(true);
Object oDocToStore = xCompLoader.loadComponentFromURL(
sLoadUrl.toString(), "_blank", 0, propertyValue );
com.sun.star.frame.XStorable xStorable =
(com.sun.star.frame.XStorable)UnoRuntime.queryInterface(
com.sun.star.frame.XStorable.class, oDocToStore );
propertyValue = new com.sun.star.beans.PropertyValue[ 2 ];
propertyValue[0] = new com.sun.star.beans.PropertyValue();
propertyValue[0].Name = "Overwrite";
propertyValue[0].Value = new Boolean(true);
propertyValue[1] = new com.sun.star.beans.PropertyValue();
propertyValue[1].Name = "FilterName";
propertyValue[1].Value = "HTML (StarWriter)";//你一定发现了,把这里改成其他参数,可以保存为不同的文件 MS Word 97,writer_pdf_Export
xStorable.storeAsURL( sSaveUrl.toString(), propertyValue );
System.out.println("\nDocument \"" + sLoadUrl + "\" saved under \"" +
sSaveUrl + "\"\n");
com.sun.star.util.XCloseable xCloseable = (com.sun.star.util.XCloseable)
UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class,
oDocToStore );
if (xCloseable != null ) {
xCloseable.close(false);
} else
{
com.sun.star.lang.XComponent xComp = (com.sun.star.lang.XComponent)
UnoRuntime.queryInterface(
com.sun.star.lang.XComponent.class, oDocToStore );
xComp.dispose();
}
System.out.println("document closed!");
}
catch( Exception e ) {
e.printStackTrace(System.err);
System.exit(1);
}
}
public static void main(String args[]) throws Exception
{
String paths = new String("c:\\a\\");
String savepaths = new String ("f:\\");
readdoc(paths,savepaths);
}
}
Ⅶ 用Java从word中提取出图片
因为太长了, 我放我空间了
思路是2003以后, word就可存为xml, 二进制数据按base64编码
然后可以按解析xml文档方式获取图片数据
然后对它进行解码--
Ⅷ java:一个文件夹里里面有很多图片有很多word文件有我要如何读取word文件名,该如何做
可以用过滤器,也可以循环自己判断,如FourQueue 写的那样。
下边是过滤器的例子。
------------------------------------------------------------------------------------
import java.io.File;
import java.io.FilenameFilter;
public class FileDoc {
public static void main(String[] args) {
File root = new File("D:\\test");
File[] files = root.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
if (name.toLowerCase().endsWith("doc")) {
// 2003 word
return true;
}
if (name.toLowerCase().endsWith("docx")) {
// 2007 word
return true;
}
return false;
}
});
for (int i = 0; i < files.length; i++) {
System.out.println(files[i].getPath());
}
}
}
Ⅸ java 读取word 并识别图片
肯定不是少引用了包,提示你类型不对。这个getImagedataArray这个方法,返回的是int类型,怎么能用数组的方式去取值
Ⅹ 用java程序如何读取word里面的图片
试试jacob,这个操作word的功能还是蛮多的。