/**
 * 2017-5-15
 */

package com.xfzx.test.POI.main;

import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


/**
 * @author ThinkPad
 *
 */
public class ReportToWord {

    /** 
     * 替换动态文档 
     * @param content 
     * @param markersign 
     * @param replacecontent 
     * @return 
     */  
    public String replaceStr(String content, String markersign,  
            String replacecontent) {  
        //String rc = strToRtf(replacecontent);  
        String rc = encode2HtmlUnicode(replacecontent);  
        String target = "";  
        markersign = "$" + markersign + "$";  
        target = content.replace(markersign, rc);  
        return target;  
    }  
      
    /** 
     * 把给定的str转换为10进制表示的unicode 
     * 目前只是用于mht模板的转码 
     * @param str 
     * @return 
     */  
    public String encode2HtmlUnicode(String str) {  
      
    if(str == null) return "";  
      
    StringBuilder sb = new StringBuilder(str.length() * 2);  
    for (int i = 0; i < str.length(); i++) {  
        sb.append(encode2HtmlUnicode(str.charAt(i)));  
    }  
    return sb.toString();  
    }  
      
    public String encode2HtmlUnicode(char character) {  
    if (character > 255) {  
        return "&#" + (character & 0xffff) + ";";  
    } else {  
        return String.valueOf(character);  
    }  
    }  
      
      
  
    /** 
     * 读取和输出文件 
     * @param inputPath 
     * @param outPath 
     * @param data 
     */  
    public void rgModel(String inputPath, String outPath, Map<String, String> data) {  
        /* 字节形式读取模板文件内容,将结果转为字符串 */  
        String sourname = inputPath;  
        String sourcecontent = "";  
        InputStream ins = null;  
        try {  
            ins = new FileInputStream(sourname);  
            byte[] b = new byte[1638400];// 提高对文件的读取速度,特别是对于1M以上的文件  
            if (ins == null) {  
                System.out.println("源模板文件不存在");  
            }  
            int bytesRead = 0;  
            while (true) {  
                bytesRead = ins.read(b, 0, 1638400);  
                if (bytesRead == -1) {  
                    System.out.println("读取模板文件结束");  
                    break;  
                }  
                sourcecontent += new String(b, 0, bytesRead);  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
          
        /* 修改变化部分 */  
        String targetcontent = "";  
        String oldText = "";  
        Object newValue;  
        /* 结果输出保存到文件 */  
        try {  
            Iterator<String> keys = data.keySet().iterator();  
            int keysfirst = 0;  
            while (keys.hasNext()) {  
                oldText = (String) keys.next();  
                newValue = data.get(oldText);  
                String newText = (String) newValue;  
                if (keysfirst == 0) {  
                    targetcontent = replaceStr(sourcecontent, oldText, newText);  
                    keysfirst = 1;  
                } else {  
                    targetcontent = replaceStr(targetcontent, oldText, newText);  
                    keysfirst = 1;  
                }  
            }  
  
            FileWriter fw = new FileWriter(outPath, true);  
            PrintWriter out = new PrintWriter(fw);  
            if (targetcontent.equals("") || targetcontent == "") {  
                out.println(sourcecontent);  
            } else {  
                out.println(targetcontent);  
            }  
            out.close();  
            fw.close();  
            System.out.println(outPath + " 生成文件成功");  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
      
    public static void main(String[] args) {  
        ReportToWord oRTF = new ReportToWord();  
  
        // *****************************************  
        // 利用HashMap读取数据库中的数据  
        HashMap<String, String> map = new HashMap<String, String>();  
        map.put("timetop", "张三");  
        map.put("info", "0155");  
        map.put("idea", "公元前2000年");  
        map.put("advice", "13");  
        map.put("infosend", "168");  
        // ******************************************  
        oRTF.rgModel("D:\\qst.txt", "d:\\LC-OUT.doc", map);  
        System.out.println("map:"+map);
    }  
}
最近下载更多
微信网友_6540411653607424  LV1 2023年6月29日
1358849392  LV21 2022年12月14日
AaronDjc  LV3 2021年11月23日
yangtianliang  LV1 2021年8月16日
别来无恙  LV1 2021年8月8日
x2b2d2  LV12 2021年7月10日
hyxhyx1234  LV2 2021年4月18日
Destiny微斯人  LV6 2020年5月1日
wangdong123  LV1 2019年12月30日
gt1336296521  LV6 2019年8月10日
最近浏览更多
lmroman  LV8 4月2日
微信网友_6540411653607424  LV1 2023年6月29日
irivn007  LV15 2023年1月28日
anhao5188 2022年12月19日
暂无贡献等级
1358849392  LV21 2022年12月14日
yangxb2  LV10 2022年9月28日
lhblion  LV9 2022年9月21日
1145304128  LV12 2022年9月4日
xueying049  LV5 2022年5月9日
543539666  LV7 2022年3月3日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友