`
yehengxy
  • 浏览: 10687 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
OperatorRTF RTF模版
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

public class OperatorRTF {

	/**
	 * 
	 * 字符串转换为rtf编码
	 * 
	 * @param content
	 * 
	 * @return
	 * 
	 */
	public String strToRtf(String content) {
		char[] digital = "0123456789ABCDEF".toCharArray();
		StringBuffer sb = new StringBuffer("");
		byte[] bs = content.getBytes();
		int bit;
		for (int i = 0; i < bs.length; i++) {
			bit = (bs[i] & 0x0f0) >> 4;
			sb.append("\\'");
			sb.append(digital[bit]);
			bit = bs[i] & 0x0f;
			sb.append(digital[bit]);
		}
		return sb.toString();
	}

	/**
	 * 替换文档的可变部分
	 * 
	 * @param content
	 * @param replacecontent
	 * @param flag
	 * @return
	 */
	public String replaceRTF(String content, String replacecontent) {
		// 转成RTF编码
		String rc = strToRtf(replacecontent);
		String target = "";
		target = content.replace("$companyName$", "浙江和平钢铁网络有限公司");
		target = target.replace("$goodsName$", "中板");
		target = target.replace("$areaName$", "杭州");
		target = target.replace("$factoryName$", "宝钢");
		target = target.replace("$standardName$", "12*56*89");
		target = target.replace("$materialName$", "Q235");
		target = target.replace("$price$", "1200");
		target = target.replace("$quality$", "120");
		target = target.replace("$piece$", "10");
		target = target.replace("$remark$", "备注");
		return target;
	}

	/**
	 * 获取文件路径
	 * 
	 * @param flag
	 * @return
	 */
	public String getSavePath() {
		String path = "C:\\YQ";
		File fDirecotry = new File(path);
		if (!fDirecotry.exists()) {
			fDirecotry.mkdirs();
		}
		return path;
	}

	/**
	 * 半角转为全角
	 */
	public String ToSBC(String input) {
		char[] c = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == 32) {
				c[i] = (char) 12288;
				continue;
			}
			if (c[i] < 127) {
				c[i] = (char) (c[i] + 65248);
			}
		}
		return new String(c);
	}

	public void rgModel() {
		/* 字节形式读取模板文件内容,将结果转为字符串 */
		String strpath = getSavePath();
		String sourname = strpath + "\\" + "模板.rtf";
		String sourcecontent = "";
		InputStream ins = null;
		try {
			ins = new FileInputStream(sourname);
			byte[] b = new byte[1024];
			if (ins == null) {
				System.out.println("源模板文件不存在");
			}
			int bytesRead = 0;
			while (true) {
				bytesRead = ins.read(b, 0, 1024); // return final read bytes
				if (bytesRead == -1) {// end of InputStream
					System.out.println("读取模板文件结束");
					break;
				}
				sourcecontent += new String(b, 0, bytesRead); // convert to
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//替换模版内容
		sourcecontent=this.replaceRTF(sourcecontent, "");
		/* 结果输出保存到文件 */
		try {
			FileWriter fw = new FileWriter(getSavePath() + "\\" + "new_2.doc",true);
			PrintWriter out = new PrintWriter(fw);
			System.out.println(sourcecontent);
			out.write(sourcecontent);
			out.close();
			fw.close();
			System.out.println(getSavePath() + "  该目录下生成文件" + "new_2.rtf"+" 成功");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		OperatorRTF oRTF = new OperatorRTF();
		/**
		 * 
		 * 被替换内容以"~"符号分割,处理的时候将其拆分为数组即可
		 * 
		 */
		oRTF.rgModel();
	}

}
Global site tag (gtag.js) - Google Analytics