使用POI读写word docx文件 |
您所在的位置:网站首页 › poi读取doc文件失败 › 使用POI读写word docx文件 |
目录 1 读docx文件 1.1 通过XWPFWordExtractor读 1.2 通过XWPFDocument读 2 写docx文件 2.1 直接通过XWPFDocument生成 2.2 以docx文件作为模板
POI在读写word docx文件时是通过xwpf模块来进行的,其核心是XWPFDocument。一个XWPFDocument代表一个docx文档,其可以用来读docx文档,也可以用来写docx文档。XWPFDocument中主要包含下面这几种对象: l XWPFParagraph:代表一个段落。 l XWPFRun:代表具有相同属性的一段文本。 l XWPFTable:代表一个表格。 l XWPFTableRow:表格的一行。 l XWPFTableCell:表格对应的一个单元格。 1 读docx文件 跟读doc文件一样,POI在读docx文件的时候也有两种方式,通过XWPFWordExtractor和通过XWPFDocument。在XWPFWordExtractor读取信息时其内部还是通过XWPFDocument来获取的。 1.1 通过XWPFWordExtractor读在使用XWPFWordExtractor读取docx文档的内容时,我们只能获取到其文本,而不能获取到其文本对应的属性值。下面是一段使用XWPFWordExtractor来读取docx文档内容的示例代码: Java代码 public class XwpfTest { /** * 通过XWPFWordExtractor访问XWPFDocument的内容 * @throws Exception */ @Test public void testReadByExtractor() throws Exception { InputStream is = new FileInputStream("D:\\test.docx"); XWPFDocument doc = new XWPFDocument(is); XWPFWordExtractor extractor = new XWPFWordExtractor(doc); String text = extractor.getText(); System.out.println(text); CoreProperties coreProps = extractor.getCoreProperties(); this.printCoreProperties(coreProps); this.close(is); } /** * 输出CoreProperties信息 * @param coreProps */ private void printCoreProperties(CoreProperties coreProps) { System.out.println(coreProps.getCategory()); //分类 System.out.println(coreProps.getCreator()); //创建者 System.out.println(coreProps.getCreated()); //创建时间 System.out.println(coreProps.getTitle()); //标题 } /** * 关闭输入流 * @param is */ private void close(InputStream is) { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } }
1.2 通过XWPFDocument读 在通过XWPFDocument读取docx文档时,我们就可以获取到文本比较精确的属性信息了。比如我们可以获取到某一个XWPFParagraph、XWPFRun或者是某一个XWPFTable,包括它们对应的属性信息。下面是一个使用XWPFDocument读取docx文档的示例: Java代码 public class XwpfTest { /** * 通过XWPFDocument对内容进行访问。对于XWPF文档而言,用这种方式进行读操作更佳。 * @throws Exception */ @Test public void testReadByDoc() throws Exception { InputStream is = new FileInputStream("D:\\table.docx"); XWPFDocument doc = new XWPFDocument(is); List paras = doc.getParagraphs(); for (XWPFParagraph para : paras) { //当前段落的属性 // CTPPr pr = para.getCTP().getPPr(); System.out.println(para.getText()); } //获取文档中所有的表格 List tables = doc.getTables(); List rows; List cells; for (XWPFTable table : tables) { //表格属性 // CTTblPr pr = table.getCTTbl().getTblPr(); //获取表格对应的行 rows = table.getRows(); for (XWPFTableRow row : rows) { //获取行对应的单元格 cells = row.getTableCells(); for (XWPFTableCell cell : cells) { System.out.println(cell.getText());; } } } this.close(is); } /** * 关闭输入流 * @param is */ private void close(InputStream is) { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } }
2 写docx文件 2.1 直接通过XWPFDocument生成 在使用XWPFDocument写docx文件时不需要像使用HWPFDocument写doc文件那样必须从一个doc文件开始,我们可以直接new一个空的XWPFDocument,之后再往这个XWPFDocument里面填充内容,然后再把它写入到对应的输出流中。下面是使用XWPFDocument生成docx文件的示例代码: Java代码 public class XwpfTest { /** * 基本的写操作 * @throws Exception */ @Test public void testSimpleWrite() throws Exception { //新建一个文档 XWPFDocument doc = new XWPFDocument(); //创建一个段落 XWPFParagraph para = doc.createParagraph(); //一个XWPFRun代表具有相同属性的一个区域。 XWPFRun run = para.createRun(); run.setBold(true); //加粗 run.setText("加粗的内容"); run = para.createRun(); run.setColor("FF0000"); run.setText("红色的字。"); OutputStream os = new FileOutputStream("D:\\simpleWrite.docx"); //把doc输出到输出流 doc.write(os); this.close(os); } /*** * 写一个表格 * @throws Exception */ @Test public void testWriteTable() throws Exception { XWPFDocument doc = new XWPFDocument(); //创建一个5行5列的表格 XWPFTable table = doc.createTable(5, 5); //这里增加的列原本初始化创建的那5行在通过getTableCells()方法获取时获取不到,但通过row新增的就可以。 // table.addNewCol(); //给表格增加一列,变成6列 table.createRow(); //给表格新增一行,变成6行 List rows = table.getRows(); //表格属性 CTTblPr tablePr = table.getCTTbl().addNewTblPr(); //表格宽度 CTTblWidth width = tablePr.addNewTblW(); width.setW(BigInteger.valueOf(8000)); XWPFTableRow row; List cells; XWPFTableCell cell; int rowSize = rows.size(); int cellSize; for (int i=0; i |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |