国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

java 替換docx文件中的字符串方法實現

瀏覽:98日期:2022-08-17 08:50:41

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

java 替換docx文件中的字符串方法實現

public class Main { public static void main(String[] args) throws Exception { String template = 'C:UserslzhDesktop模板.docx'; String outSrc = 'C:UserslzhDesktop簡歷.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 替換docx文件中的字符串方法實現的文章就介紹到這了,更多相關java 替換docx字符串內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
主站蜘蛛池模板: 太白县| 调兵山市| 松溪县| 循化| 北流市| 富顺县| 聂拉木县| 南昌县| 聂拉木县| 龙里县| 九江市| 郓城县| 宽城| 禹城市| 莎车县| 府谷县| 衡南县| 漯河市| 大同县| 泰顺县| 平安县| 阳高县| 突泉县| 漳州市| 樟树市| 吉木乃县| 屏东县| 宜城市| 望谟县| 昆山市| 镇赉县| 涿州市| 郎溪县| 竹北市| 汨罗市| 兴宁市| 铜鼓县| 平湖市| 禹州市| 南昌市| 嘉祥县|