使用 Apache POI 操作 Word 文件的一些公共方法或示例
除了 extractText 方法外,其他所有方法均只支持 .docx 文件。
| Modifiers | Name | Description |
|---|---|---|
static class |
WordUtils.Picture |
插入到文档中的图片定义 |
| Type | Name and description |
|---|---|
static void |
addParagraph(org.apache.poi.xwpf.usermodel.XWPFDocument doc, String text)在文档末尾添加一段文字 |
static void |
addPicture(org.apache.poi.xwpf.usermodel.XWPFRun run, WordUtils.Picture picture)插入图片 |
static void |
appendText(org.apache.poi.xwpf.usermodel.XWPFRun run, String text)追加文字 |
static void |
copyAndAppendRows(org.apache.poi.xwpf.usermodel.XWPFTableRow row, Integer count, Map options = null)复制行,并插入到后面 |
static String |
extractText(String filename)提取文档中的所有文本 |
static Map<String, List<org.apache.poi.xwpf.usermodel.XWPFRun>> |
findAllParams(org.apache.poi.xwpf.usermodel.XWPFDocument doc)在文档里面搜索所有变量(形如 ${xxx} 的文本) |
static org.apache.poi.xwpf.usermodel.XWPFDocument |
openDocument(String filename)打开 .docx 文档 |
static org.apache.poi.xwpf.usermodel.XWPFDocument |
openDocument(java.io.File file) |
static org.apache.poi.xwpf.usermodel.XWPFDocument |
openDocument(java.io.InputStream is) |
static void |
removeTableBorder(org.apache.poi.xwpf.usermodel.XWPFTable table)设置表格的边框为“无” |
static void |
render(Object doc, String filename, javax.servlet.http.HttpServletRequest request = null, javax.servlet.http.HttpServletResponse response = null)将 Word 文件输出到浏览器 |
static void |
replaceParams(org.apache.poi.xwpf.usermodel.XWPFDocument doc, Map<String, Object> params)替换文档里面的变量 |
static java.io.File |
saveDocument(org.apache.poi.xwpf.usermodel.XWPFDocument doc, String filename)保存 .docx 文档 |
static BigDecimal |
setTableWidthPct100(org.apache.poi.xwpf.usermodel.XWPFTable table, Boolean wpsCompatible = true)设置表格的宽度为 100% |
static void |
setText(org.apache.poi.xwpf.usermodel.XWPFRun run, String text)替换文字 |
| Methods inherited from class | Name |
|---|---|
class Object |
Object#wait(long, int), Object#wait(long), Object#wait(), Object#equals(Object), Object#toString(), Object#hashCode(), Object#getClass(), Object#notify(), Object#notifyAll() |
在文档末尾添加一段文字
插入图片
追加文字
复制行,并插入到后面
row - 要复制的行count - 插入的行数options.callback - 每插入一个新行后的回掉闭包,接收新插入的行的序号和对象(Integer i, XWPFTableRow newRow)作为参数提取文档中的所有文本
在文档里面搜索所有变量(形如 ${xxx} 的文本)
打开 .docx 文档
这里只能打开二进制的文件;
即便是 Office 另存的 XML 文件,也会抛出 NotOfficeXmlFileException 或 NotOLE2FileException 异常:
The supplied data appears to be a raw XML file. Formats such as Office 2003 XML are not supported
设置表格的边框为“无”
将 Word 文件输出到浏览器
doc - 文档对象(XWPFDocument 或 HWPFDocument类型)替换文档里面的变量
制作 Word 模板时,务必关闭 “选项/校对/键入时检查拼写”,否则变量可能无法识别(也可以在控制台中调用方法 findAllParams 检查)。
模板中的变量为形如 ${xxx} 的文本,支持 ${foo.bar.xyz} 的形式。
支持插入图片:如果 params 中的变量值为 WordUtils.Picture 类型,则自动替换为图片。
支持插入多行数据:如果 params 中的变量值为列表类型,则自动复制变量所在的模板行、并依次写入数据。
检查变量的代码示例:def template = ctx.getBean(foo.oa.hr.HrAskForLeaveProcessController).findTemplateFile("import-export", "export.docx")
def doc = WordUtils.openDocument(template)
WordUtils.findAllParams(doc).keySet()
doc - 要替换的文档params - 形如 [变量名: 变量值] 的数据保存 .docx 文档
doc - 文档对象filename - 文件名设置表格的宽度为 100%
替换文字