批量修改word文档并保存

*重点:本文章只支持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();
	}
}

转载请标明出处: http://www.yueshuge.cn/?p=157

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值