java 讀寫word java 動態寫入 模板檔案,javaword

來源:互聯網
上載者:User

java 讀寫word java 動態寫入 模板檔案,javaword

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;/** * @author zhangchaochao * @date 2015-12-4上午10:30:59 */public class MSWordPoi4  {            /**     * @param args     */      public static void main(String[] args)      {          Map<String, String> map = new HashMap<String, String>();          map.put("${sub}", "湖南大學");          map.put("${item2.school}", "湖南大學");          map.put("${item2.address}", "湖南");  //        map.put("${item1.name}", "王五1");  //        map.put("${item1.numberStudent}", "編號002");  //        map.put("${item1.sex}", "男2");  //        map.put("${item1.age}", "19");          String srcPath = "D:\\鑄造工藝卡.doc";          readwriteWord(srcPath, map);      }            /**     * 實現對word讀取和修改操作     *      * @param filePath     *            word模板路徑和名稱     * @param map     *            待填充的資料,從資料庫讀取     */      public static void readwriteWord(String filePath, Map<String, String> map)      {          // 讀取word模板          // 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());          }                    //讀取word常值內容          Range range = hdt.getRange();          TableIterator tableIt = new TableIterator(range);           //迭代文檔中的表格          int ii = 0;          while (tableIt.hasNext()) {                Table tb = (Table) tableIt.next();                ii++;              System.out.println("第"+ii+"個表格式資料...................");              //迭代行,預設從0開始              for (int i = 0; i < tb.numRows(); i++) {                    TableRow tr = tb.getRow(i);                    //唯讀前8行,標題部分                  if(i >=8) break;                  //迭代列,預設從0開始                  for (int j = 0; j < tr.numCells(); j++) {                        TableCell td = tr.getCell(j);//取得儲存格                      //取得儲存格的內容                      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("D:/" + 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();          }      }  } 

java 讀寫word java 動態寫入 模板檔案 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.