直接上代码:
import java.io.FileOutputStream; import java.math.BigInteger; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFFootnotes; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.xmlbeans.impl.xb.xmlschema.SpaceAttribute; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdn; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdnRef; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHighlight; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHpsMeasure; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTUnderline; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVerticalAlignRun; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STFtnEdn; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHighlightColor; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun; public class POI_创建脚注_S3_Test { public static void main(String[] args) throws Exception { POI_创建脚注_S3_Test t = new POI_创建脚注_S3_Test(); t.testAddFootnotesToDocument("f:/saveFile/temp/sys_" + System.currentTimeMillis() + ".docx"); } public void testAddFootnotesToDocument(String savePath) throws Exception { XWPFDocument xdoc = new XWPFDocument(); XWPFParagraph p2 = xdoc.createParagraph(); setParagraphTextStyleInfo(p2, false, "测试", "微软雅黑", "000000", "22", false, false, false, false, 0, null, false, 0, null); BigInteger noteId = BigInteger.valueOf(1); addFootNote(xdoc, p2, noteId, "参见1.1.1", null, null, "微软雅黑", "FF0000", "18", true, false, false, false, 0, null, false, 0, STVerticalAlignRun.SUPERSCRIPT); setParagraphTextStyleInfo(p2, true, "插", "微软雅黑", "000000", "22", false, false, false, false, 0, null, false, 0, null); noteId = noteId.add(BigInteger.ONE); addFootNote(xdoc, p2, noteId, "参见1.1.2", "[", "]", "微软雅黑", "000000", "18", false, false, false, false, 0, null, false, 0, STVerticalAlignRun.SUPERSCRIPT); setParagraphTextStyleInfo(p2, true, "入脚注", "微软雅黑", "000000", "22", false, false, false, false, 0, null, false, 0, null); noteId = noteId.add(BigInteger.ONE); addFootNote(xdoc, p2, noteId, "参见1.1.3", "[", "]", "微软雅黑", "000000", "18", false, false, false, false, 0, null, false, 0, STVerticalAlignRun.SUPERSCRIPT); saveDocument(xdoc, savePath); } public void addFootNote(XWPFDocument xdoc, XWPFParagraph p, BigInteger noteId, String noteContent, String notePrefix, String noteSuffix, String fontFamily, String colorVal, String fontSize, boolean isBlod, boolean isItalic, boolean isStrike, boolean isUnderLine, int underLineStyle, String underLineColor, boolean isHightLight, int hightLightValue, STVerticalAlignRun.Enum stRunEnum) { XWPFRun r1 = null; if (notePrefix != null) { r1 = p.createRun(); setRunTextStyleInfo(r1, notePrefix, fontFamily, colorVal, fontSize, isBlod, isItalic, isStrike, isUnderLine, underLineStyle, underLineColor, isHightLight, hightLightValue, stRunEnum); } r1 = p.createRun(); setRunTextStyleInfo(r1, null, fontFamily, colorVal, fontSize, isBlod, isItalic, isStrike, isUnderLine, underLineStyle, underLineColor, isHightLight, hightLightValue, stRunEnum); CTFtnEdnRef fr = r1.getCTR().addNewFootnoteReference(); fr.setId(noteId); if (noteSuffix != null) { r1 = p.createRun(); setRunTextStyleInfo(r1, noteSuffix, fontFamily, colorVal, fontSize, isBlod, isItalic, isStrike, isUnderLine, underLineStyle, underLineColor, isHightLight, hightLightValue, stRunEnum); } XWPFFootnotes footnotes = xdoc.createFootnotes(); CTFtnEdn ctNote = null; CTP ctp = null; XWPFParagraph p2 = null; ctNote = CTFtnEdn.Factory.newInstance(); ctNote.setId(noteId); ctNote.setType(STFtnEdn.NORMAL); ctp = ctNote.addNewP(); p2 = new XWPFParagraph(ctp, xdoc); if (notePrefix != null) { r1 = p2.createRun(); setRunTextStyleInfo(r1, notePrefix, fontFamily, colorVal, fontSize, isBlod, isItalic, isStrike, isUnderLine, underLineStyle, underLineColor, isHightLight, hightLightValue, stRunEnum); } r1 = p2.createRun(); setRunTextStyleInfo(r1, null, fontFamily, colorVal, fontSize, isBlod, isItalic, isStrike, isUnderLine, underLineStyle, underLineColor, isHightLight, hightLightValue, stRunEnum); r1.getCTR().addNewFootnoteRef(); if (noteSuffix != null) { r1 = p2.createRun(); setRunTextStyleInfo(r1, noteSuffix, fontFamily, colorVal, fontSize, isBlod, isItalic, isStrike, isUnderLine, underLineStyle, underLineColor, isHightLight, hightLightValue, stRunEnum); } r1 = p2.createRun(); CTText ctText = r1.getCTR().addNewT(); ctText.setStringValue(" "); ctText.setSpace(SpaceAttribute.Space.PRESERVE); r1 = p2.createRun(); setRunTextStyleInfo(r1, noteContent, fontFamily, colorVal, fontSize, isBlod, isItalic, isStrike, isUnderLine, underLineStyle, underLineColor, isHightLight, hightLightValue, stRunEnum); footnotes.addFootnote(ctNote); } public void setRunTextStyleInfo(XWPFRun pRun, String content, String fontFamily, String colorVal, String fontSize, boolean isBlod, boolean isItalic, boolean isStrike, boolean isUnderLine, int underLineStyle, String underLineColor, boolean isHightLight, int hightLightValue, STVerticalAlignRun.Enum stRunEnum) { if (content != null) { pRun.setText(content); } CTRPr pRpr = null; if (pRun.getCTR() != null) { pRpr = pRun.getCTR().getRPr(); if (pRpr == null) { pRpr = pRun.getCTR().addNewRPr(); } } if (fontFamily != null) { // 设置字体 CTFonts fonts = pRpr.isSetRFonts() ? pRpr.getRFonts() : pRpr .addNewRFonts(); fonts.setAscii(fontFamily); fonts.setEastAsia(fontFamily); fonts.setHAnsi(fontFamily); } if (fontSize != null) { // 设置字体大小 CTHpsMeasure sz = pRpr.isSetSz() ? pRpr.getSz() : pRpr.addNewSz(); sz.setVal(new BigInteger(fontSize)); CTHpsMeasure szCs = pRpr.isSetSzCs() ? pRpr.getSzCs() : pRpr .addNewSzCs(); szCs.setVal(new BigInteger(fontSize)); } if (colorVal != null) { pRun.setColor(colorVal); } // 设置字体样式 if (isBlod) { pRun.setBold(isBlod); } if (isItalic) { pRun.setItalic(isItalic); } if (isStrike) { pRun.setStrike(isStrike); } if (colorVal != null) { pRun.setColor(colorVal); } // 设置下划线样式 if (isUnderLine) { CTUnderline u = pRpr.isSetU() ? pRpr.getU() : pRpr.addNewU(); u.setVal(STUnderline.Enum.forInt(Math.abs(underLineStyle % 19))); if (underLineColor != null) { u.setColor(underLineColor); } } // 设置字突出显示文本 if (isHightLight) { if (hightLightValue > 0 && hightLightValue < 17) { CTHighlight hightLight = pRpr.isSetHighlight() ? pRpr .getHighlight() : pRpr.addNewHighlight(); hightLight .setVal(STHighlightColor.Enum.forInt(hightLightValue)); } } if (stRunEnum != null) { CTVerticalAlignRun ctV = CTVerticalAlignRun.Factory.newInstance(); ctV.setVal(stRunEnum); pRpr.setVertAlign(ctV); } } public void setParagraphTextStyleInfo(XWPFParagraph p, boolean isNew, String content, String fontFamily, String colorVal, String fontSize, boolean isBlod, boolean isItalic, boolean isStrike, boolean isUnderLine, int underLineStyle, String underLineColor, boolean isHightLight, int hightLightValue, STVerticalAlignRun.Enum stRunEnum) { XWPFRun pRun = null; if (isNew) { pRun = p.createRun(); } else { if (p.getRuns() != null && p.getRuns().size() > 0) { pRun = p.getRuns().get(0); } else { pRun = p.createRun(); } } if (content != null) { pRun.setText(content); } CTRPr pRpr = null; if (pRun.getCTR() != null) { pRpr = pRun.getCTR().getRPr(); if (pRpr == null) { pRpr = pRun.getCTR().addNewRPr(); } } // 设置字体 CTFonts fonts = pRpr.isSetRFonts() ? pRpr.getRFonts() : pRpr .addNewRFonts(); fonts.setAscii(fontFamily); fonts.setEastAsia(fontFamily); fonts.setHAnsi(fontFamily); // 设置字体大小 CTHpsMeasure sz = pRpr.isSetSz() ? pRpr.getSz() : pRpr.addNewSz(); sz.setVal(new BigInteger(fontSize)); CTHpsMeasure szCs = pRpr.isSetSzCs() ? pRpr.getSzCs() : pRpr .addNewSzCs(); szCs.setVal(new BigInteger(fontSize)); if (colorVal != null) { pRun.setColor(colorVal); } // 设置字体样式 if (isBlod) { pRun.setBold(isBlod); } if (isItalic) { pRun.setItalic(isItalic); } if (isStrike) { pRun.setStrike(isStrike); } if (colorVal != null) { pRun.setColor(colorVal); } // 设置下划线样式 if (isUnderLine) { CTUnderline u = pRpr.isSetU() ? pRpr.getU() : pRpr.addNewU(); u.setVal(STUnderline.Enum.forInt(Math.abs(underLineStyle % 19))); if (underLineColor != null) { u.setColor(underLineColor); } } // 设置突出显示文本 if (isHightLight) { if (hightLightValue > 0 && hightLightValue < 17) { CTHighlight hightLight = pRpr.isSetHighlight() ? pRpr .getHighlight() : pRpr.addNewHighlight(); hightLight .setVal(STHighlightColor.Enum.forInt(hightLightValue)); } } if (stRunEnum != null) { CTVerticalAlignRun ctV = CTVerticalAlignRun.Factory.newInstance(); ctV.setVal(stRunEnum); pRpr.setVertAlign(ctV); } } public void saveDocument(XWPFDocument document, String savePath) throws Exception { FileOutputStream fos = new FileOutputStream(savePath); document.write(fos); fos.close(); } }
结果为:
转载请注明原处,原文链接:http://53873039oycg.iteye.com/blog/2158635 ,谢谢。
--------------------------------------------顺便说一句--------------------------------------------
使用上述方法创建批注(comment)或者尾注(endnote)是不可行的,到目前为止,尚未发现有效的方法使用poi新增批注或者尾注,如果有这方便的需求,请使用docx4j,至少docx4j在git上面的samples里包含了怎么创建comment,其他的如怎么使用poi创建书签(bookmark)这种简单的功能就不写了,修改书签网上也有现成的代码。
全文完。
相关推荐
简单poi创建word文档源码\简单poi创建word文档源码\简单poi创建word文档源码
POI 替换word2007文本框的值POI,包含word表中的文本框的值,定义好word模板即可替换word中的值。
POI解析word2007文本及图片(已测试).doc
这是一个POI读取word 2003 和 word 2007的例子 是一个Eclipse工程 下载后直接导入工程 运行 src 目录下的 Test java 类即可 这个 rar 包中包含着 POI 读取word 2003 和 word 2007 所需要的 jar 包 也有需要读取的 ...
NULL 博文链接:https://53873039oycg.iteye.com/blog/2187388
poi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成wordpoi生成word
POI报表Word导出
POI导出Word包含页眉 页脚的设置,标题的设置以及显示显示设置,包括POI操作Word完整的jar包
下载后,运行 可以 Test1中的main方法,注意里面的word模版地址要修改下。 test1.java主要是针对word的书签操作 MSWordTool.java主要是针对word的列表书签的操作,相对来说比较复杂,具体看个人需求。
使用poi替换word中的图片,无需加书签,doc/docx均可,亲测。
使用java Apache poi 根据word模板生成word报表 仅支持docx格式的word文件,大概是word2010及以后版本,doc格式不支持。 使用说明:https://blog.csdn.net/u012775558/article/details/79678701
附件包含一个web项目,是一个简单的解析excel和word的例子,项目中包含jar poi解析excel、word2007,2010等版本
Java POI 生成Word文档,支持图片插入,关键是修改XML部分,本人已测试通过。
POI对Word的操作,特别是针对生成报告模板时,向word中写入文本和图片。
一个使用Apache的poi使用word模板循环输出表格行并导出的例子
使用poi将Word2003和Word2007内容(包括图片)完整转换成html。内附核心代码,成功事例,及完整Jar包。不用再东查西找拼凑Jar包了!
POI无法读取到word每一段所在的页码。我给出了代码和所有的解释,用POI读取word中的页码。也算是小方法了,基本很准确,为什么要加一个基本呢?因为,分页标志符号在第一行为空白行的时候没有分页符!!所以你的文档...
Apache poi 根据word模板生成word报表 替换 循环列表 图片,代码调试过了,修改相应的图片位置,word模板路径即可运行
poi将word里面的内容转换成html、包括word里面字体的大小、表格的处理、还有图片、段落等等...
poi完美word转html,完整操作资料的引用说明