前言

最近做的一个项目有个word导入的功能,一开始做的是导入纯文本,然后显示在前端页面,客户提出了建议,能不能改成和word文件里面格式一样的。

推荐

1、poi

2、open office

3、libreoffice

poi

pom.xml

org.apache.poi

poi-scratchpad

3.14

org.apache.poi

poi-ooxml

3.14

fr.opensagres.xdocreport

xdocreport

1.0.6

org.apache.poi

poi-ooxml-schemas

3.14

org.apache.poi

ooxml-schemas

1.3

word读取

/**

* 将word2003转换为html文件

*

* @param wordPath word文件路径

* @param wordName word文件名称无后缀

* @param suffix word文件后缀

* @param htmlPath html存储地址

* @throws IOException

* @throws TransformerException

* @throws ParserConfigurationException

*/

public static String Word2003ToHtml(String wordPath, String wordName, String suffix, String htmlPath)

throws IOException, TransformerException, ParserConfigurationException {

String htmlName = wordName + ".html";

final String imagePath = htmlPath + "image" + File.separator;

// 判断html文件是否存在

File htmlFile = new File(htmlPath + htmlName);

if (htmlFile.exists()) {

return htmlFile.getAbsolutePath();

}

// 原word文档

final String file = wordPath + File.separator + wordName + suffix;

InputStream input = new FileInputStream(new File(file));

HWPFDocument wordDocument = new HWPFDocument(input);

WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(

DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());

// 设置图片存放的位置

wordToHtmlConverter.setPicturesManager(new PicturesManager() {

public String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches,

float heightInches) {

File imgPath = new File(imagePath);

// 图片目录不存在则创建

if (!imgPath.exists()) {

imgPath.mkdirs();

}

File file = new File(imagePath + suggestedName);

try {

OutputStream os = new FileOutputStream(file);

os.write(content);

os.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

// 图片在html文件上的路径 相对路径

return "image/" + suggestedName;

}

});

// 解析word文档

wordToHtmlConverter.processDocument(wordDocument);

Document htmlDocument = wordToHtmlConverter.getDocument();

// 生成html文件上级文件夹

File folder = new File(htmlPath);

if (!folder.exists()) {

folder.mkdirs();

}

OutputStream outStream = new FileOutputStream(htmlFile);

DOMSource domSource = new DOMSource(htmlDocument);

StreamResult streamResult = new StreamResult(outStream);

TransformerFactory factory = TransformerFactory.newInstance();

Transformer serializer = factory.newTransformer();

serializer.setOutputProperty(OutputKeys.ENCODING, "GB2312");

serializer.setOutputProperty(OutputKeys.INDENT, "yes");

serializer.setOutputProperty(OutputKeys.METHOD, "html");

serializer.transform(domSource, streamResult);

return htmlFile.getAbsolutePath();

}

/**

*

* 2007版本word转换成html

*

* @param wordPath word文件路径

* @param wordName word文件名称无后缀

* @param suffix word文件后缀

* @param htmlPath html存储地址

* @return

* @throws IOException

*/

public static String word2007ToHtml(String wordPath, String wordName, String suffix, String htmlPath)

throws IOException {

String htmlName = wordName + ".html";

String imagePath = htmlPath + "image" + File.separator;

// 判断html文件是否存在

File htmlFile = new File(htmlPath + htmlName);

if (htmlFile.exists()) {

return htmlFile.getAbsolutePath();

}

// word文件

File wordFile = new File(wordPath + File.separator + wordName + suffix);

// 1) 加载word文档生成 XWPFDocument对象

InputStream in = new FileInputStream(wordFile);

XWPFDocument document = new XWPFDocument(in);

// 2) 解析 XHTML配置 (这里设置IURIResolver来设置图片存放的目录)

File imgFolder = new File(imagePath);

XHTMLOptions options = XHTMLOptions.create();

options.setExtractor(new FileImageExtractor(imgFolder));

// html中图片的路径 相对路径

options.URIResolver(new BasicURIResolver("image"));

options.setIgnoreStylesIfUnused(false);

options.setFragment(true);

// 3) 将 XWPFDocument转换成XHTML

// 生成html文件上级文件夹

File folder = new File(htmlPath);

if (!folder.exists()) {

folder.mkdirs();

}

OutputStream out = new FileOutputStream(htmlFile);

XHTMLConverter.getInstance().convert(document, out, options);

return htmlFile.getAbsolutePath();

}

