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();
}
}
}
}
- Word 2007文件, 替换书签内容
- c#对Aspose.Word替换书签内容的简单封装
- 用ActiveXObject控件替换word书签,将内容导出到word后打印
- 利用.net替换Word的内容(从数据库中取数据来替换word里面的书签)
- 利用.net替换Word的内容(从数据库中取数据来替换word里面的书签)
- Word 2007 POI 替换文档内容
- 使用Jacob自动替换书签内容
- word vba批量替换文件内容并批量修改文件名
- c#调用word com组件 替换书签套打
- div 动态收缩 html+jsp书签替换div内容
- 文件内容查找替换
- shell 替换文件内容
- java 文件内容替换
- 批处理替换文件内容
- 文件内容替换
- Python 替换文件内容
- linux 替换文件内容
- python 文件内容替换
- Android技术TextView属性详解
- CorePlot 第三方画曲线图饼图方案
- POJ 2377 - Bad Cowtractors(最小生成树)
- 重绘(刷帧)
- UISlider 控件
- Word 2007文件, 替换书签内容
- sublime中f7启动cocos2dx应用的方法
- Mercurial HG windows下nginx流媒体编译脚本备忘
- 小动画练习
- Find 命令 举例
- 定时器学习
- ConcurrentHashMap源码分析
- CISCO 2台路由器静态路由
- 挖掘SimpleSection.o