首页 > 编程知识 正文

java poi html转word_java POI HTML转Word 两种方式

时间:2023-05-04 13:30:32 阅读:30477 作者:4611

说明:无论使用哪种方法,都不能参照CSS渲染样式。 而是使用style或在当前页面上放置样式

懦弱的八宝粥、

1、引用的jar包

org.apache.poi

波伊

4.1.0

2、核心代码

String html='

测试内容poifs文件系统poifs=null;

文件输出流流流=null;

字节输入基本值=空值;

String uuid='测试. doc ';

文件文件=null;

try {

//HTML的内容是

软件包fileParam.setcContent ('

' html ' '; byte[] b=fileParam.getcContent ().getBytes );

bais=newbytearrayinputstream(b;

poifs=new poifs文件系统(;

directoryentrydirectory=poifs.get root (;

//WordDocument名称不能更改

目录. create document (' word document ',bais );

ostream=new文件输出流(uuid;

POIFS.writefilesystem(Ostream ); //在当前目录下生成测试. doc文档

}catch(exceptione ) {

logger.error(exceptionis ),e );

} finally {

Ioutils.closequietly(poifs );

Ioutils.closequietly(Ostream );

Ioutils.closequietly(Bais );

try {

fileutils.forcedelete(file;

}catch(exceptionE2) {

}

}

方法2

//*

* word格式的html标记标头

*/

publicstaticfinalstringhtml _ tag _ BGN='

'; publicfilepathdownloadwordreport (字符串tmlforprint )。

try {

stringwordstring=htmlfor print.replace all ('

','').replaceAll (',HTML_TAG_BGN ); String fileName=new String ('测试文件. doc'.getBytes )、' UTF-8 ' );

//文件上传方法

return this.upload (newbytearrayinputstream (word string.getbytes )、fileName );

}catch(exceptione ) {

返回空值;

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。