批量修改word文件並儲存
阿新 • • 發佈:2019-01-04
*重點:本文章只支援DOC文件,不支援DOCX*
需求:由於驗收需要提交試執行記錄表(週報),有2套文件需要提交,從15年10月份開始至今,大概200多份文件,每份文件預計需要花1分鐘,如果手工,那麼大概需要4-5個小時,而且容易錯誤,還好我是程式設計師。
package com.hy.servicedetection.util; 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.text.SimpleDateFormat; import java.util.Date; 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.Range; /** * *功能說明:word讀取寫入工具類 * *建立時間:2017-12-26 下午4:56:54 * *修改人 修改時間 修改描述 * * * */ public class WordUtil { /** * 文件物件 */ private static HWPFDocument hdt = null; public static void main(String[] args) { WordUtil.doTest(); } public static void doTest(){ String writeUrl = "D:\\001\\測試文件(?).doc"; try { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat strFormat = new SimpleDateFormat("yyyy年MM月dd日"); SimpleDateFormat mlFormat = new SimpleDateFormat("yyyy.MM.dd"); Date currDate = dateFormat.parse("2015-09-28"); String startDate = null; String endDate = null; String writeDate = null; String mlStartDate = null; String mlEndDate = null; while(true){ int week = DateUtil.getDayWeek(currDate); if(week == 1){ startDate = strFormat.format(currDate); mlStartDate = mlFormat.format(currDate); } if(week == 0){ endDate = strFormat.format(currDate); writeDate = strFormat.format(DateUtil.nextDay(currDate)); mlEndDate = mlFormat.format(currDate); Map<String, String> map = new HashMap<String, String>(); map.put("params1", startDate); map.put("params2", endDate); map.put("params3", writeDate); Range range = readWord("D:\\001\\測試文件(2015.10).doc"); //修改檔名及檔案內容s writeWord(writeUrl.replace("?", mlStartDate+"~"+mlEndDate), range,map); } if(currDate.getTime() > new Date().getTime()){ break; } currDate = DateUtil.nextDay(currDate); } } catch (Exception e) { } } /** * *功能說明:讀取word文件內容 *輸入引數:地址string *輸出引數:文字物件 *建立時間:2017-12-26 下午5:01:09 * */ public static Range readWord(String filePath){ FileInputStream in = null; try { in = new FileInputStream(new File(filePath)); hdt = new HWPFDocument(in); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } Fields fields = hdt.getFields(); Iterator it = fields.getFields(FieldsDocumentPart.MAIN) .iterator(); while (it.hasNext()) { System.out.println(it.next().getType()); } return hdt.getRange(); } /** * *功能說明:將修改後的內容寫入word文件 *輸入引數:文件地址fileName(可不存在),文字物件,替換物件(可為文字的具體內容) *輸出引數:無 *建立時間:2017-12-26 下午5:02:46 * */ public static void writeWord(String fileName, Range range, Map<String, String> map){ // 替換文字內容 for (Map.Entry<String, String> entry : map.entrySet()) { range.replaceText(entry.getKey(), entry.getValue()); } ByteArrayOutputStream ostream = new ByteArrayOutputStream(); FileOutputStream out = null; try { //建立或覆蓋檔案 out = new FileOutputStream(fileName, true); //寫入內容 hdt.write(ostream); out.write(ostream.toByteArray()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ try { out.close(); ostream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
使用到的時間工具類如下:
package com.hy.servicedetection.util; import java.util.Date; /** * *功能說明:時間工具類 * *建立人: * *建立時間:2017-12-29 下午8:32:12 * *修改人 修改時間 修改描述 * * * */ @SuppressWarnings("deprecation") public final class DateUtil { /** * *功能說明:時間加一天 *建立時間:2017-12-29 下午8:33:36 * */ public static java.util.Date nextDay(java.util.Date date) { java.util.Calendar c1 = new java.util.GregorianCalendar(); c1.setTime(date); c1.setTimeInMillis(c1.getTimeInMillis() + 24 * 60 * 60 * 1000); return c1.getTime(); } /** * *功能說明:查詢今天是星期幾 (0,1,2,3,4,5,6)對應(日、一、二、三、四、五、六) *建立時間:2017-12-29 下午8:34:26 * */ public static int getDayWeek(Date date){ return date.getDay(); } }