【Java】java 替换docx文件中的字符串

java 替换docx文件中的字符串

灬都是个谜发布于 2 月 5 日


替换docx文件里面的 ${} 字符串


【Java】java 替换docx文件中的字符串


public class Main {

public static void main(String[] args) throws Exception {

String template = "C:\\Users\\lzh\\Desktop\\模板.docx";

String outSrc = "C:\\Users\\lzh\\Desktop\\简历.docx";

var is = new FileInputStream(template);

var os = new FileOutputStream(outSrc);

editDocx(os, is, xml -> {

Map<String,String> map = new HashMap<>();

map.put("${name}", "李**");

map.put("${sex}", "男");

map.put("${age}", "21");

Pattern p = Pattern.compile("(\\$\\{)([\\w]+)(\\})");

Matcher m = p.matcher(xml);

StringBuffer sb = new StringBuffer();

while (m.find()) {

String group = m.group();

m.appendReplacement(sb, map.get(group));

}

m.appendTail(sb);

xml = sb.toString();

return xml;

});

}

public static void editDocx(OutputStream bos,InputStream is, Process process){

ZipInputStream zin = new ZipInputStream(is);

ZipOutputStream zos = new ZipOutputStream(bos);

try {

ZipEntry entry;

while((entry = zin.getNextEntry()) != null) {

//把输入流的文件传到输出流中 如果是word/document.xml由我们输入

zos.putNextEntry(new ZipEntry(entry.getName()));

if("word/document.xml".equals(entry.getName())){

String xml = new BufferedReader(new InputStreamReader(zin)).lines().collect(Collectors.joining(System.lineSeparator()));

xml = process.process(xml);

ByteArrayInputStream byteIn = new ByteArrayInputStream(xml.getBytes());

int c;

while ((c = byteIn.read()) != -1) {

zos.write(c);

}

byteIn.close();

}else {

int c;

while ((c = zin.read()) != -1) {

zos.write(c);

}

}

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

zos.close();

zin.closeEntry();

zin.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

interface Process {

String process(String xml);

}

javadocxword

阅读 29更新于 2 月 5 日

本作品系原创,采用《署名-非商业性使用-禁止演绎 4.0 国际》许可协议

avatar

灬都是个谜

0 声望

0 粉丝

0 条评论

得票时间

avatar

灬都是个谜

0 声望

0 粉丝

宣传栏


替换docx文件里面的 ${} 字符串


【Java】java 替换docx文件中的字符串


public class Main {

public static void main(String[] args) throws Exception {

String template = "C:\\Users\\lzh\\Desktop\\模板.docx";

String outSrc = "C:\\Users\\lzh\\Desktop\\简历.docx";

var is = new FileInputStream(template);

var os = new FileOutputStream(outSrc);

editDocx(os, is, xml -> {

Map<String,String> map = new HashMap<>();

map.put("${name}", "李**");

map.put("${sex}", "男");

map.put("${age}", "21");

Pattern p = Pattern.compile("(\\$\\{)([\\w]+)(\\})");

Matcher m = p.matcher(xml);

StringBuffer sb = new StringBuffer();

while (m.find()) {

String group = m.group();

m.appendReplacement(sb, map.get(group));

}

m.appendTail(sb);

xml = sb.toString();

return xml;

});

}

public static void editDocx(OutputStream bos,InputStream is, Process process){

ZipInputStream zin = new ZipInputStream(is);

ZipOutputStream zos = new ZipOutputStream(bos);

try {

ZipEntry entry;

while((entry = zin.getNextEntry()) != null) {

//把输入流的文件传到输出流中 如果是word/document.xml由我们输入

zos.putNextEntry(new ZipEntry(entry.getName()));

if("word/document.xml".equals(entry.getName())){

String xml = new BufferedReader(new InputStreamReader(zin)).lines().collect(Collectors.joining(System.lineSeparator()));

xml = process.process(xml);

ByteArrayInputStream byteIn = new ByteArrayInputStream(xml.getBytes());

int c;

while ((c = byteIn.read()) != -1) {

zos.write(c);

}

byteIn.close();

}else {

int c;

while ((c = zin.read()) != -1) {

zos.write(c);

}

}

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

zos.close();

zin.closeEntry();

zin.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

interface Process {

String process(String xml);

}

以上是 【Java】java 替换docx文件中的字符串 的全部内容, 来源链接: www.h5w3.com/113542.html

度小满广告!风险提示:广告信息均来自平台方,不代表平台安全性,不构成建议!
度小满
回到顶部