package com.hicom.support;import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Enumeration;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Map.Entry;import java.util.zip.ZipEntry;import java.util.zip.ZipFile;import java.util.zip.ZipOutputStream;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerFactory;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import org.apache.poi.POIXMLDocument;import org.apache.poi.poifs.filesystem.DirectoryEntry;import org.apache.poi.poifs.filesystem.DocumentEntry;import org.apache.poi.poifs.filesystem.POIFSFileSystem;import org.apache.poi.xwpf.extractor.XWPFWordExtractor;import org.apache.poi.xwpf.usermodel.XWPFDocument;import org.apache.poi.xwpf.usermodel.XWPFTable;import org.apache.poi.xwpf.usermodel.XWPFTableCell;import org.apache.poi.xwpf.usermodel.XWPFTableRow;import org.w3c.dom.Document;import org.w3c.dom.Element;import com.hicom.cuijiao.model.CbcjStat;public class WordContral {public static void searchAndReplace(String srcPath ,String destPath, Object obj) { try { CbcjStat cj = (CbcjStat)obj; ZipFile docxFile = new ZipFile(new File(srcPath)); 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; } Date dt=new Date(); SimpleDateFormat y1 = new SimpleDateFormat("yyyy"); SimpleDateFormat m1 = new SimpleDateFormat("MM"); SimpleDateFormat d1 = new SimpleDateFormat("dd"); String y = y1.format(dt); String m = m1.format(dt); String d = d1.format(dt); s = s.replaceAll("number", ""); s = s.replaceAll("year1", cj.getSsqs().substring(0, 4)); s = s.replaceAll("year2", cj.getSsqz().substring(0, 4)); s = s.replaceAll("m1", cj.getSsqs().substring(4)); s = s.replaceAll("m2", cj.getSsqz().substring(4)); s = s.replaceAll("□□", "√□"); // 签收单位(公章) String c =cj.getDwmc(); s = s.replaceAll("gonghui", cj.getGhmc()); s = s.replaceAll("danwei", cj.getDwmc()); s = s.replaceAll("qsr", cj.getLxr()); //签名的年月日 s = s.replaceAll("Y", y); s = s.replaceAll("M", m); s = s.replaceAll("D", d); // System.out.println("myword:"+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(); Element bodyElement = (Element) docElement.getElementsByTagName( "w:body").item(0); Element pElement = (Element) bodyElement.getElementsByTagName("w:p") .item(0); Element rElement = (Element) pElement.getElementsByTagName("w:r").item( 0); Element tElement = (Element) rElement.getElementsByTagName("w:t").item( 0); System.out.println("hacran:"+tElement.getTextContent()); Transformer t = TransformerFactory.newInstance().newTransformer(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); t.transform(new DOMSource(doc), new StreamResult(baos)); ZipOutputStream docxOutFile = new ZipOutputStream(new FileOutputStream(destPath)); Enumeration entriesIter = (Enumeration) docxFile .entries(); while (entriesIter.hasMoreElements()) { ZipEntry entry = entriesIter.nextElement();// System.out.println("entry.getName()-->> "+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 { 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(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception { HashMap map=new HashMap(); String srcPath = "D:\\工会催报催缴\\上海市工会经费(筹备金)催缴通知书.docx"; String destPath = "D:\\工会催报催缴\\上海市工会经费(筹备金)催缴通知书2.doc"; searchAndReplace(srcPath ,destPath,""); }}
读写word文档
最新推荐文章于 2024-03-04 21:33:53 发布