ZipFile docxFile = new ZipFile(new File("c:/3.docx"));
ZipEntry documentXML = docxFile.getEntry("word/document.xml");
InputStream documentXMLIS = docxFile.getInputStream(documentXML);
String s = "";
InputStreamReader reader = new InputStreamReader(documentXMLIS,"UTF-8");
BufferedReader br = new BufferedReader(reader);
String str = null;
while ((str = br.readLine()) != null) {
s = s+str;
}
s = s.replaceAll("${key}", "替换内容");
System.out.println(s);
reader.close();
br.close();
if(true){
//return;
}
//ZipEntry imgFile = docxFile.getEntry("word/media/image1.png");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
InputStream documentXMLIS1 = docxFile.getInputStream(documentXML);
Document doc = dbf.newDocumentBuilder().parse(documentXMLIS1);
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());
//assertEquals("这是第一个测试文档", tElement.getTextContent());
//tElement.setTextContent("这是第一个用Java写的测试文档");
Transformer t = TransformerFactory.newInstance().newTransformer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
t.transform(new DOMSource(doc), new StreamResult(baos));
ZipOutputStream docxOutFile = new ZipOutputStream(new FileOutputStream(
"response.docx"));
Enumeration<ZipEntry> entriesIter = (Enumeration<ZipEntry>) docxFile
.entries();
while (entriesIter.hasMoreElements()) {
ZipEntry entry = entriesIter.nextElement();
System.out.println(entry.getName());
if (entry.getName().equals("word/document.xml")) {
byte[] data = baos.toByteArray();
docxOutFile.putNextEntry(new ZipEntry(entry.getName()));
byte[] datas = s.getBytes("UTF-8");
docxOutFile.write(datas, 0, datas.length);
//docxOutFile.write(data, 0, data.length);
docxOutFile.closeEntry();
} else if(entry.getName().equals("word/media/image1.png")){
InputStream incoming = new FileInputStream("c:/aaa.jpg");
byte[] data = new byte[incoming.available()];
int readCount = incoming.read(data, 0, data.length);
docxOutFile.putNextEntry(new ZipEntry(entry.getName()));
docxOutFile.write(data, 0, readCount);
docxOutFile.closeEntry();
}else {
InputStream incoming = docxFile.getInputStream(entry);
byte[] data = new byte[incoming.available()];
int readCount = incoming.read(data, 0, data.length);
docxOutFile.putNextEntry(new ZipEntry(entry.getName()));
docxOutFile.write(data, 0, readCount);
docxOutFile.closeEntry();
}
}
docxOutFile.close();
java poi导出word 可以设置格式吗
读取word 2003及word 2007需要的jar包
读取 2003 版本(.doc)的word文件相对来说比较简单,只需要 poi-3.5-beta6-.jar 和 poi-scratchpad-3.5-beta6-.jar 两个 jar 包即可, 而 2007 版本(.docx)就麻烦多,我说的这个麻烦不是我们写代码的时候麻烦,是要导入的 jar 包比较的多,有如下 7 个之多:
1. openxml4j-bin-beta.jar
2. poi-3.5-beta6-.jar
3. poi-ooxml-3.5-beta6-.jar
4 .dom4j-1.6.1.jar
5. geronimo-stax-api_1.0_spec-1.0.jar
6. ooxml-schemas-1.0.jar
7. xmlbeans-2.3.0.jar
其中 4-7 是 poi-ooxml-3.5-beta6-.jar 所依赖的 jar 包(在 poi-bin-3.5-beta6-.tar.gz 中的 ooxml-lib 目录下可以找到)。
2.换行符号
硬换行:文件中换行,如果是键盘中使用了"enter"的换行。
软换行:文件中一行的字符数容量有限,当字符数量超过一定值时,会自动切到下行显示。
对程序来说,硬换行才是可以识别的、确定的换行,软换行与字体大小、缩进有关。
3.读取的注意事项
值得注意的是: POI 在读取不会读取 word 文件中的图片信息; 还有就是对于 2007 版的 word(.docx), 如果 word 文件中有表格,所有表格中的数据都会在读取出来的字符串的最后。
4.读取word文本内容代码
1 import java.io.File;
2 import java.io.FileInputStream;
3 import java.io.InputStream;
4
5 import org.apache.poi.POIXMLDocument;
6 import org.apache.poi.POIXMLTextExtractor;
7 import org.apache.poi.hwpf.extractor.WordExtractor;
8 import org.apache.poi.openxml4j.opc.OPCPackage;
9 import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
10
11 public class Test {
12 public static void main(String[] args) {
13 try {
14 InputStream is = new FileInputStream(new File("2003.doc"));
15 WordExtractor ex = new WordExtractor(is);
16 String text2003 = ex.getText();
17 System.out.println(text2003);
18
19 OPCPackage opcPackage = POIXMLDocument.openPackage("2007.docx");
20 POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
21 String text2007 = extractor.getText();
22 System.out.println(text2007);
23
24 } catch (Exception e) {
25 e.printStackTrace();
26 }
27 }
28 }
POI可不可以实现导出带有图片的word文件
poi jacob不太好用,楼主可以用PageOffice解决这个问题。 生成文件的时候替换模板标签数据,插入图片或合并多个word文档都可以实现的。
请问,apache poi 导出word的时候,有没有方法能在上一页没有写满的情况下直接到下一页写啊
可能是你设置成了“孤行控制”在右键段落里,有设置,这个功能的意思是为了保证段落的完整性,自动进入下一页。如果还不行,就把文件发给我,我给你调试一下,很有可能是因为一个小问题。我也是这么过来的,越是小问题越烦人,不过越能体现一个人的技术
转载请注明出处51数据库 » poi模板导出word动态 poi根据模板导出word