html2word(带图片)

it2023-05-15  101

1 先引入下面jar包

/WebContent/WEB-INF/lib/docx4j-3.3.4.jar /WebContent/WEB-INF/lib/docx4j-ImportXHTML-3.3.4.jar /WebContent/WEB-INF/lib/flying-saucer-pdf-9.0.4.jar /WebContent/WEB-INF/lib/guava-13.0.1.jar /WebContent/WEB-INF/lib/itext-1.4.6.jar /WebContent/WEB-INF/lib/itext-4.2.0.jar /WebContent/WEB-INF/lib/serializer-2.7.2.jar /WebContent/WEB-INF/lib/xalan-2.7.2.jar /WebContent/WEB-INF/lib/xercesImpl-2.11.0.jar /WebContent/WEB-INF/lib/xhtmlrenderer-3.0.0.jar /WebContent/WEB-INF/lib/xml-apis-1.4.01.jar /WebContent/WEB-INF/lib/xmlgraphics-commons-2.2.jar

 

2  文件工具类(将文件转换为base64编码)

public class FileUtil { /** * * @param path 文件全路径(加文件名) * @return String * @description 将文件转base64字符串 * @date 2019年11月24日 * @author rmk */ public static String fileToBase64(String path) { String base64 = null; InputStream in = null; try { File file = new File(path); in = new FileInputStream(file); byte[] bytes = new byte[(int) file.length()]; in.read(bytes); base64 = new String(Base64.encodeBase64(bytes),"UTF-8"); } catch (Exception e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return base64; } /** * @param outFilePath 输出文件路径, base64 base64文件编码字符串, outFileName 输出文件名 * @return String * @description BASE64解码成File文件 * @date 2019年11月24日 * @author rmk */ public static void base64ToFile(String outFilePath,String base64, String outFileName) { File file = null; //创建文件目录 String filePath=outFilePath; File dir=new File(filePath); if (!dir.exists() && !dir.isDirectory()) { dir.mkdirs(); } BufferedOutputStream bos = null; FileOutputStream fos = null; try { byte[] bytes = Base64.decodeBase64(base64.getBytes("UTF-8")); file=new File(filePath+"/"+outFileName); fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); bos.write(bytes); } catch (Exception e) { e.printStackTrace(); } finally { if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static String fanyi1(String str) throws UnsupportedEncodingException { return new String(Base64.encodeBase64(str.getBytes()),"UTF-8"); } public static String fanyi2(String str) throws UnsupportedEncodingException { return new String(Base64.decodeBase64(str.getBytes("UTF-8"))); } /** * 单元测试 */ public static void main(String[] args) { //定义文件路径 String filePath = "F:\\exer\\yuanwenjian\\签章.docx"; //将文件转base64字符串 String base64 = fileToBase64(filePath); System.out.println(base64); //定义输出文件的路径outFilePath和输出文件名outoutFileName String outFilePath = "F:\\exer\\gaizhangwenjain"; String outFileName = "签章.docx"; //将BASE64解码成File文件 base64ToFile(outFilePath, base64, outFileName); } }

3   将html内容转转为word文档(保留样式)

 注意:需要处理未闭合标签(例如 img)

String htmlStr = "<html>" + request.getParameter("htmlStr").replace("&nbsp;","").replace("data-test=\"img\">","data-test=\"img\"></img>") + "</html>"; WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); NumberingDefinitionsPart ndp = new NumberingDefinitionsPart(); wordMLPackage.getMainDocumentPart().addTargetPart(ndp); ndp.unmarshalDefaultNumbering(); // Convert the XHTML, and add it into the empty docx we made XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage); XHTMLImporter.setHyperlinkStyle("Hyperlink"); wordMLPackage.getMainDocumentPart().getContent().addAll( XHTMLImporter.convert(htmlStr, "") ); wordMLPackage.save(new java.io.File(filePath+id + ".docx"));

 

最新回复(0)