资源简介

部分代码.................. . .. .. /** * 处理动态表格 * 默认取第二行第一列的标识 * @param table * @param ctRow * @param values 需要添加的数据 */ public static void processDynamicTable(XWPFTable table, CTRow ctRow, List values) { if(values==null || values.size()==0) return; for (Iterator iterator = values.iterator(); iterator.hasNext();) { String[] value = iterator.next(); XWPFTableRow newRow = new XWPFTableRow(ctRow,table); List cells = newRow.getTableCells(); if(cells==null || cells.size()==0) return; for (int j = 0; j < cells.size(); j++) { XWPFTableCell cell = cells.get(j); cell.removeParagraph(0); if(j<=value.length) cell.setText(value[j]); } table.addRow(newRow); } } /** * 处理表格 * @param doc * @param dataMap */ public static void processTable(XWPFDocument doc, Map dataMap) { Iterator iter = doc.getTablesIterator(); while(iter.hasNext()) { XWPFTable table = iter.next(); List rows = table.getRows(); if(rows==null || rows.size()==0) continue; CTRow ctRow = null; for (int i = 0; i < rows.size(); i++) { XWPFTableRow row = rows.get(i); ctRow = (CTRow)row.getCtRow().copy(); List cells = row.getTableCells(); if(cells==null || cells.size()==0) continue; //处理标致 boolean sign = false; for (int j = 0; j < cells.size(); j++) { XWPFTableCell cell = cells.get(j); if(cell.getText()==null) continue; if(i==1 && j==0) { //取第二行第一列的值,再添加table.关键字之后与dataMap匹配,有则当动态表格处理 String key = "table."+ cell.getText(); if(dataMap.containsKey(key)) { List values = (List)dataMap.get(key); table.removeRow(1); processDynamicTable(table, ctRow, values); //更新处理标致 sign = true; break; } } if(!cell.getText().startsWith("\\$") && cell.getText().endsWith("\\$")) continue; String key = cell.getText().replaceAll("\\$", ""); if(dataMap.containsKey(key)) {

资源截图

代码片段和文件信息

package net.gbicc.re;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.xmlWriter;

/**
 *  Class Name: DelWordByTable.java
 *  @author zhangxu.wu
 *  DateTime 2011-12-21 上午09:24:10    
 *  

Description:   


 *  

Copyright:Copyright (c) 2011  


 */

public class DelWordByTable {

public static String deleteID = “DELETE“;
public static void main(String[] args) {

String dir=“c:\\“;
String zipfile =“wordtest.docx.zip“;
String destDir =“desdir“;
String fileName = “document.xml“;
String newFileName =“C:\\upload.docx“;

try {
//解压缩zip包
ZipUtil.extZipFileList(dir + zipfile dir + destDir);

InputStream input;
input = new FileInputStream(dir + destDir+“\\word\\“ + fileName);
Document document = readxml(input);
byte[] byre = copyDocumentToByteArray(document“UTF-8“);
ZipUtil.isExist(dir + destDir+“\\word\\“ + fileName);
OutputStream op = new FileOutputStream(dir + destDir+“\\word\\“ + fileName);
            op.write(byre);
            op.close();
            
           //压缩zip包
            ZipUtil.zip(“c:\\upload.docx.zip“ dir + destDir);
            
           //重命名upload.docx.zip 为  upload.docx
            ZipUtil.reName(“c:\\upload.docx.zip“newFileName);
            
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch(Exception e) {

}
System.out.println(“sucessful!“);
}


public static byte[] copyDocumentToByteArray(Document document String encoding) {
        if (document == null) {
            return null;
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding(encoding);
        try {
            xmlWriter output = new xmlWriter(bos format);
            output.write(document);
            output.close();
        } catch (IOException ex) {
            ex.printStackTrace();
            throw new IllegalArgumentException(ex.getMessage());
        }
        return bos.toByteArray();
    }

@SuppressWarnings(“unchecked“)
public static Document readxml(InputStream input){
SAXReader saxReader = new SAXReader();
 try {
Document document = saxReader.read(input);
Element eml = (Element) document.getRootElement();
List list = eml.elements();
if(list!=null&& list.size()>0) {
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
Element element = (Element) iterator.next();

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       1450  2011-12-14 17:39  POI\.classpath

     文件        379  2011-12-14 17:37  POI\.project

     文件         88  2011-12-14 17:39  POI\.settings\org.eclipse.core.resources.prefs

     文件        629  2011-12-14 17:37  POI\.settings\org.eclipse.jdt.core.prefs

     文件       6207  2012-02-07 11:43  POI\bin\net\gbicc\re\DelWordByTable.class

     文件       4561  2012-02-15 10:16  POI\bin\net\gbicc\re\FixedAndTuple.class

     文件       4982  2011-12-30 13:18  POI\bin\net\gbicc\re\Header.class

     文件       3207  2011-12-30 11:37  POI\bin\net\gbicc\re\TableRC.class

     文件       5429  2011-12-30 13:12  POI\bin\net\gbicc\re\ZipUtil.class

     文件       5327  2011-12-26 12:38  POI\bin\net\gbicc\word\CustomXWPFDocument.class

     文件       3485  2012-02-15 10:26  POI\bin\net\gbicc\word\PoiTest.class

     文件       5753  2011-12-26 12:38  POI\bin\net\gbicc\word\PoiUtil.class

     文件       3522  2011-12-26 12:38  POI\bin\net\gbicc\word\readWordByTable.class

     文件       4212  2011-12-29 16:02  POI\bin\net\gbicc\word\TRD.class

     文件       3497  2011-12-14 16:38  POI\bin\resource\logo.gif

     文件      22521  2012-02-15 10:26  POI\bin\resource\test_out.docx

     文件      23430  2011-12-16 16:14  POI\bin\resource\wordtest.docx

     文件     156275  2011-12-14 16:38  POI\bin\resource\wordtest.xml

     文件      67983  2011-12-14 16:38  POI\bin\resource\wordtestaa.xml

     文件      52915  2011-12-14 17:25  POI\lib\commons-logging-1.1.jar

     文件     313898  2011-12-14 17:25  POI\lib\dom4j-1.6.1.jar

     文件     513409  2011-12-14 17:34  POI\lib\dom4j-full.jar

     文件      28592  2011-12-14 17:25  POI\lib\geronimo-stax-api_1.0_spec-1.0.jar

     文件     121070  2011-12-14 17:25  POI\lib\junit-3.8.1.jar

     文件     358180  2011-12-14 17:25  POI\lib\log4j-1.2.13.jar

     文件    1675036  2011-12-14 16:38  POI\lib\poi-3.7-20101029.jar

     文件     264108  2011-12-14 16:38  POI\lib\poi-examples-3.7-20101029.jar

     文件     498259  2011-12-14 16:38  POI\lib\poi-ooxml-3.7-20101029.jar

     文件    3967696  2011-12-14 16:38  POI\lib\poi-ooxml-schemas-3.7-20101029.jar

     文件     840218  2011-12-14 16:38  POI\lib\poi-scratchpad-3.7-20101029.jar

............此处省略35个文件信息

评论

共有 条评论