如何使用apache poi在文档.docx中的表格单元格中创建TextBox

时间:2019-03-31 15:03:53

标签: java apache textbox apache-poi

我用过Javafx和

我使用apache poi创建了一个表:

 XWPFDocument document = new XWPFDocument();
 XWPFParagraph paragraph = document.createParagraph();
 XWPFTable table = document.createTable(4, 3);

并创建了类似于以下段落的段落:

XWPFParagraph p1 = table.getRow(0).getCell(2).getParagraphs().get(0);
XWPFRun r1 = p1.createRun();
r1.setText(category_number.getText() + category.toString());

现在,我想在一行的一个单元格中创建一个TextBox,但不知道如何将Cell和Row寻址到textBox以及设置文本和对齐文本框。

请帮助我):

1 个答案:

答案 0 :(得分:1)

Cursor cursor = database.rawQuery("select * from " + ChildMonitoringAppDBHelper.CHILD_TABLE_NAME, null); if (cursor.moveToFirst()) { do { Child child = new Child(cursor.getString(cursor.getColumnIndex("CHILD_NAME")), cursor.getString(cursor.getColumnIndex("CHILD_AGE")), cursor.getString(cursor.getColumnIndex("CHILD_DOB")), cursor.getString(cursor.getColumnIndex("CHILD_HEIGHT")), cursor.getString(cursor.getColumnIndex("CHILD_WEIGHT"))); List.add(child); } while (cursor.moveToNext()); } cursor.close(); return List; 中的文本框是文档内容中的形状。 <p:treeTable value="..." id="orgaTable" var="b" widgetVar="bTable" scrollHeight="#{viewSettings.winheight-282}" emptyMessage="Keine Orgaeinheit gefunden" rowKey="#{b.id}" scrollable="true"> <p:column> <p:selectBooleanCheckbox ... /> </p:column> <p:column headerText="Bezeichnung" style="border: 1px solid #cacaca" width="50%"> <h:outputText value="#{b.name}" /> </p:column> ... </p:treeTable> 中尚未实现创建形状。但这可以使用基础的*.docx类来完成。

示例:

XWPF

此代码已使用ooxml-schemas测试过,并且在类路径中需要import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.*; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTxbxContent; import com.microsoft.schemas.vml.CTGroup; import com.microsoft.schemas.vml.CTShape; import org.w3c.dom.Node; public class CreateWordTextBoxInTable { public static void main(String[] args) throws Exception { XWPFDocument document= new XWPFDocument(); XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = paragraph.createRun(); run.setText("The table:"); XWPFTable table = document.createTable(4, 3); // table header row for (int c = 0; c < 3; c++ ) { paragraph = table.getRow(0).getCell(c).getParagraphArray(0); if (paragraph == null) paragraph = table.getRow(0).getCell(c).addParagraph(); run = paragraph.createRun(); run.setText("Column " + (c+1)); } // get run in cell for text box XWPFTableCell cell = table.getRow(1).getCell(1); paragraph = cell.getParagraphArray(0); if (paragraph == null) paragraph = cell.addParagraph(); run = paragraph.createRun(); // create inline text box in run // first crfeate group shape CTGroup ctGroup = CTGroup.Factory.newInstance(); // now add shape to group shape CTShape ctShape = ctGroup.addNewShape(); ctShape.setStyle("width:100pt;height:36pt"); // add text box content to shape CTTxbxContent ctTxbxContent = ctShape.addNewTextbox().addNewTxbxContent(); XWPFParagraph textboxparagraph = new XWPFParagraph(ctTxbxContent.addNewP(), (IBody)cell); textboxparagraph.setAlignment(ParagraphAlignment.CENTER); XWPFRun textboxrun = textboxparagraph.createRun(); textboxrun.setText("The TextBox content..."); textboxrun.setFontSize(10); // add group shape as picture to the run Node ctGroupNode = ctGroup.getDomNode(); CTPicture ctPicture = CTPicture.Factory.parse(ctGroupNode); CTR cTR = run.getCTR(); cTR.addNewPict(); cTR.setPictArray(0, ctPicture); FileOutputStream out = new FileOutputStream("test.docx"); document.write(out); out.close(); } }