用户登录
用户注册

分享至

docx4j读word内容

  • 作者: 软妹子思密达
  • 来源: 51数据库
  • 2020-04-21

java docx4j 怎么生成docx文档

按Alt+B可进入Break/watch菜单, 该菜单有以下内容,如图所示:

1. Add watch: 向监视窗口插入一监视表达式。

2. Delete watch:从监视窗口中删除当前的监视表达式。

3. Edit watch:在监视窗口中编辑一个监视表达式。

4. Remove all :watches从监视窗口中删除所有的监视表达式。

5. Toggle breakpoint:对光标所在的行设置或清除断点。

6. Clear all breakpoints:清除所有断点。

7. View next breakpoint:将光标移动到下一个断点处。

读取word文档,并将其中的内容按原来的样式输出,望各位大神赐教!

package com;import static org.junit.Assert.assertEquals;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.Enumeration;import java.util.zip.ZipEntry;import java.util.zip.ZipException;import java.util.zip.ZipFile;import java.util.zip.ZipOutputStream;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerConfigurationException;import javax.xml.transform.TransformerException;import javax.xml.transform.TransformerFactory;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import org.junit.Test;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.xml.sax.SAXException;public class InputToword { @Test public void modifyDocumentAndSave()throws IOException, ZipException, SAXException,ParserConfigurationException,TransformerException,TransformerConfigurationException { //读取e盘下的hello.docx文档 ZipFile docxFile =new ZipFile(new File("e:\\hello.docx")); //解压缩后获得里面和内容相关的xml,word文档是可以解压的,大家可以解压了试试 ZipEntry documentXML =docxFile.getEntry("word/document.xml"); InputStream documentXMLIS =docxFile.getInputStream(documentXML); DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance(); Document doc =dbf.newDocumentBuilder().parse(documentXMLIS); //获得文档里相关的节点 Element docElement = doc.getDocumentElement(); assertEquals("w:document", docElement.getTagName()); Element bodyElement = (Element)docElement.getElementsByTagName("w:body").item(0); assertEquals("w:body", bodyElement.getTagName()); Element pElement = (Element)bodyElement.getElementsByTagName("w:p").item(0); assertEquals("w:p", pElement.getTagName()); Element rElement = (Element)pElement.getElementsByTagName("w:r").item(0); assertEquals("w:r", rElement.getTagName()); Element tElement = (Element)rElement.getElementsByTagName("w:t").item(0); assertEquals("w:t", tElement.getTagName()); //查找文档中的Hello, from Office 2007!文字部分 assertEquals("Hello, from Office 2007!",tElement.getTextContent()); //写入新的内容 tElement.setTextContent("哈哈,终于可以用java写word了,Hello, Office 2007, from Java6!"); Transformer t =TransformerFactory.newInstance().newTransformer(); ByteArrayOutputStream baos =new ByteArrayOutputStream(); t.transform(new DOMSource(doc), new StreamResult(baos)); //创建新的要输出的word文档,按钮原来word文档的内容写入新的文档中。

ZipOutputStream docxOutFile = new ZipOutputStream(new FileOutputStream("e:\\response.docx")); Enumeration entriesIter =docxFile.entries(); while (entriesIter.hasMoreElements()) { ZipEntry entry = (ZipEntry) entriesIter.nextElement(); if (entry.getName().equals("word/document.xml")) { byte[] data = baos.toByteArray(); docxOutFile.putNextEntry(new ZipEntry(entry.getName())); docxOutFile.write(data, 0, data.length); docxOutFile.closeEntry(); } else { InputStream incoming =docxFile.getInputStream(entry); byte[] data = new byte[1024 * 16]; int readCount =incoming.read(data, 0, data.length); docxOutFile.putNextEntry(new ZipEntry(entry.getName())); docxOutFile.write(data, 0, readCount); docxOutFile.closeEntry(); } } docxOutFile.close(); }}。

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());

}

}

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

excel vba 读取 word 指定字符

Sub 按钮1()

Dim myPath As String

Set Wdapp = CreateObject("Word.Application")

Wdapp.Visible = True

Application.ScreenUpdating = False '关闭屏幕刷新

' On Error Resume Next '捕捉错误

myPath = ThisWorkbook.Path &; "\多房地产预评估函.doc" '定义word文件路径,自己修改

Set wdDoc = Wdapp.Documents.Open(myPath) '打开word

wdDoc.Activate

sr = wdDoc.Content '将word的文档内容赋予变量sr

MsgBox Mid(sr, InStr(sr, "籍贯") + 3, 2)

wdDoc.Close '关闭word

Wdapp.Quit

Set Wdapp = Nothing

Set wdDoc = Nothing

Application.ScreenUpdating = True '开启屏幕刷新

End Sub

有谁知道用java语言怎么实现将word的doc格式文件转换为docx格式的-

如果只是文字的话,直接读出来,然后用07的存回去,poi能实现。

但是有目录什么的就麻烦了,03的读出来目录是一行代码,就容易出问题我用的poi正好在纠结,最后没办法,用按键精灵一个个转的。有个word文档批处理软件貌似能做到,但是要收费。

下面的是我读取word文字的代码03读取:InputStream is = new FileInputStream(docfile); WordExtractor ex = new WordExtractor(is); text = ex.getText(); System.out.println(text);07读取:OPCPackage opcPackage = POIXMLDocument.openPackage(docxfile.getAbsolutePath()); POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage); text = extractor.getText(); System.out.println(text);最后:doc不一定是03的,也可能是07的。

转载请注明出处51数据库 » docx4j读word内容

软件
前端设计
程序设计
Java相关