java中poi4.12怎么将excke转成html文件

html-css08

java中poi4.12怎么将excke转成html文件,第1张

java中poi4.12怎么将excke转成html文件,解决方案2:使用HTML打开Excel文件(此方法仅在用户可以打开损坏的Excel文件时才有效)

打开受损文件,然后单击选择另存为。

选择将原文件存储到其他位置并用网页的格式保存。确保选中完整的电子表格。

用excell打开保存的文件,并以Excel格式再次保存。

如果运气足够好,文件损坏就会消失

前几天经理说要做一个打印的功能,功能简单,设定打印模板后内容填充再转换HTML给前台,前台调用打印机就行了。在网上搜了下word内容填充,发现了poi-tl工具包,查看 文档 的时候发现需要poi版本在4.1.1以上:错误出现在FileImageExtractor类的extract方法调用IOUtils.copy时: 项目poi之前是3.16的,转html没有问题,于是将poi降版本到4.0.0,发现还是报同样的错误,再降版本到3.17,错误神奇的消失了,然后分别查看4.0.0,3.17的IOUtils.copy方法: 对比两个版本的copy方法,可以发现4.0.0的copy方法增加了返回值,3.17则是void的,然后再回到出错的地方,FileImageExtractor类存在于org.apache.poi.xwpf.converter.core包下 这个包在我程序中是1.0.0版本,在pom中ctrl单击core包可以看到它内置了3.8版本的poi 将poi版本更换为3.8可以看到copy方法果然是没有返回值的 也就是说FileImageExtractor类的extract方法在调用IOUtils.copy( in, out )时期待一个没有返回值的copy方法,但是poi 4.1.1却提供了一个return long的copy方法,自然会报NoSuchMethodError。 如果像我这样对poi版本有强制要求并且word转html时需要保存图片时,就只好自己写个继承IImageExtractor的FileImageExtractor和IOUtils类了。

实现代码如下:

public class Word2Html { public static void main(String argv[]) { try { //word 路径 html输出路径 convert2Html("D:/doctohtml/1.doc","D:/doctohtml/1.html") } catch (Exception e) { e.printStackTrace() } } public static void writeFile(String content, String path) { FileOutputStream fos = null BufferedWriter bw = null 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) { } } } public static void convert2Html(String fileName, String outPutFile) throws TransformerException, IOException, ParserConfigurationException { HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileName))//WordToHtmlUtils.loadDoc(new FileInputStream(inputFile)) 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 ) { //html 中 图片标签中 显示的图片路路径 <img src="d:/test/0.jpg"/>return "d:/doctohtml/"+suggestedName} } ) wordToHtmlConverter.processDocument(wordDocument) //save pictures List pics=wordDocument.getPicturesTable().getAllPictures() if(pics!=null){ for(int i=0i<pics.size()i++){ Picture pic = (Picture)pics.get(i) System.out.println() try { //word中图片的存储路径 pic.writeImageContent(new FileOutputStream("D:/doctohtml/" + 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) }}