Java学习笔记:Word文档的读写

Java学习笔记:Word文档的读写,第1张

Java学习笔记:Word文档的读写

不变样式就是run,段落是XWPFParagraph

static org.apache.poi.xwpf.usermodel.XWPFPictureaddPicture(org.apache.poi.xwpf.usermodel.XWPFdocument doc, byte[] pictureData)

向Word文档对象doc中增加一张图片,用图片的默认尺寸显示。

static org.apache.poi.xwpf.usermodel.XWPFPictureaddPicture(org.apache.poi.xwpf.usermodel.XWPFdocument doc, byte[] pictureData, int width, int height)

向Word文档对象doc中增加一张图片,图片的显示尺寸为width、height。

static org.apache.poi.xwpf.usermodel.XWPFPictureaddPicture(org.apache.poi.xwpf.usermodel.XWPFRun run, byte[] pictureData)

向XWPFRun对象doc中增加一张图片,用图片的默认尺寸显示。

static org.apache.poi.xwpf.usermodel.XWPFPictureaddPicture(org.apache.poi.xwpf.usermodel.XWPFRun run, byte[] pictureData, int width, int height)

向XWPFRun对象中增加一张图片,图片的显示尺寸为width、height。

static org.apache.poi.xwpf.usermodel.XWPFdocumentclone(org.apache.poi.xwpf.usermodel.XWPFdocument document)

创建document的一个副本。

static voidclose(org.apache.poi.ooxml.POIXMLdocument doc)

关闭Word对象。

static org.apache.poi.xwpf.usermodel.XWPFChartcreateChart(org.apache.poi.xwpf.usermodel.XWPFdocument doc, int width, int height)

在doc中添加一个尺寸为(width,height)的图表对象,XWPFChart类型。

static org.apache.poi.xwpf.usermodel.XWPFdocumentcreateDocxdocument()

创建XWPFdocument类型(*.docx格式)的Word内存对象。

static org.apache.poi.xwpf.usermodel.XWPFRuncreateRun(org.apache.poi.xwpf.usermodel.XWPFdocument doc, String text)

创建一个XWPFRun对象,文本内容为text,左对齐。

static org.apache.poi.xwpf.usermodel.XWPFRuncreateRun(org.apache.poi.xwpf.usermodel.XWPFdocument doc, String text, org.apache.poi.xwpf.usermodel.ParagraphAlignment alignment)

创建一个XWPFRun对象,文本内容为text,对齐方式为alignment。

static voidforEachRun(org.apache.poi.xwpf.usermodel.XWPFTableCell cell, java.util.function.Consumer action)

对于cell这个格的每个XWPFRun调用action这个lambda方法。

static org.apache.poi.xwpf.usermodel.XWPFdocumentopenDocx(byte[] bytes)

从byte[]中打开*.docx格式的Word文档的XWPFdocument对象。

static org.apache.poi.xwpf.usermodel.XWPFdocumentopenDocx(File file)

打开*.docx格式的Word文档的XWPFdocument对象。

static org.apache.poi.xwpf.usermodel.XWPFdocumentopenDocx(InputStream inputStream)

从inputStream中打开*.docx格式的Word文档的XWPFdocument对象。

static org.apache.poi.xwpf.usermodel.XWPFdocumentopenDocx(String file)

打开*.docx格式的Word文档的XWPFdocument对象。

static StringreadAllText(File file)

读取word文档file中的全部文本。

static StringreadAllText(InputStream inStream)

读取inStream中word文档的全部文本。

static StringreadAllText(String filename)

读取word文档filename中的全部文本。

static voidsaveToFile(org.apache.poi.xwpf.usermodel.XWPFdocument doc, File file)

把XWPFdocument对象代表的word文档保存到file文件中。

static voidsaveToFile(org.apache.poi.xwpf.usermodel.XWPFdocument doc, String filename)

把XWPFdocument对象代表的word文档保存到filename文件中。

static org.apache.poi.xwpf.usermodel.XWPFTableCellsetTableCellValue(org.apache.poi.xwpf.usermodel.XWPFTable table, int rowNum, int colNum, Object value)

设置table表格的第rowNum行的第colNum列的单元格的值为value。

static byte[]toByteArray(org.apache.poi.xwpf.usermodel.XWPFdocument doc)

获取Word对象的word文档的byte数组内容。

String s=WordHelpers.readAllText("D:\temp\1/参考碳路者--1.20会议内容.docx");//读Word文件
System.out.println(s);

 快捷键ctrl+F12,可以找到类中的所有成员

 

package Part4;

import com.yzk18.commons.IOHelpers;
import com.yzk18.docs.WordHelpers;
import org.apache.poi.xwpf.usermodel.XWPFdocument;
import org.apache.poi.xwpf.usermodel.XWPFPicture;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;

public class 读取Word文件 {
    public static void main(String[] args) {
        XWPFdocument doc = WordHelpers.openDocx("D:\temp/参考碳路者--1.20会议内容.docx");
        String s=WordHelpers.readAllText("D:\temp\1/参考碳路者--1.20会议内容.docx");//读Word文件
        System.out.println(s);
        for (XWPFPictureData pic : doc.getAllPictures())
        {
            IOHelpers.writeAllBytes("d:/temp/pics/"+pic.getFileName(),pic.getData());
        }
        WordHelpers.close(doc);
    }
}

 

 

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

原文地址: http://outofmemory.cn/zaji/5721644.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存