本篇教程介绍了HTML+CSS入门 如何使用POI将doc文件转换为HTML,希望阅读本篇文章以后大家有所收获,帮助大家HTML+CSS入门。

<

需要的jar包有:有一些是依赖包,可以使用maven下载

doc文件转换为html文件

package com.gsww.sxzz.controller.service;

import org.apache.poi.hwpf.HWPFDocument;

import org.apache.poi.hwpf.converter.PicturesManager;

import org.apache.poi.hwpf.converter.WordToHtmlConverter;

import org.apache.poi.hwpf.usermodel.Picture;

import org.apache.poi.hwpf.usermodel.PictureType;

import org.jsoup.Jsoup;

import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.transform.OutputKeys;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerException;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import java.io.*;

import java.util.List;

/**

* Created by Carey on 15-2-2.

*/

public class docTohtml {

public static void main(String argv[]) {

try {

convert2Html("D:\\b.doc","D:\\1.html");

} catch (Exception e) {

e.printStackTrace();

}

}

//输出html文件

public static void writeFile(String content, String path) {

FileOutputStream fos = null;

BufferedWriter bw = null;

org.jsoup.nodes.Document doc = Jsoup.parse(content);

String styleOld=doc.getElementsByTag("style").html();

//统一字体格式为宋体

styleOld=styleOld.replaceAll("font-family:.+(?=;\\b)", "font-family:SimSun");

doc.getElementsByTag("head").empty();

doc.getElementsByTag("head").append("");

doc.getElementsByTag("head").append(" ");

doc.getElementsByTag("style").append(styleOld);

/*正则表达式查询字体内容:font-family:.+(?=;\b)*/

System.out.println(content);

content=doc.html();

content=content.replace("", "");

try {

File file = new File(path);

fos = new FileOutputStream(file);

bw = new BufferedWriter(new OutputStreamWriter(fos,"UTF-8"));

bw.write(content);

} catch (FileNotFoundException fnfe) {

fnfe.printStackTrace();

} catch (IOException ioe) {

ioe.printStackTrace();

} finally {

try {

if (bw != null)

bw.close();

if (fos != null)

fos.close();

} catch (IOException ie) {

}

}

}

//word 转 html

public static void convert2Html(String fileName, String outPutFile)

throws TransformerException, IOException,

ParserConfigurationException {

HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileName));//WordToHtmlUtils.loadDoc(new FileInputStream(inputFile));

//兼容2007 以上版本

//        XSSFWorkbook  xssfwork=new XSSFWorkbook(new FileInputStream(fileName));

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 )

{

return "test/"+suggestedName;

}

} );

wordToHtmlConverter.processDocument(wordDocument);

//save pictures

List pics=wordDocument.getPicturesTable().getAllPictures();

if(pics!=null){

for(int i=0;i

Picture pic = (Picture)pics.get(i);

System.out.println();

try {

pic.writeImageContent(new FileOutputStream("D:/test/"

+ pic.suggestFullFileName()));

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

}

Document htmlDocument = wordToHtmlConverter.getDocument();

ByteArrayOutputStream out = new ByteArrayOutputStream();

DOMSource domSource = new DOMSource(htmlDocument);

StreamResult streamResult = new StreamResult(out);

TransformerFactory tf = TransformerFactory.newInstance();

Transformer serializer = tf.newTransformer();

serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

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

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

serializer.transform(domSource, streamResult);

out.close();

writeFile(new String(out.toByteArray()), outPutFile);

}

}

遇到的问题,当doc转换为html时不会将图像的线条给转换过来。只有在table表格中才可以转换为span标签。如果要作下滑线,可以放一个table的单元格只设定下边框就可以完美转换为html了。

将html转换为pdf

package com.gsww.sxzz.controller.service;

import com.lowagie.text.pdf.BaseFont;

import org.xhtmlrenderer.pdf.ITextFontResolver;

import org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.OutputStream;

/**

* Created by Carey on 15-2-2.

*/

