本文目录一览:

POI操作word,怎么设置换行

软回车,即自动换行符,可以通过查找/替换功能删除。 操作步骤: 1、单击开始----查找,如图所示; 2、打开查找和替换对话框,在查找内容处输入:^l;在替换为处空着,什么也不用输入,如图所示,单击全部替换即可,如图所示。

如何样能让poi读取的word按原来的格式显示在页面

怎么样能让poi读取的word按原来的格式显示在页面

因为poi读取word 没法读取到空格和回车.这个问题要如何解决呢

poi java

------解决方案--------------------

public static void main(String[] args) {

File file = new File("D:/test.doc");

try {

FileInputStream fis = new FileInputStream(file);

HWPFDocument hwpfd = new HWPFDocument(fis);

WordExtractor wordExtractor = new WordExtractor(hwpfd);

String[] paragraph = wordExtractor.getParagraphText();

for (int i = 0; i paragraph.length; i++) {

System.out.println(paragraph[i]);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

怎样用poi在word中生成表格

关键代码如下:

FileInputStream fileInputStream = new FileInputStream( soureFile);

POIFSFileSystem pfs = new POIFSFileSystem( fileInputStream );

HWPFDocument hwpf = new HWPFDocument(pfs);// make a HWPFDocument object

OutputStream output = new FileOutputStream( targetFile );

hwpf.write(output);// write to the target file

output.close();

(2)再word中插入表格。HWPF的情况:

Table tcDataTable = range.insertTableBefore( (short)column , row);//column and row列数和行数

tcDataTable.getRow(i).getCell(j).getParagraph(0).getCharacterRun(0).insertBefore("插入i行j列的内容" );

XWPF的情况:

String outputFile = "D://test.doc";

XWPFDocument document = new XWPFDocument();

XWPFTable tableOne = document.createTable();

XWPFTableRow tableOneRowOne = tableOne.getRow(0);

tableOneRowOne.getCell(0).setText("11");

XWPFTableCell cell12 = tableOneRowOne.createCell();

cell12.setText("12");

// tableOneRowOne.addNewTableCell().setText("第1行第2列");

// tableOneRowOne.addNewTableCell().setText("第1行第3列");

// tableOneRowOne.addNewTableCell().setText("第1行第4列");

XWPFTableRow tableOneRowTwo = tableOne.createRow();

tableOneRowTwo.getCell(0).setText("21");

tableOneRowTwo.getCell(1).setText("22");

// tableOneRowTwo.getCell(2).setText("第2行第3列");

XWPFTableRow tableOneRow3 = tableOne.createRow();

tableOneRow3.addNewTableCell().setText("31");

tableOneRow3.addNewTableCell().setText("32");

FileOutputStream fOut;

try {

fOut = new FileOutputStream(outputFile);

document.write(fOut);

fOut.flush();

// 操作结束,关闭文件

fOut.close();

} catch (Exception e) {

e.printStackTrace();

}

使用poi操作word时如何在有多个表格的word中定位到其中一个表格

关键代码如下:

FileInputStream fileInputStream = new FileInputStream( soureFile);

POIFSFileSystem pfs = new POIFSFileSystem( fileInputStream );

HWPFDocument hwpf = new HWPFDocument(pfs);// make a HWPFDocument object

OutputStream output = new FileOutputStream( targetFile );

hwpf.write(output);// write to the target file

output.close();

(2)再word中插入表格。HWPF的情况:

Table tcDataTable = range.insertTableBefore( (short)column , row);//column and row列数和行数

tcDataTable.getRow(i).getCell(j).getParagraph(0).getCharacterRun(0).insertBefore("插入i行j列的内容" );

XWPF的情况:

String outputFile = "D://test.doc";

XWPFDocument document = new XWPFDocument();

XWPFTable tableOne = document.createTable();

XWPFTableRow tableOneRowOne = tableOne.getRow(0);

tableOneRowOne.getCell(0).setText("11");

XWPFTableCell cell12 = tableOneRowOne.createCell();

cell12.setText("12");

// tableOneRowOne.addNewTableCell().setText("第1行第2列");

// tableOneRowOne.addNewTableCell().setText("第1行第3列");

// tableOneRowOne.addNewTableCell().setText("第1行第4列");

XWPFTableRow tableOneRowTwo = tableOne.createRow();

tableOneRowTwo.getCell(0).setText("21");

tableOneRowTwo.getCell(1).setText("22");

// tableOneRowTwo.getCell(2).setText("第2行第3列");

XWPFTableRow tableOneRow3 = tableOne.createRow();

tableOneRow3.addNewTableCell().setText("31");

tableOneRow3.addNewTableCell().setText("32");

FileOutputStream fOut;

try {

fOut = new FileOutputStream(outputFile);

document.write(fOut);

fOut.flush();

// 操作结束,关闭文件

fOut.close();

} catch (Exception e) {

e.printStackTrace();

}

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

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

FileInputStream fis = new FileInputStream("e:/file.docx");

XWPFDocument doc = new XWPFDocument(fis);

ListXWPFParagraph paragraphList = doc.getParagraphs();

XWPFParagraph paragraph = paragraphList.get(10);

文本框在Word中显示如图所示:

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

XmlObject object = paragraph.getCTP().getRArray(1);

XmlCursor cursor = object.newCursor();

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

//修改第一处文本:

cursor.toChild(1); cursor.toChild(0); cursor.toChild(3); cursor.toChild(0); cursor.toChild(0); cursor.toChild(3); cursor.toChild(1); cursor.setTextValue("First");

// 修改第二处文本

cursor.toParent(); cursor.toParent(); cursor.toChild(1);

cursor.toChild(3); cursor.toChild(1);

cursor.setTextValue("Second");

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

FileOutputStream fos = new FileOutputStream("e:/export.docx");

doc.write(fos);

fos.flush();

fos.close();

fis.close();

修改后的文本框如图所示: