一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

java怎么读word文档?Java读取并写入Word文件的实例代码

时间:2017-02-15 编辑:简简单单 来源:一聚教程网

  1.  代码如下复制代码

  2. /* 

  3.  * 文 件 名:  MSWordPoi4.java 

  4.  * 版    权:  Sunny Technologies Co., Ltd. Copyright YYYY-YYYY,  All rights reserved 

  5.  * 描    述:  

    <描述> 
  6.  * 修 改 人:  L.Hao 

  7.  * 修改时间:  2014-8-8 

  8.  * 跟踪单号:  

    <跟踪单号> 
  9.  * 修改单号:  

    <修改单号> 
  10.  * 修改内容:  

    <修改内容> 
  11.  */  

  12. package com.test;  

  13. /** 

  14.  * 

    <一句话功能简述> 
  15.  * 

    <功能详细描述> 
  16.  *  

  17.  * @author  L.Hao 

  18.  * @version  [版本号, 2014-8-8] 

  19.  * @see  [相关类/方法] 

  20.  * @since  [产品/模块版本] 

  21.  */  

  22. import java.io.ByteArrayOutputStream;  

  23. import java.io.File;  

  24. import java.io.FileInputStream;  

  25. import java.io.FileNotFoundException;  

  26. import java.io.FileOutputStream;  

  27. import java.io.IOException;  

  28. import java.util.HashMap;  

  29. import java.util.Iterator;  

  30. import java.util.Map;  

  31. import org.apache.poi.hwpf.HWPFDocument;  

  32. import org.apache.poi.hwpf.model.FieldsDocumentPart;  

  33. import org.apache.poi.hwpf.usermodel.Field;  

  34. import org.apache.poi.hwpf.usermodel.Fields;  

  35. import org.apache.poi.hwpf.usermodel.Paragraph;  

  36. import org.apache.poi.hwpf.usermodel.Range;  

  37. import org.apache.poi.hwpf.usermodel.Table;  

  38. import org.apache.poi.hwpf.usermodel.TableCell;  

  39. import org.apache.poi.hwpf.usermodel.TableIterator;  

  40. import org.apache.poi.hwpf.usermodel.TableRow;  

  41. public class MSWordPoi4  

  42. {  

  43.     /** 

  44.     * @param args 

  45.     */  

  46.     public static void main(String[] args)  

  47.     {  

  48.         Map

     map = new HashMap();  
  49.         map.put("${sub}", "湖南大学");  

  50.         map.put("${item2.school}", "湖南大学");  

  51.         map.put("${item2.address}", "湖南");  

  52. //        map.put("${item1.name}", "王五1");  

  53. //        map.put("${item1.numberStudent}", "编号002");  

  54. //        map.put("${item1.sex}", "男2");  

  55. //        map.put("${item1.age}", "19");  

  56.         String srcPath = "D:\\temp\\铸造工艺卡.doc";  

  57.         readwriteWord(srcPath, map);  

  58.     }  

  59.     /** 

  60.     * 实现对word读取和修改操作 

  61.     *  

  62.     * @param filePath 

  63.     *            word模板路径和名称 

  64.     * @param map 

  65.     *            待填充的数据,从数据库读取 

  66.     */  

  67.     public static void readwriteWord(String filePath, Map

     map)  
  68.     {  

  69.         // 读取word模板  

  70.         // String fileDir = new  

  71.         // File(base.getFile(),"http://www.cnblogs.com/http://www.cnblogs.com/../doc/").getCanonicalPath();  

  72.         FileInputStream in = null;  

  73.         try  

  74.         {  

  75.             in = new FileInputStream(new File(filePath));  

  76.         }  

  77.         catch (FileNotFoundException e1)  

  78.         {  

  79.             e1.printStackTrace();  

  80.         }  

  81.         HWPFDocument hdt = null;  

  82.         try  

  83.         {  

  84.             hdt = new HWPFDocument(in);  

  85.         }  

  86.         catch (IOException e1)  

  87.         {  

  88.             e1.printStackTrace();  

  89.         }  

  90.         Fields fields = hdt.getFields();  

  91.         Iterator

     it = fields.getFields(FieldsDocumentPart.MAIN)  
  92.                 .iterator();  

  93.         while (it.hasNext())  

  94.         {  

  95.             System.out.println(it.next().getType());  

  96.         }  

  97.         //读取word文本内容  

  98.         Range range = hdt.getRange();  

  99.         TableIterator tableIt = new TableIterator(range);   

  100.         //迭代文档中的表格  

  101.         int ii = 0;  

  102.         while (tableIt.hasNext()) {    

  103.             Table tb = (Table) tableIt.next();    

  104.             ii++;  

  105.             System.out.println("第"+ii+"个表格数据...................");  

  106.             //迭代行,默认从0开始  

  107.             for (int i = 0; i < tb.numRows(); i++) {    

  108.                 TableRow tr = tb.getRow(i);    

  109.                 //只读前8行,标题部分  

  110.                 if(i >=8) break;  

  111.                 //迭代列,默认从0开始  

  112.                 for (int j = 0; j < tr.numCells(); j++) {    

  113.                     TableCell td = tr.getCell(j);//取得单元格  

  114.                     //取得单元格的内容  

  115.                     for(int k=0;k

  116.                         Paragraph para =td.getParagraph(k);    

  117.                         String s = para.text();    

  118.                         System.out.println(s);  

  119.                     } //end for     

  120.                 }   //end for  

  121.             }   //end for  

  122.         } //end while  

  123.         //System.out.println(range.text());  

  124.         // 替换文本内容  

  125.         for (Map.Entry

     entry : map.entrySet())  
  126.         {  

  127.             range.replaceText(entry.getKey(), entry.getValue());  

  128.         }  

  129.         ByteArrayOutputStream ostream = new ByteArrayOutputStream();  

  130.         String fileName = "" + System.currentTimeMillis();  

  131.         fileName += ".doc";  

  132.         FileOutputStream out = null;  

  133.         try  

  134.         {  

  135.             out = new FileOutputStream("F:/" + fileName, true);  

  136.         }  

  137.         catch (FileNotFoundException e)  

  138.         {  

  139.             e.printStackTrace();  

  140.         }  

  141.         try  

  142.         {  

  143.             hdt.write(ostream);  

  144.         }  

  145.         catch (IOException e)  

  146.         {  

  147.             e.printStackTrace();  

  148.         }  

  149.         // 输出字节流  

  150.         try  

  151.         {  

  152.             out.write(ostream.toByteArray());  

  153.         }  

  154.         catch (IOException e)  

  155.         {  

  156.             e.printStackTrace();  

  157.         }  

  158.         try  

  159.         {  

  160.             out.close();  

  161.         }  

  162.         catch (IOException e)  

  163.         {  

  164.             e.printStackTrace();  

  165.         }  

  166.         try  

  167.         {  

  168.             ostream.close();  

  169.         }  

  170.         catch (IOException e)  

  171.         {  

  172.             e.printStackTrace();  

  173.         }  

  174.     }  

所使用到的jar是:

热门栏目