国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

java讀寫wordjava動態寫入模板文件

2019-11-14 14:54:33
字體:
來源:轉載
供稿:網友
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://m.survivalescaperooms.com/http://m.survivalescaperooms.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 動態寫入 模板文件下載地址 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长海县| 灵台县| 成武县| 永康市| 额尔古纳市| 普宁市| 黔西| 常山县| 天镇县| 宜州市| 博白县| 灵宝市| 湘阴县| 常德市| 平泉县| 葫芦岛市| 郓城县| 葵青区| 宁明县| 芷江| 溆浦县| 平凉市| 昌乐县| 石河子市| 新泰市| 裕民县| 巴中市| 普安县| 东乡族自治县| 鄱阳县| 平定县| 吉木萨尔县| 淮南市| 陵川县| 边坝县| 樟树市| 德兴市| 汝南县| 神农架林区| 沭阳县| 麦盖提县|