前言

最近做的一个项目有个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. cf round #421 div2 D. Mister B and PR Shifts
  2. jset编写测试vue代码_使用 Jest 进行 Vue 单元测试
  3. 打造vim的python编辑器
  4. 【Top】Plan (updating...)
  5. Arduino IDE 配置文件
  6. PL/SQL Developer调试Oracle存储过程
  7. excel表转换成txt导入
  8. ORACLE的跟踪文件
  9. OpenSSL历史版本整理
  10. Google推出免费公共域名解析DNS服务
  11. Vue:embed结合ElementUI中dialog实现PDF文件预览
  12. EOS钱包开发注意交易消失的情况
  13. (四轴)无人机工作原理及组成
  14. PMP笔记-风险应对策略的区别
  15. VScode使用markdown
  16. 野火PID上位机通信移植
  17. ERP实施心得(转)
  18. 微型计算机结构框图,微型计算机系统结构图.doc
  19. (附源码)计算机毕业设计SSM黄淮学院二手物品交易平台
  20. 【步态识别】LagrangeGait基于拉格朗日《Lagrange Motion Analysis and View Embeddings for Improved Gait Recognition》

热门文章

  1. STM32电路板调试Could not power up debug port
  2. scanf(“%3c%3c“, a, b)
  3. thymeleaf 遍历map
  4. Linux创建文件的四种方式
  5. 动态二进制插桩的原理和基本实现过程(Pin/DynamoRIO/Frida)
  6. LeetCode题解PDF百度网盘下载
  7. 每日签到功能怎么实现的?
  8. 使用闲置手机搭配花生壳创建一个https服务器
  9. 以我之名,壮哉我大洛克萨斯!
  10. TextView字体样式