Word 2007文件, 替换书签内容

来源:互联网 发布:福建网络原创女歌手 编辑:程序博客网 时间:2024/05/17 22:48

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.Map;
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.springframework.util.Assert;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public final class Word2007Util {


public static void replaceBookMarks(File word2007File, File outFile,
   Map<String, String> bookMarkValues) throws IOException,
   ZipException, SAXException, ParserConfigurationException,
   TransformerException, TransformerConfigurationException {
  Assert.notNull(word2007File);
  Assert.isTrue(word2007File.exists(), "file not exist");
  Assert.isTrue(word2007File.canRead(), "file can't be read");
  Assert.isTrue(word2007File.canWrite(), "file can't be wrote");
  ZipFile docxFile = null;
  ZipOutputStream docxOutFile = null;
  try{
   docxFile = new ZipFile(word2007File);
   ZipEntry documentXML = docxFile.getEntry("word/document.xml");
   InputStream documentXMLIS = docxFile.getInputStream(documentXML);
   DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
   Document doc = dbf.newDocumentBuilder().parse(documentXMLIS);
   NodeList bookmarks = doc.getElementsByTagName("w:bookmarkStart");
   for (int j = 0; j < bookmarks.getLength(); j++) {
    Element bookMarkStart = (Element) bookmarks.item(j); // 获取每个书签
    String bookMarkName = bookMarkStart.getAttribute("w:name"); // 书签名
    if(bookMarkValues.containsKey(bookMarkName)){
     // 书签处值开始
     Node wr = doc.createElement("w:r");
     Node wt = doc.createElement("w:t");
     Node wt_text = doc.createTextNode(bookMarkValues.get(bookMarkName));
     wt.appendChild(wt_text);
     wr.appendChild(wt);
     // 书签处值结束
     Element node = (Element) bookMarkStart.getNextSibling();
     NodeList wtList = node.getElementsByTagName("w:t");
     if (wtList.getLength() == 0) {
      bookMarkStart.appendChild(wr);
     } else {
      Element wtNode = (Element) wtList.item(0);
      wtNode.setTextContent(bookMarkValues.get(bookMarkName));
     }
    }
   }
   Transformer t = TransformerFactory.newInstance().newTransformer();
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   t.transform(new DOMSource(doc), new StreamResult(baos));
   docxOutFile = new ZipOutputStream(new FileOutputStream(outFile));
   @SuppressWarnings("unchecked")
   Enumeration<ZipEntry> entriesIter = (Enumeration<ZipEntry>) docxFile.entries();
   while (entriesIter.hasMoreElements()) {
    ZipEntry entry = null;
    entry = 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);
     int size = 1024;
     byte[] data = new byte[size];
     long totalSize = entry.getSize();
     int n = (int)(totalSize / size);
     int m = (int)(totalSize % size);
     docxOutFile.putNextEntry(new ZipEntry(entry.getName()));
     for(int i = 0; i < n; i++){
      int readCount = incoming.read(data, 0, size);
      docxOutFile.write(data, 0, readCount);
     }
     if(m > 0){
      int readCount = incoming.read(data, 0, m);
      docxOutFile.write(data, 0, readCount);
     }
     docxOutFile.closeEntry();
    }
   }
  } finally {
   if(docxFile != null){
    docxFile.close();
   }
   if(docxOutFile != null){
    docxOutFile.close();
   }
  }
 }

}

0 0
原创粉丝点击