open office

linux 一键安装脚本

cd /tmp

install_redhat() {

wget https://kkfileview.keking.cn/Apache_OpenOffice_4.1.6_Linux_x86-64_install-rpm_zh-CN.tar.gz -cO openoffice_rpm.tar.gz && tar zxf /tmp/openoffice_rpm.tar.gz && cd /tmp/zh-CN/RPMS

if [ $? -eq 0 ];then

yum install -y libXext.x86_64

yum groupinstall -y "X Window System"

rpm -Uvih *.rpm

echo 'install desktop service ...'

rpm -Uvih desktop-integration/openoffice4.1.6-redhat-menus-4.1.6-9790.noarch.rpm

echo 'install finshed...'

else

echo 'download package error...'

fi

}

install_ubuntu() {

wget https://kkfileview.keking.cn/Apache_OpenOffice_4.1.6_Linux_x86-64_install-deb_zh-CN.tar.gz -cO openoffice_deb.tar.gz && tar zxf /tmp/openoffice_deb.tar.gz && cd /tmp/zh-CN/DEBS

echo $?

if [ $? -eq 0 ];then

apt-get install -y libxrender1

apt-get install -y libxt6

apt-get install -y libxext-dev

apt-get install -y libfreetype6-dev

dpkg -i *.deb

echo 'install desktop service ...'

dpkg -i desktop-integration/openoffice4.1-debian-menus_4.1.6-9790_all.deb

echo 'install finshed...'

else

echo 'download package error...'

fi

}

if [ -f "/etc/redhat-release" ]; then

yum install -y wget

install_redhat

else

apt-get install -y wget

install_ubuntu

安装OpenOffice,安装结束后,调用命令行,启动OpenOffice的一项服务:

soffice -headless -accept="socket,port=8100;urp;"

测试 pom.xml

com.artofsolving

jodconverter

2.2.1

log4j

log4j

1.2.17

org.slf4j

slf4j-api

1.7.25

org.slf4j

slf4j-log4j12

1.7.25

test

org.slf4j

slf4j-simple

1.7.25

test

public class OfficeConverter {

public static void main(String[] args) {

File inputFile = new File("C:/test/yy.doc");

File outputFile = new File("C:/test/yy.html");

OpenOfficeConnection con = new SocketOpenOfficeConnection(8100);

try {

con.connect();

} catch (ConnectException e) {

System.err.println("文件转换出错,请检查OpenOffice服务是否启动。");

e.printStackTrace();

}

DocumentConverter converter = new OpenOfficeDocumentConverter(con);

converter.convert(inputFile, outputFile);

con.disconnect();

}

}

libreoffice

安装

yum install libreoffice libreoffice-headless

word 转 html

soffice --headless --convert-to html:HTML test.docx

poi、open office、libreoffice的区别

poi纯java转换,转换比较慢 openoffice小文件转换速度快,libreoffice转换较慢,大文件libreoffice转换快, openoffice只支持java代码操作,libreoffice支持命令行和java代码, 最重要一点openoffice支持队列转换,libreoffice不支持,同时转换一个文件会出现soffice卡死,

参考

来源:oschina

链接:https://my.oschina.net/u/3568600/blog/4411071