public class htmlToPdf {

public boolean convertHtmlToPdf(String inputFile, String outputFile)

{

try {

OutputStream     os = new FileOutputStream(outputFile);

ITextRenderer renderer = new ITextRenderer();

String url = new File(inputFile).toURI().toURL().toString();

renderer.setDocument(url);

// 解决中文支持问题

ITextFontResolver fontResolver = renderer.getFontResolver();

/*fontResolver.addFont("C:\\Windows\\Fonts\\simsunb.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

*///宋体文件的相对路径

fontResolver.addFont("C:\\Windows\\Fonts\\simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

renderer.getSharedContext().setBaseURL("file:/D:/");

renderer.layout();

renderer.createPDF(os);

os.flush();

os.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return true;

}

public   static  void  main(String [] args){

htmlToPdf html2Pdf =new htmlToPdf();

try {

html2Pdf.convertHtmlToPdf("D:\\1.html","D:\\index.pdf");

} catch (Exception e) {

e.printStackTrace();

}

}

}

本文由职坐标整理发布,欢迎关注职坐标WEB前端HTML/CSS频道,获取更多HTML/CSS知识!

doc文件转换html,HTML+CSS入门 如何使用POI将doc文件转换为HTML相关推荐

  1. 基于Python的DICOM文件转换教程,使用pydicom将图片存为DICOM文件。

    基于Python的DICOM文件转换教程,使用pydicom将图片存为DICOM文件. DICOM是医学图像和信息的数字化标准,可用于将医学影像数据.诊断报告等信息在医疗领域进行传输.分享和分析.而常 ...

  2. java文件转换成byte数组以及byte数组上传到文件

    文件转换成byte数组 /*** 文件 File file = new File("...");* @param filePath* @return*/public static ...

  3. python文件处理基础_[Python入门及进阶笔记]Python-基础-文件处理小结

    博客迁往:新地址(点击直达) 新博客使用markdown维护,线下有版本库,自己写的所以会定时更新同步,同时提供更好的导航和阅读体验 csdn对markdown支持不好,所以旧版不会花时间进行同步修订 ...

  4. 【PDF转换图片】如何把pdf文件转换成图片?如何把批量pdf文件转换成图片?如何把多目录批量pdf文件转换成图片?如何pdf文件转换成图片不失真不损失清晰度?今天教方法

    在工作中常常需要将PDF转换为图片,好多免费但是图片有水印和功能不全的问题. 还有市面上很多软件知识完成了部分的工作,比如只能单个文件转PDF,那么遇到多文件的就只能挨个转么? 还有转换的格式支持的非 ...

  5. 和doc的区别怎么转换手机_如何把doc文件转换成PDF格式?高手告诉你应该这么做...

    视频步骤: 知乎视频​www.zhihu.com 有时候老师让交的实验报告文件格式必须为PDF格式,因为PDF与不同的操作系统和软件版本具有很好的兼容性,所以大多数人都喜欢将Word转换成PDF.但有 ...

  6. react在线浏览doc_如何把doc文件转换成PDF格式?教你一招秒实现

    有时候老师让交的实验报告文件格式必须为PDF格式,因为PDF与不同的操作系统和软件版本具有很好的兼容性,所以大多数人都喜欢将Word转换成PDF.但有的同学的Word版本不能直接将doc文件转换成PD ...

  7. pdf转换成word后有文字叠加_PDF文件转换成word后,文字为什么都没 – 手机爱问

    2006-08-27 怎么把pdf转化成可编辑word文本格式 (转贴)在当今的计算机世界里,使用率最高的两种文档方式分别是Microsoft Word的Doc格式和Adobe Acrobat的Pdf ...

  8. 不支持所上传的文件格式。请确认选择的文件无误。_职场人必备!如何把PDF文件转换成word文档?...

    点击箭头处"蓝色字",关注我们哦!! 再撑一下!明天就是周末了! PDF大家都用过,它是平时我们办公中最常用到的文件格式之一.它是一种可移植文档格式,与操作系统平台无关,在任何系统 ...

  9. 怎么将录音文件转换成文字呢?

    录音是小伙伴们在日常生活和工作中经常使用的一种操作.毕竟每个手机基本都有录音功能.这个功能让录音变得非常简单,每一个小伙伴职场中,参加各种大大小小的会议是无法回避的任务.而在会议过程中,打开电话录音, ...

最新文章

  1. 重磅回击!ZeniMax回击Oculus新试验
  2. 企业客户都满意的ToB产品运营秘诀
  3. 南京工业大学python考试期末题库_大学慕课2020用Python玩转数据期末考试大全答案...
  4. Cygwin,Nutch安装配置,检验是否正确(对网友守望者博客的修改---在此感谢守望者)2
  5. 02.es的节点发现和集群构建
  6. excel同一单元格怎么换行_excel表格内怎么换行 方法有两种 一看就会 新手教程...
  7. java过时_Java 语言的几个缺陷之四: 过时的 JavaBean
  8. Python面试题【315+道题】
  9. mysql5.6.40免安装版配置方案
  10. VALSE学习(三):深度学习梯度讲解
  11. Python自学笔记-map和reduce函数(来自廖雪峰的官网Python3)
  12. python 多继承 MRO
  13. java tire树_谢特——后缀数组+tire 树(示例代码)
  14. Meta拟裁撤Instagram伦敦员工 其余人将调往美国---转自百度新闻|财联社
  15. 吃热狗游戏Java_《我的世界》吃才是王道“食物工艺”让你尝遍八大菜系
  16. SMP,UMA,NUMA,MPP的概念理解
  17. Appium 按压元素进行滑动
  18. 计算机科学与技术论文选题怎么选,比较好写的计算机科学与技术专业论文选题 计算机科学与技术专业论文题目如何取...
  19. Java获取当天0时0分0秒时间的方法
  20. java日志体系分析

热门文章

  1. iOS学习之UItableView
  2. 【转】Mybatis/Ibatis,数据库操作的返回值
  3. HTTP、TCP、UDP、Socket (转)
  4. ★ Flex を使って Scalable Vector Graphics とビットマップを描画する
  5. [Leetcode][第5458题][JAVA][字符串的好分割数目][双指针][HashSet]
  6. apriori算法代码_sklearn(九)apriori 关联规则算法,以及FP-growth 算法
  7. asp按时间自动递增编号_约束力最强的手铐——美国ASP钢性手铐
  8. python postgresql跨数据库查询_python访问PostgreSQL数据库之连接库Psycopg2
  9. java form的时间格式_SpringMvc接收日期表单提交,自动转换成Date类型方法
  10. 计算机入门在线阅读,电脑入门基础教程..doc