java uses POI tool to read word document and write to generate new word document and error sorting

First, read the word document and write to generate a new word document:

   1. Use the toolkit: poi-version.jar poi-scratchpad-version-beta5.jar or poi-scratchpad-version.jar

   2. Code:   

import java.io.ByteArrayOutputStream;    
import java.io.File;    
import java.io.FileInputStream;    
import java.io.FileNotFoundException;    
import java.io.FileOutputStream;    
import java.io.IOException;    
import java.util.HashMap;    
import java.util.Iterator;    
import java.util.Map;    
    
import org.apache.poi.hwpf.HWPFDocument;    
import org.apache.poi.hwpf.model.FieldsDocumentPart;    
import org.apache.poi.hwpf.usermodel.Field;    
import org.apache.poi.hwpf.usermodel.Fields;    
import org.apache.poi.hwpf.usermodel.Paragraph;    
import org.apache.poi.hwpf.usermodel.Range;    
import org.apache.poi.hwpf.usermodel.Table;    
import org.apache.poi.hwpf.usermodel.TableCell;    
import org.apache.poi.hwpf.usermodel.TableIterator;    
import org.apache.poi.hwpf.usermodel.TableRow;    
    
public class WordTest    
{    
        
    /**  
    * @param args  
    */    
    public static void main(String[] args)    
    {    
        Map<String, String> map = new HashMap<String, String>();    
        map.put("${name}", "张三");    
        map.put("${age}", "20");    
        map.put("${province}", "山西");    
        map.put("${city}", "太原");    
        map.put("${district}", "迎泽区");    
        map.put("${school}", "test");    
        String srcPath = "C:\\Users\\Administrator\\Desktop\\test.doc";    
        readwriteWord(srcPath, map);    
    }    
        
    /**  
    * Realize the operation of reading and modifying word  
    *   
    * @param filePath  
    * word template path and name  
    * @param map  
    * data to be filled, read from database  
    */    
    public static void readwriteWord(String filePath, Map<String, String> map)    
    {    
        // read word template    
        // String fileDir = new    
        // File(base.getFile(),"http://www.cnblogs.com/http://www.cnblogs.com/../doc/").getCanonicalPath();    
        FileInputStream in = null;    
        try    
        {    
            in = new FileInputStream(new File(filePath));    
        }    
        catch (FileNotFoundException e1)    
        {    
            e1.printStackTrace();    
        }    
        HWPFDocument hdt = null;    
        try    
        {    
            hdt = new HWPFDocument(in);    
        }    
        catch (IOException e1)    
        {    
            e1.printStackTrace();    
        }    
        Fields fields = hdt.getFields();    
        Iterator<Field> it = fields.getFields(FieldsDocumentPart.MAIN)    
                .iterator();    
        while (it.hasNext())    
        {    
            System.out.println(it.next().getType());    
        }    
            
        //read word text content    
        Range range = hdt.getRange();    
        TableIterator tableIt = new TableIterator(range);     
        //Iterate the table in the document    
        int ii = 0;    
        while (tableIt.hasNext()) {      
            Table tb = (Table) tableIt. next();      
            ii++;    
            System.out.println("The "+ii+" table data.......");    
            //Iterate over rows, default from 0 starts    
            for (int i = 0; i < tb.numRows(); i++) {      
                TableRow tr = tb.getRow(i);      
                //read only the first 8 rows, header part    
                if(i >=8) break;    
                //iteration Column, starting from 0 by default    
                for (int j = 0; j < tr.numCells(); j++) {      
                    TableCell td = tr.getCell(j);//Get the cell    
                    //Get the content of the cell    
                    for(int k =0;k<td.numParagraphs();k++){      
                        Paragraph para =td.getParagraph(k);      
                        String s = para.text();      
                        System.out.println(s);    
                    } //end for       
                }   //end for    
            }   //end for    
        } //end while    
        //System.out.println(range.text());    
            
        // 替换文本内容    
        for (Map.Entry<String, String> entry : map.entrySet())    
        {    
            range.replaceText(entry.getKey(), entry.getValue());    
        }    
        ByteArrayOutputStream ostream = new ByteArrayOutputStream();    
        String fileName = "" + System.currentTimeMillis();    
        fileName += ".doc";    
        FileOutputStream out = null;    
        try    
        {    
            out = new FileOutputStream("F:/" + fileName, true);    
        }    
        catch (FileNotFoundException e)    
        {    
            e.printStackTrace();    
        }    
        try    
        {    
            hdt.write(ostream);    
        }    
        catch (IOException e)    
        {    
            e.printStackTrace();    
        }    
        // 输出字节流    
        try    
        {    
            out.write(ostream.toByteArray());    
        }    
        catch (IOException e)    
        {    
            e.printStackTrace();    
        }    
        try    
        {    
            out.close();    
        }    
        catch (IOException e)    
        {    
            e.printStackTrace();    
        }    
        try    
        {    
            ostream.close();    
        }    
        catch (IOException e)    
        {    
            e.printStackTrace();    
        }    
    }    

 

2. Problem sorting:

    Error content: org.apache.poi.poifs.filesystem.POIFSFileSystem.getRoot()

Lorg/apache/poi/poifs/filesystem/DirectoryNode error!

    Solution: Upgrade the poi-version number.jar package

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325202290&siteId=291194637