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文档都可以实现的。
java word 导出怎么合并表头 Word poi
给你个例子
iText-2.1.5.jar、iTextAsian.jar、iText-rtf-2.1.4.jar
public String CreateWord() {
try {
Document document = new Document(PageSize.A4);
RtfWriter2.getInstance(document, new FileOutputStream("c:\\"));
document.open();
for (int i = 0; i < datalist.size(); i++) {
Lawmanager lawmanager = (Lawmanager) datalist.get(i);
Paragraph title = new Paragraph("行 政 执 法 人 员 登 记 表");
title.setAlignment(Element.ALIGN_CENTER);
title.setFont(new RtfFont("仿 宋__GB2312", 18, Font.BOLD, Color.BLACK));
document.add(title);
Table table = new Table(7);
table.setWidth(100);
table.setBorderWidth(1);
table.setBorderColor(Color.BLACK);
table.setPadding(0);
table.setSpacing(0);
table.setAlignment("CENTER");
table.setWidths(new float[]{16.0f,16.0f,15.0f,15.0f,16.0f,22.0f,0.0f});
table.setAutoFillEmptyCells(true);
RtfFont font = new RtfFont("仿 宋__GB2312", 12, Font.COURIER, Color.BLACK);
Cell cell = new Cell(new Phrase("姓 名", font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new Cell(new Phrase(lawmanager.getLawname(), font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setColspan(2);
table.addCell(cell);
cell = new Cell(new Phrase("性 别", font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new Cell(new Phrase(lawmanager.getLawsex(), font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
try {
Image lawPic = Image.getInstance(publicTools.getPath() + "/" + lawmanager.getLawpic());
lawPic.scaleAbsolute(105, 135);
cell = new Cell(lawPic);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setRowspan(4);
table.addCell(cell);
} catch (IOException e) {
publicTools.OutputLog(e.toString(), "ERROR", 0);
Image lawPic = Image.getInstance(publicTools.getPath("upfile/pic") + "pic.jpg");
lawPic.scaleAbsolute(105, 135);
cell = new Cell(lawPic);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setRowspan(4);
table.addCell(cell);
}
cell = new Cell("\n\n");
cell.setBorderColor(new Color(255, 255, 255));
cell.setBackgroundColor(new Color(255, 255, 255));
table.addCell(cell);
cell = new Cell(new Phrase("出生年月日", font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new Cell(new Phrase(lawmanager.getLawbirthdate(), font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setColspan(2);
table.addCell(cell);
cell = new Cell(new Phrase("民 族", font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new Cell(new Phrase(lawmanager.getVolkdictionaryname(), font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new Cell("\n\n");
cell.setBorderColor(new Color(255, 255, 255));
cell.setBackgroundColor(new Color(255, 255, 255));
table.addCell(cell);
cell = new Cell(new Phrase("工作单位", font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new Cell(new Phrase(" " + lawmanager.getDeptname(), font));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setColspan(4);
table.addCell(cell);
cell = new Cell("\n\n");
cell.setBorderColor(new Color(255, 255, 255));
cell.setBackgroundColor(new Color(255, 255, 255));
table.addCell(cell);
cell = new Cell(new Phrase("职 务", font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new Cell(new Phrase(lawmanager.getPostdictionaryname(), font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new Cell(new Phrase("参加工作时间", font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new Cell(new Phrase(lawmanager.getLawworkdate(), font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setColspan(2);
table.addCell(cell);
cell = new Cell("\n\n");
cell.setBorderColor(new Color(255, 255, 255));
cell.setBackgroundColor(new Color(255, 255, 255));
table.addCell(cell);
document.add(table);
Phrase phrase = new Phrase("\n注:如未取得行政执法证件、行政执法资格的,在表中“行政执法证件编号”、“行政执法资格编号”栏中显示“0”。", new RtfFont("黑 体", 10, Font.TIMES_ROMAN, Color.red));
document.add(phrase);
if(i < datalist.size() - 1){
document.newPage();
}
}
document.close();
publicTools.insertLog("5", "下载Word文件", userSession);
} catch (FileNotFoundException e) {
publicTools.OutputLog(e.toString(), "ERROR", 0);
e.printStackTrace();
} catch (DocumentException e) {
publicTools.OutputLog(e.toString(), "ERROR", 0);
e.printStackTrace();
} catch (MalformedURLException e) {
publicTools.OutputLog(e.toString(), "ERROR", 0);
e.printStackTrace();
} catch (IOException e) {
publicTools.OutputLog(e.toString(), "ERROR", 0);
e.printStackTrace();
}
return "upfile/download/" + fileName;
}
poi-Java导出word时,能实现在word正文内“插入附件”的效果吗
这个貌似很难,上次我们项目里用到poi了,也是导出word,也希望能附加一些附件,但是这个好像没什么好办法,因为本来就是把数据库中的数据导出,你可能是想通过数据库中的路径取到某些文件,然后把这些文件放到word中,但是怎么解决一一对应的关系?即使解决了,这个效果应该也不好实现,这是我的经验。当然,如果你实现了,请告知一下,我也学习一下。嘿嘿。
转载请注明出处51数据库 » poi导出word模板 poi根据模板导出word