⽤POI在word07模板⽂件中创建表格,修改内容等操作 最近做项⽬时,需要利⽤POI技术,在word⽂档中写表格,但是⽹上⼏乎搜索不到相关资料,经过2天的研究,终于实现了功能。
⽤到的POI对象是:XWPFDocument
读取模板⽂件获取所有的table:
OPCPackage opcPackage = POIXMLDocument.openPackage(wordFilePath);
XWPFDocument doc = new XWPFDocument(opcPackage);
List<XWPFTable> list = Tables();
对表格增加新的⼀⾏,并设置数据:
XWPFTableRow crtRow = tb.createRow();
List<XWPFTableCell> cells = TableCells();
for (int j = 0; j < cells.size(); j++) {
<(j).removeParagraph(0);
<(j).(j));
// (j).tVerticalAlignment(XWPFVertAlign.BOTH);
LOGGER.debug("inrt into table value:" + (j));小电脑桌
}
安全分享小案例
如需删除⾏:
// tb.removeRow(row); API删除⾏有点问题,我就直接⽤源码的⼀部分(上⾯⼀句话)即可。
⽂字的替换原则 :先删后加
Iterator<XWPFParagraph> paragraphs = ParagraphsIterator();
while (paragraphs.hasNext()) {
XWPFParagraph paragraph = (XWPFParagraph) ();
Set<Entry<String, String>> t = Set();
for (Map.Entry<String, String> key : t) {
String text = "";
if (ParagraphText().Key()) != -1) {
PositionInParagraph positionInParagraph = new PositionInParagraph();
TextSegement textSegement = SearchText.CTP(), Key(), positionInParagraph);
text = Text(textSegement).Key(), Value());
List<XWPFRun> paragraphRuns = Runs();
for (int i = EndRun(); i > BeginRun(); i--) {
}
XWPFRun paragraphRun = (BeginRun());仪表故障灯
失物招领英语CTR ctr = CTR();
for (int i = ctr.sizeOfTArray() - 1; i >= 0; i--) {
}
paragraphRun.tText(text);
酒店adr
}
}张铁林舌吻
法律的名言警句}
我修改了API搜索⽂本⽅法:archText 为 SearchText.archTextOfParagraph(CTP paragraph, String arched, PositionInParagraph startPos)
格斗技