要求07word实现在线预览,当时上网找了很多都是有各种问题,没有成功。最后自己也是东拼西凑完成了
import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.net.URL; import javax.servlet.http.HttpSession; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.TransformerException; import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.poi.xwpf.converter.core.BasicURIResolver; import org.apache.poi.xwpf.converter.core.FileImageExtractor; import org.apache.poi.xwpf.converter.core.XWPFConverterException; import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter; import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions; import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class WordToHtml {
public static String writeFile(String content, String path) { FileOutputStream fos = null; BufferedWriter bw = null; File file = null; try { file = new File(path + “.html”); // file.mkdirs(); if (!file.exists()) {
} fos = new FileOutputStream(file); bw = new BufferedWriter(new OutputStreamWriter(fos)); 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) { } }
return file.getName(); }
/** * 将word转换成html 支持 .doc and .docx * * @param fileName * word文件在 数据库中保存的FTP的文件访问路径 * @throws TransformerException * @throws IOException * @throws ParserConfigurationException */ public static String convert2Html(String fileName, HttpSession session){
//获 文件名称没有后缀 文件名 前有 / String name = fileName.substring(fileName.lastIndexOf(“/”), fileName.lastIndexOf(“.”));
int indexOf = fileName.indexOf(“/”, fileName.indexOf(“.”));
int lastIndexOf = fileName.lastIndexOf(“/”); // 文件地址 后面有/ String string = fileName.substring(indexOf, lastIndexOf + 1); // 本地工作空间文件夹路径 String workPath = session.getServletContext().getRealPath(“/webapp/html”).replaceAll(“\\”, “/”);
File file = new File(workPath + “/” + string); if (!file.exists()) { file.mkdirs(); }
ByteArrayOutputStream out = new ByteArrayOutputStream();
URL url2 = null; InputStream inputStream = null; XWPFDocument document = null; try { url2 = new URL(fileName); inputStream = url2.openConnection().getInputStream(); //用POI解析这个word文件 document = new XWPFDocument(inputStream); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
String imagePathStr = workPath + “/” + string + name + “/”; name = name.substring(1); //设置word文档中图片保存的路径 XHTMLOptions options = XHTMLOptions.create(); options.URIResolver(new BasicURIResolver(name)); File file2 = new File(imagePathStr); if (!file2.exists())file2.mkdirs(); options.setExtractor(new FileImageExtractor(file2)); options.setIgnoreStylesIfUnused(false); options.setFragment(true);
try { XHTMLConverter.getInstance().convert(document, out, options); } catch (XWPFConverterException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { if(out!=null)out.close(); if(document!=null)document.close(); if(inputStream!=null)inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //写出这个word文档 返回word文件所在的位置,为了FTP上传 String string2 = writeFile(new String(out.toByteArray()), workPath + “/” + string + name);
return string2; } }
jar包(如有不全,请联系我)
<dependency> <groupId>com.artofsolving</groupId> <artifactId>jodconverter</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>3.14</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.14</version> </dependency> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>org.apache.poi.xwpf.converter.core</artifactId> <version>1.0.6</version> </dependency> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId> <version>1.0.6</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.16</version><!--3.16 --> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-examples</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-excelant</artifactId> <version>3.9</version> </dependency>