Java ApachePOI单词教程。
有人知道使用ApachePOI库使用MS Word的好教程吗? 我想了解如何创建word documents.doc(或者可能需要创建template.dot)来在文档(模板)中创建变量,如Java ApachePOI单词教程。,java,apache,apache-poi,Java,Apache,Apache Poi,有人知道使用ApachePOI库使用MS Word的好教程吗? 我想了解如何创建word documents.doc(或者可能需要创建template.dot)来在文档(模板)中创建变量,如${customer.name},然后通过Range.replaceText(${customer.name}”,“Microsoft CO”)替换它或者可能${customer.name}不是变量,只是纯文本(为了更清楚起见,它的名称类似于变量名)?我也没有找到关于在POI中使用表的教程 事实上,我有一个.
${customer.name}
,然后通过Range.replaceText(${customer.name}”,“Microsoft CO”)替换它
或者可能${customer.name}
不是变量,只是纯文本(为了更清楚起见,它的名称类似于变量名)?我也没有找到关于在POI中使用表的教程
事实上,我有一个.doc文档,我需要在其中替换一些变量,如名称、姓氏,还需要用一些值填充表。您有机会尝试吗
我建议您在IDE中将源代码作为单独的项目签出,并参考相关的测试用例。您可以使用Microsoft Word在Word文档中使用书签。 我一周前就做了。我在文档中插入书签,方法是选择要替换的文本,然后插入->书签并键入新的书签名称。 之后,在代码中,您应该在util类中实现如下内容:
/**
* Inserts a value at a location within the Word document specified by a
* named bookmark.
*
* @param bookmarkName An instance of the String class that encapsulates
* the name of the bookmark. Note that case is important and the case
* of the bookmarks name within the document and that of the value
* passed to this parameter must match.
* @param bookmarkValue An instance of the String class that encapsulates
* the value that should be inserted into the document at the location
* specified by the bookmark.
*/
public final void insertAtBookmark(String bookmarkName, String bookmarkValue, Style style) {
List<XWPFTable> tableList = null;
Iterator<XWPFTable> tableIter = null;
List<XWPFTableRow> rowList = null;
Iterator<XWPFTableRow> rowIter = null;
List<XWPFTableCell> cellList = null;
Iterator<XWPFTableCell> cellIter = null;
XWPFTable table = null;
XWPFTableRow row = null;
XWPFTableCell cell = null;
// Firstly, deal with any paragraphs in the body of the document.
this.procParaList(this.document.getParagraphs(), bookmarkName, bookmarkValue, style);
// Then check to see if there are any bookmarks in table cells. To do this
// it is necessary to get at the list of paragraphs 'stored' within the
// individual table cell, hence this code which get the tables from the
// document, the rows from each table, the cells from each row and the
// paragraphs from each cell.
tableList = this.document.getTables();
tableIter = tableList.iterator();
while(tableIter.hasNext()) {
table = tableIter.next();
rowList = table.getRows();
rowIter = rowList.iterator();
while(rowIter.hasNext()) {
row = rowIter.next();
cellList = row.getTableCells();
cellIter = cellList.iterator();
while(cellIter.hasNext()) {
cell = cellIter.next();
this.procParaList(cell.getParagraphs(),
bookmarkName,
bookmarkValue, style);
}
}
}
}
/**
* Inserts text into the document at the position indicated by a specific
* bookmark. Note that the current implementation does not take account
* of nested bookmarks, that is bookmarks that contain other bookmarks. Note
* also that any text contained within the bookmark itself will be removed.
*
* @param paraList An instance of a class that implements the List interface
* and which encapsulates references to one or more instances of the
* XWPFParagraph class.
* @param bookmarkName An instance of the String class that encapsulates the
* name of the bookmark that identifies the position within the
* document some text should be inserted.
* @param bookmarkValue An instance of the AString class that encapsulates
* the text that should be inserted at the location specified by the
* bookmark.
*/
private final void procParaList(List<XWPFParagraph> paraList,
String bookmarkName, String bookmarkValue,Style style) {
Iterator<XWPFParagraph> paraIter = null;
XWPFParagraph para = null;
List<CTBookmark> bookmarkList = null;
Iterator<CTBookmark> bookmarkIter = null;
CTBookmark bookmark = null;
XWPFRun run = null;
Node nextNode = null;
// Get an Iterator to step through the contents of the paragraph list.
paraIter = paraList.iterator();
while(paraIter.hasNext()) {
// Get the paragraph, a llist of CTBookmark objects and an Iterator
// to step through the list of CTBookmarks.
para = paraIter.next();
bookmarkList = para.getCTP().getBookmarkStartList();
bookmarkIter = bookmarkList.iterator();
while(bookmarkIter.hasNext()) {
// Get a Bookmark and check it's name. If the name of the
// bookmark matches the name the user has specified...
bookmark = bookmarkIter.next();
if(bookmark.getName().equals(bookmarkName)) {
// ...create the text run to insert and set it's text
// content and then insert that text into the document.
run = para.createRun();
run.setText(bookmarkValue);
//run.set
/*CTR ctr = run.getCTR();
CTRPr ctrPr = ctr.getRPr();
if(ctrPr == null) {
ctrPr = ctr.addNewRPr();
} */
if(defaultStyle != null || style != null){
//ctrPr.addNewRFonts().setAscii("Calibri");
if(style != null){
applyStyleToRun(style, run);
if(style.isCenter()) para.setAlignment(ParagraphAlignment.CENTER);
}else{
if(defaultStyle.isCenter()) para.setAlignment(ParagraphAlignment.CENTER);
applyStyleToRun(defaultStyle, run);
}
}
// The new Run should be inserted between the bookmarkStart
// and bookmarkEnd nodes, so find the bookmarkEnd node.
// Note that we are looking for the next sibling of the
// bookmarkStart node as it does not contain any child nodes
// as far as I am aware.
nextNode = bookmark.getDomNode().getNextSibling();
// If the next node is not the bookmarkEnd node, then step
// along the sibling nodes, until the bookmarkEnd node
// is found. As the code is here, it will remove anything
// it finds between the start and end nodes. This, of course
// comepltely sidesteps the issues surrounding boorkamrks
// that contain other bookmarks which I understand can happen.
while(nextNode != null && nextNode.getNodeName() != null && !(nextNode.getNodeName().contains("bookmarkEnd"))) {
para.getCTP().getDomNode().removeChild(nextNode);
nextNode = bookmark.getDomNode().getNextSibling();
}
// Finally, insert the new Run node into the document
// between the bookmarkStrat and the bookmarkEnd nodes.
para.getCTP().getDomNode().insertBefore(
run.getCTR().getDomNode(),
nextNode);
}
}
}
}
/**
* Applique un style sur un XWPFRun
* @param style
* @param run
*/
private void applyStyleToRun(Style style, XWPFRun run) {
run.setFontSize(style.getFontSize());
if(style.getFontFamily() != null){
run.setFontFamily(style.getFontFamily());
}
run.setBold(style.isBold());
run.setItalic(style.isItalic());
if(style.getColorCode() != null){
run.getCTR().addNewRPr().addNewColor().setVal(style.getColorCode());
}
}
/**
*在Word文档中由指定的位置插入值
*命名书签。
*
*@param bookmark name封装的字符串类的实例
*书签的名称。请注意,案例很重要,案例
*文档中书签名称和值的名称
*传递给此参数的值必须匹配。
*@param bookmarkValue封装的字符串类的实例
*应在该位置插入到文档中的值
*由书签指定。
*/
public final void insertBookmark(字符串bookmark名称、字符串bookmark值、样式样式){
List tableList=null;
迭代器tableIter=null;
List rowList=null;
迭代器rowIter=null;
List CELLIST=null;
迭代器cellIter=null;
XWPFTable table=null;
XWPFTableRow行=空;
XWPFTableCell单元格=空;
//首先,处理文件正文中的任何段落。
this.procParaList(this.document.getparations(),bookmarkName,bookmarkValue,style);
//然后检查表格单元格中是否有书签。若要执行此操作
//有必要查看“存储”在文档中的段落列表
//单个表单元格,因此此代码从
//文档、每个表中的行、每行中的单元格以及
//每个单元格中的段落。
tableList=this.document.getTables();
tableIter=tableList.iterator();
while(tableter.hasNext()){
table=tableter.next();
rowList=table.getRows();
rowIter=rowList.iterator();
while(rowIter.hasNext()){
row=rowIter.next();
cellList=行。getTableCells();
cellIter=cellList.iterator();
while(cellIter.hasNext()){
cell=cellIter.next();
this.procParaList(cell.getparations(),
书签名称,
(价值、风格);
}
}
}
}
/**
*在文档中指定的位置插入文本
*书签。请注意,当前的实现没有考虑
*嵌套书签,即包含其他书签的书签。注
*此外,书签中包含的任何文本都将被删除。
*
*@param paraList实现列表接口的类的实例
*它封装了对一个或多个
*XWPFParagraph类。
*@param bookmark name封装
*用于标识中位置的书签的名称
*文档中应插入一些文本。
*@param bookmarkValue封装的AString类的实例
*应插入到指定位置的文本
*书签。
*/
私人最终作废副清单(清单副清单,
字符串书签名称、字符串书签值、样式){
迭代器并行程序=null;
XWPFParagraph paragraph=null;
List bookmarkList=null;
迭代器书签iter=null;
CTBookmark bookmark=null;
XWPFRun=null;
节点nextNode=null;
//获取一个迭代器以逐步遍历段落列表的内容。
paraIter=paraList.iterator();
while(paraIter.hasNext()){
//获取段落、CTBookmark对象的列表和迭代器
//单步浏览CTBookmarks列表。
para=paraIter.next();
bookmarkList=para.getCTP().getbookmarklist();
bookmarkIter=bookmarkList.iterator();
while(bookmarkIter.hasNext()){
//获取书签并检查其名称。如果
//书签与用户指定的名称匹配。。。
bookmark=bookmarkIter.next();
如果(bookmark.getName().equals(bookmarkName)){
//…创建文本运行以插入并设置其文本
//内容,然后将该文本插入文档。
run=para.createRun();
run.setText(bookmarkValue);
//run.set
/*CTR CTR=run.getCTR();
CTRPr CTRPr=ctr.getRPr();
如果(ctrPr==null){
ctrPr=ctr.addNewRPr();
} */
if(defaultStyle!=null | | style!=null){
//ctrPr.addNewRFonts().setAscii(“Calibri”);
如果(样式!=null){
applyStyleToRun(样式、运行);
if(style.isCenter())para.setAlignment(ParagraphAlignment.CENTER);
}埃尔斯