如何使用POI *** 作Word文本框中的内容

如何使用POI *** 作Word文本框中的内容,第1张

步骤

第一步,使用输入流打开文件,并获得文档的XWPFDocument对象。然后获得文档的所有段落,进而获得要 *** 作的文本框所在的段落,具体使用时候,可以通过判断或者print *** 作得知要 *** 作的文本框到底是哪一段。

FileInputStream fis = newFileInputStream("e:/filedocx");

XWPFDocument doc = new XWPFDocument(fis);

List<XWPFParagraph> paragraphList =docgetParagraphs();

XWPFParagraph paragraph = paragraphListget(10);

文本框在Word中显示

第二步,获取XWPFParagraph的XmlObject,然后获得XmlObject对象的游标。可以通过打印XmlObject来得知当前XML的内容,也可以使用XmlCursor的getName方法和getTextValue方法来查看当前游标所在位置的Node及Node的值。

XmlObject object =paragraphgetCTP()getRArray(1);

XmlCursor cursor = objectnewCursor();

第四步,通过移动游标,找到要修改的文本所在位置,然后使用游标的setTextValue来设置其值。

//修改第一处文本:

cursortoChild(1); cursortoChild(0);cursortoChild(3); cursortoChild(0); cursortoChild(0); cursortoChild(3);cursortoChild(1); cursorsetTextValue("First");

// 修改第二处文本

cursortoParent(); cursortoParent();cursortoChild(1);

cursortoChild(3); cursortoChild(1);

cursorsetTextValue("Second");

第四步,保存文件、关闭输入输出流。

FileOutputStream fos = newFileOutputStream("e:/exportdocx");

docwrite(fos);

fosflush();

fosclose();

fisclose();

修改后的文本框

你好,试试以下代码行不行。

package comsample;

import javaawtColor;

import javaioFileOutputStream;

import javaioIOException;

import comlowagietextCell;

import comlowagietextDocument;

import comlowagietextDocumentException;

import comlowagietextElement;

import comlowagietextFont;

import comlowagietextFontFactory;

import comlowagietextImage;

import comlowagietextPageSize;

import comlowagietextParagraph;

import comlowagietextPhrase;

import comlowagietextTable;

import comlowagietextpdfBaseFont;

import comlowagietextrtfRtfWriter2;

/

@author wangyanjun

@email bd_wyj@sinacom

@createDate Jun 12, 2008

/

public class CreateWordDemo {

public void createDocContext(String file) throws DocumentException,

IOException {

// 设置纸张大小

Document document = new Document(PageSizeA4);

// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中

RtfWriter2getInstance(document, new FileOutputStream(file));

documentopen();

// 设置中文字体

BaseFont bfChinese = BaseFontcreateFont("STSongStd-Light",

"UniGB-UCS2-H", BaseFontNOT_EMBEDDED);

// 标题字体风格

Font titleFont = new Font(bfChinese, 12, FontBOLD);

// 正文字体风格

Font contextFont = new Font(bfChinese, 10, FontNORMAL);

Paragraph title = new Paragraph("标题");

// 设置标题格式对齐方式

titlesetAlignment(ElementALIGN_CENTER);

titlesetFont(titleFont);

documentadd(title);

String contextString = "iText是一个能够快速产生PDF文件的java类库。"

+ " \n"// 换行

+ "iText的java类对于那些要产生包含文本,"

+ "表格,图形的只读文档是很有用的。它的类库尤其与java Servlet有很好的给合。"

+ "使用iText与PDF能够使你正确的控制Servlet的输出。";

Paragraph context = new Paragraph(contextString);

// 正文格式左对齐

contextsetAlignment(ElementALIGN_LEFT);

contextsetFont(contextFont);

// 离上一段落(标题)空的行数

contextsetSpacingBefore(5);

// 设置第一行空的列数

contextsetFirstLineIndent(20);

documentadd(context);

//利用类FontFactory结合Font和Color可以设置各种各样字体样式

/

FontUNDERLINE 下划线,FontBOLD 粗体

/

Paragraph underline = new Paragraph("下划线的实现", FontFactorygetFont(

FontFactoryHELVETICA_BOLDOBLIQUE, 18, FontUNDERLINE,

new Color(0, 0, 255)));

documentadd(underline);

// 设置 Table 表格

Table aTable = new Table(3);

int width[] = {25,25,50};

aTablesetWidths(width);//设置每列所占比例

aTablesetWidth(90); // 占页面宽度 90%

aTablesetAlignment(ElementALIGN_CENTER);//居中显示

aTablesetAlignment(ElementALIGN_MIDDLE);//纵向居中显示

aTablesetAutoFillEmptyCells(true); //自动填满

aTablesetBorderWidth(1); //边框宽度

aTablesetBorderColor(new Color(0, 125, 255)); //边框颜色

aTablesetPadding(2);//衬距,看效果就知道什么意思了

aTablesetSpacing(3);//即单元格之间的间距

aTablesetBorder(2);//边框

//设置表头

/

cellsetHeader(true);是将该单元格作为表头信息显示;

cellsetColspan(3);指定了该单元格占3列;

为表格添加表头信息时,要注意的是一旦表头信息添加完了之后,

必须调用 endHeaders()方法,否则当表格跨页后,表头信息不会再显示

/

Cell haderCell = new Cell("表格表头");

haderCellsetHeader(true);

haderCellsetColspan(3);

aTableaddCell(haderCell);

aTableendHeaders();

Font fontChinese = new Font(bfChinese, 12, FontNORMAL, ColorGREEN);

Cell cell = new Cell(new Phrase("这是一个测试的 33 Table 数据", fontChinese ));

cellsetVerticalAlignment(ElementALIGN_TOP);

cellsetBorderColor(new Color(255, 0, 0));

cellsetRowspan(2);

aTableaddCell(cell);

aTableaddCell(new Cell("#1"));

aTableaddCell(new Cell("#2"));

aTableaddCell(new Cell("#3"));

aTableaddCell(new Cell("#4"));

Cell cell3 = new Cell(new Phrase("一行三列数据", fontChinese ));

cell3setColspan(3);

cell3setVerticalAlignment(ElementALIGN_CENTER);

aTableaddCell(cell3);

documentadd(aTable);

documentadd(new Paragraph("\n"));

//添加

Image img=ImagegetInstance("d:\\img01800jpg");

imgsetAbsolutePosition(0, 0);

imgsetAlignment(ImageRIGHT);//设置显示位置

imgscaleAbsolute(12,35);//直接设定显示尺寸

imgscalePercent(50);//表示显示的大小为原尺寸的50%

imgscalePercent(25, 12);//图像高宽的显示比例

imgsetRotation(30);//图像旋转一定角度

documentadd(img);

documentclose();

}

/

@param args

/

public static void main(String[] args) {

CreateWordDemo word = new CreateWordDemo();

String file = "c:/demo1doc";

try {

wordcreateDocContext(file);

} catch (DocumentException e) {

eprintStackTrace();

} catch (IOException e) {

eprintStackTrace();

}

}

}

以上就是关于如何使用POI *** 作Word文本框中的内容全部的内容,包括:如何使用POI *** 作Word文本框中的内容、如何使用JAVA,POI读写word文档、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9731040.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存