office 转换html,word转html相关推荐

  1. office另存为pdf的加载项_pdf怎么转换成word?打工人必备的丛林法则

    小编接受了一个工作,要批量将pdf转换成word,而且不太花钱的我(和大部分人一样,只想找免费的工具),在同事的帮助下,找到了很不错的办法,再加上我自己总结的几个办法,给大家分享一些干货. 网上有不少 ...

  2. 【Python服务生活系列--2】实现WPS Office付费功能 word转换纯图pdf

    前言 大家好,今天来填上一次埋下的坑~~本期为大家讲解如何通过Python实现wps office当中的付费功能,word转换纯图pdf. 我的思路 前言 1:将word文件转换为普通pdf文件 2: ...

  3. 基于java的格式转换,word 转 pdf、word 转图片、office 格式转换、在线文件预览

    一.项目简介 不管你是java程序员.c++程序员,python程序员,在开发项目中肯定遇到过格式转换的问题,如何轻松搞定格式转换的问题呢?当然是百度啦!面向百度编程已经成为当下程序员的日常操作. 基 ...

  4. h5在线浏览word_怎样将PDF在线转换成Word?教你成为一个高手的方法

    在我们的日常生活工作学习中,是很需要的Office办公软件的.不过我们经常使用的还是PDF格式的文件.但是如果我们想在PDF文件上再编辑文字时,就要把PDF转换成Word格式再编辑.在线PDF转换成W ...

  5. 怎样把pdf转换成word

    PDF格式良好的视觉阅读性和通用性使得PDF文件的使用越来越广泛了,网络上的PDF资料也越来越多,但是我们往往想要提出某些资料里面的部分文字内容进行二次编辑,那么我们这里就是讲比较通用的PDF转为WO ...

  6. python批量将pdf转成word_如何用Python把pdf转换成word

    很多时候,我们需要把文件的形式来回转换.那么学了编程的小伙伴,我们该如何用Python把pdf转换成word呢? 一.下载所需要的库 1.pdfminer 安装库命令pip install pdfmi ...

  7. 转换文档参数_1分钟教会你将Excel转换成Word,简单高效,办公人士必备神技

    现在不管是在学习上还是工作中,我们和Word文档.Excel表格的接触是越来越多了.有一些小伙伴在用Excel做完表格数据以后,有的时候会因为便于查看等等原因需要用Word打开Excel表格.这时候我 ...

  8. 这三个步骤让你知道WPS如何转换成WORD文档

    在日常的学习当中,有些小伙伴的课后作业用WPS完成后,保存下来可能会被存档为WPS文件的格式,但由于老师要求课后作业以WORD格式上交,怎么办呢?这时候我们就需要将WPS转换成WORD的形式.那你们知 ...

  9. WPS文字在线转换成Word

    WPS文字我们也会有很多人在用,但是习惯了Word文档的人突然用到WPS文件也会有所不适应,自己想去更改里面的某些参数的话都不知道在哪个地方,其实这也不是没有办法,我们可以选择使用迅捷PDF在线转换器 ...

最新文章

  1. SAP S/4HANA里如何创建Customer主数据以及执行后续处理
  2. r中如何求变量的对数转换_对数转换以求阳性。
  3. c/c++面试试题(四)
  4. php里面没有mssql,为什么没有正确使用PHP / MSSQL的日期/时间?
  5. 在visual studio 2010+中调用ffmpeg编译时 报错error LNK xxxx: 模块对于 SAFESEH 映像是不安全的。...
  6. Git如何进行分支合并
  7. [Hive]看懂Hive的执行计划
  8. dfmea文件_DFMEA范本.doc
  9. Charles(青花瓷)抓包工具的使用
  10. 你们要的后台管理系统全套,今天它来了【开源推荐】
  11. 中职计算机录取分数线是多少,中职技校录取分数线划定
  12. 云服务下半场,企业增长的超级入口在哪?
  13. 培养出儿子清华女儿北大的家庭十条家规
  14. 工具类 - 生成6位短信验证码
  15. iOS开发之去除图片白色背景
  16. anaconda3 复制or克隆环境
  17. dz邮箱验证怎么设置_详细步骤!Discuz如何设置通过 SOCKET 连接 SMTP 服务器发送(支持 ESMTP 验证)实现论坛邮箱验证功能...
  18. 因子分解机FM算法(Factorization Machine)
  19. 系统调用功能号与execve函数详解
  20. C语言完整代码实现:二叉树的先序遍历、中序遍历、后序遍历

热门文章

  1. linux创建新用户
  2. (LaTeX) 使用 BibTeX 引用网页
  3. 天梯赛 L2-028 秀恩爱分得快 (25 分)[测试点3 4 5][未填坑]
  4. 利用Python解决最短路径问题
  5. java 抛物线_关于java中模拟抛物线轨迹的问题
  6. 浙江省拟将频繁跳槽纳入征信系统,大家怎么看?
  7. 在制作WORD小报时添加艺术横线或者艺术竖线
  8. C# 多线程(菜鸟教程及爱整理)
  9. PHP中邮箱地址的正则表达式,php邮箱地址正则表达式实现与详解,邮箱地址正则表达式_PHP教程...
  10. hihoCoder挑战赛A题——顺子