cn.hutool.poi.word.DocUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hutool-all Show documentation
Show all versions of hutool-all Show documentation
Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。
package cn.hutool.poi.word;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.poi.exceptions.POIException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import java.io.File;
import java.io.IOException;
/**
* Word Document工具
*
* @author looly
* @since 4.4.1
*/
public class DocUtil {
/**
* 创建{@link XWPFDocument},如果文件已存在则读取之,否则创建新的
*
* @param file docx文件
* @return {@link XWPFDocument}
*/
public static XWPFDocument create(File file) {
try {
return FileUtil.exist(file) ? new XWPFDocument(OPCPackage.open(file)) : new XWPFDocument();
} catch (InvalidFormatException e) {
throw new POIException(e);
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy