java替换word2003

map.put("year", year);
            map.put("yearMonthDay", yearMonthDay);
            map.put("month", month);
            map.put("xun", xun);
            map.put("lastMonth", lastMonth);
            map.put("lastXun", lastXun);
            map.put("dwAvgTemp", dwAvgTemp);
            map.put("dwhistoryTemp", dwhistoryTemp);
            map.put("dwhistoryRain", dwhistoryRain);
            map.put("psAvgTemp", psAvgTemp);
            map.put("pshistoryTemp", pshistoryTemp);
            map.put("pshistoryRain", pshistoryRain);
            map.put("maxTemp", maxTemp);
            map.put("minTemp", minTemp);
            map.put("siteName", siteName);
            map.put("siteName2", siteName2);

/**
     * 开始替换word数据
     * @param map 要替换的map
     * @throws Exception
     */
    private void Write(Map<String,String> map) throws Exception {
          String templatePath = "D:\\中期天气预报.doc";  
          String outPath = "D:\\write.doc";
          String fileName = "write.doc";
          try {
                FileInputStream fis = new FileInputStream(new File(templatePath));
                HWPFDocument doc = new HWPFDocument(fis);
                Range bodyRange = doc.getRange();
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    String key = entry.getKey();
                    String value =  entry.getValue();
                    bodyRange.replaceText("${" + key + "}", value);
                }
                ByteArrayOutputStream ostream = new ByteArrayOutputStream();
                try{
                    doc.write(ostream);
                }catch(Exception ex){
                    ex.printStackTrace();
                }
                OutputStream outs=new FileOutputStream(outPath);
                outs.write(ostream.toByteArray());
                outs.close();
                fis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
         
    }

时间: 2024-07-31 12:25:10

java替换word2003的相关文章

java替换字符串和用indexof查找字符

java自带替换 String s="hlz_and_hourui哈哈"; String new_S=s.replaceAll("哈", "笑毛"); System.out.println(new_S); 则输出为:"hlz_and_hourui笑毛笑毛"; 1 package find_repalce_keywords; 2 3 import java.io.BufferedReader; 4 import java.io.

Java 替换空格

题目描述 请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 解一: 由于最近在学习Head First Java,所以最先考虑到的就是使用字符串拼凑,思路很简单,没有考虑什么时间复杂度,直接看代码. 1 public class Solution { 2 public String replaceSpace(StringBuffer str) { 3 String str1=str.toSt

java替换文本中所有的正则符号

开发时遇到一个需求,需要对一段文本中的所有正则符号进行转义,不然使用split分割方法分割文本的话无效,想到用替换来做,全部替换正则符号为转义后的符号 贴java实现代码: 1.测试版 public static void main(String[] args) { String[] symbols = new String[] { "\\\\", "\\/", "\\[", "\\]", "\\(", &

java替换包含html标签

说明一下,该文档内容抄自开源中国里的谋篇文章,由于抄袭时间过于久远,已经找不到博主了!暂不能说明出处,源码博主看见勿气,皆可联系本人! 我的博客,文章属于个人收藏,以解日后需要之急! package q; import java.util.regex.Matcher; import java.util.regex.Pattern; public class htmlTest { private static final String regEx_script = "<script[^>

Java替换字符或十进制数的字符串

如今,这个项目的需求:将"甲状腺结节 5*3 cm" 更换 "甲状腺结节 * cm". 在一个字符串的数字来替换空白. 码,如以下: public static String subString(String str) { String regex = "\\d*"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(str); while (m.find()) { if (!&q

java 替换四个字节的字符 ‘\xF0\x9F\x98\x84\xF0\x9F)的解决方案 ??

/** * 替换四个字节的字符 '\xF0\x9F\x98\x84\xF0\x9F)的解决方案 ?? * @author ChenGuiYong * @data 2015年8月11日 上午10:31:50 * @param content * @return */ public static String removeFourChar(String content) { byte[] conbyte = content.getBytes(); for (int i = 0; i < conbyt

java替换字符串中的World为Money

public class Money{ public static void main(String[] args) { String a = "Hello Java World\n"+ "Hello Java Hello World"; System.out.println(a.replace("World","Money")); }} 原文地址:https://www.cnblogs.com/THEONLYLOVE/p/9

java 替换字符串中的中括号

正确方式:"[adbdesf]".replaceAll("\\[", "").replaceAll("\\]", "") 错误方式:"[adbdesf]".replaceAll("[", "").replaceAll("]", "") "[adbdesf]".replaceAll(&quo

java:替换字符串中的ASCII码

可对照查看网盘ASCII表 public static void main(String[] args) { // // TODO Auto-generated method stub int a=001; int b=32;//制表符 int c=13;//回车键 char character = (char)a; char cb = (char)b; String line ="niaho" + character + "woma"; String ar= St