我需要使用apache poi
在word文档中插入如下截图的标题我使用下面的代码插入标题:
// write header content
XWPFDocument docx = new XWPFDocument();
CTSectPr sectPr = docx.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(docx,sectPr);
CTP ctpHeader = CTP.Factory.newInstance();
CTR ctrHeader = ctpHeader.addNewR();
CTText ctHeader = ctrHeader.addNewT();
String headerText = "This is header";
ctHeader.setStringValue(headerText);
XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, docx);
XWPFParagraph[] parsHeader = new XWPFParagraph[1];
parsHeader[0] = headerParagraph;
policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);
FileOutputStream out = new FileOutputStream("D:/giri.docx");
docx.write(out);
out.close();
System.out.println("Done");
答案 0 :(得分:0)
与Excel不同,Word没有左,中,右标题 - Word有标题。如果您想在一行上有三个单独的条目,您可以使用适当的对齐(例如居中/对齐)和制表位(可能是3个居中,或左,中,右各一个)格式化段落,然后插入制表符进入你要插入的文本。或者,特别是对于多行输入,您可以插入具有适当单元格格式的3列表格,并将输出发送到相关单元格。
答案 1 :(得分:0)
在VBA中,您可以使用以下代码向页眉添加一个表:
With ActiveDocument
.Tables.Add Range:=.Sections.First.Headers(wdHeaderFooterPrimary).Range, NumRows:=1, NumColumns:=3, AutoFitBehavior:=wdAutoFitFixed
End With
我会留给你做C#转换。
答案 2 :(得分:0)
XWPFHeader header = policy.getDefaultHeader(); CTP ctpHeader = CTP.Factory.newInstance();
CTR ctrHeader = ctpHeader.addNewR();
CTText ctheader = ctrHeader.addNewT();
String HeaderText = "TSS Word Documentsss";
//ctheader.set(HeaderText);
XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, document);
XWPFRun headerRun= headerParagraph.createRun();
headerRun.setBold(true);
headerRun.setFontSize(39);
headerRun.setColor("808000");
headerRun.setImprinted(true);
headerRun.setShadow(true);
headerRun.setCapitalized(true);
headerRun.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
headerRun.setText(HeaderText);
headerParagraph.setAlignment(ParagraphAlignment.CENTER);
headerParagraph.setBorderBottom(Borders.BASIC_BLACK_DASHES);
XWPFParagraph[] parsHeader = new XWPFParagraph[1];
parsHeader[0] = headerParagraph;
policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);