【java】在word指定书签处插入多个字符串
1. 插入多个字符串,不加换行符
/** * 插入字符串列表 * * @param node * @param bookmarkName 书签名称 * @param list */ public static void writeStringList(Node node, String bookmarkName, List<String> list) { try { var doc = (Document) node.getDocument(); var bm = node.getRange().getBookmarks().get(bookmarkName); var builder = new DocumentBuilder(doc); builder.moveToBookmark(bookmarkName); for (var i : list) { builder.write(i); // 要插入换行符可以在字符串尾部加"\n" } bm.remove(); } catch (Exception e) { log.error("插入字符串列表异常: error = {}", e.getMessage()); throw new BadException(e.getMessage()); } }
2. 插入多个段落,每个字符串后加一个换行符
/** * 写入多个段落 * * @param node * @param bookmarkName 书签名称 * @param list */ public static void writeParagraphs(Node node, String bookmarkName, List<String> list) { try { var doc = (Document) node.getDocument(); var bm = node.getRange().getBookmarks().get(bookmarkName); var builder = new DocumentBuilder(doc); builder.moveToBookmark(bookmarkName); for (var i : list) { builder.writeln(i); } bm.remove(); } catch (Exception e) { log.error("插入段落异常: error = {}", e.getMessage()); throw new BadException(e.getMessage()); } }