1 package com.test.poiword;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.webkit.WebSettings;
6 import android.webkit.WebView;
7
8 import com.test.poiword.utils.FileUtils;
9
10 import org.apache.poi.hwpf.HWPFDocument;
11 import org.apache.poi.hwpf.converter.PicturesManager;
12 import org.apache.poi.hwpf.converter.WordToHtmlConverter;
13 import org.apache.poi.hwpf.usermodel.Picture;
14 import org.apache.poi.hwpf.usermodel.PictureType;
15 import org.w3c.dom.Document;
16
17 import java.io.BufferedWriter;
18 import java.io.ByteArrayOutputStream;
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.OutputStreamWriter;
25 import java.util.List;
26
27 import javax.xml.parsers.DocumentBuilderFactory;
28 import javax.xml.transform.OutputKeys;
29 import javax.xml.transform.Transformer;
30 import javax.xml.transform.TransformerFactory;
31 import javax.xml.transform.dom.DOMSource;
32 import javax.xml.transform.stream.StreamResult;
33
34 /**
35 *
36 */
37 public class WordHtmlActivity extends Activity {
38 //文件存储位置
39 private String docPath = "/mnt/sdcard/doc/";
40 //文件名称
41 private String docName = "word3.doc";
42 //html文件存储位置
43 private String savePath = "/mnt/sdcard/doc/";
44 @Override
45 protected void onCreate(Bundle savedInstanceState) {
46 super.onCreate(savedInstanceState);
47 setContentView(R.layout.html);
48 String name = docName.substring(0, docName.indexOf("."));
49 try {
50 convert2Html(docPath + docName, savePath + name + ".html");
51 } catch (Exception e) {
52 e.printStackTrace();
53 }
54 //WebView加载显示本地html文件
55 WebView webView = (WebView)this.findViewById(R.id.office);
56 WebSettings webSettings = webView.getSettings();
57 webSettings.setLoadWithOverviewMode(true);
58 webSettings.setSupportZoom(true);
59 webSettings.setBuiltInZoomControls(true);
60 webView.loadUrl("file:/"+savePath+name+".html");
61 }
62
63 /**
64 * word文档转成html格式
65 * */
66 public void convert2Html(String fileName, String outPutFile) {
67 HWPFDocument wordDocument = null;
68 try {
69 wordDocument = new HWPFDocument(new FileInputStream(fileName));
70 WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
71 DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
72 //设置图片路径
73 wordToHtmlConverter.setPicturesManager(new PicturesManager() {
74 public String savePicture(byte[] content,
75 PictureType pictureType, String suggestedName,
76 float widthInches, float heightInches) {
77 String name = docName.substring(0, docName.indexOf("."));
78 return name + "/" + suggestedName;
79 }
80 });
81 //保存图片
82 List<Picture> pics=wordDocument.getPicturesTable().getAllPictures();
83 if(pics!=null){
84 for(int i=0;i<pics.size();i++){
85 Picture pic = (Picture)pics.get(i);
86 System.out.println( pic.suggestFullFileName());
87 try {
88 String name = docName.substring(0,docName.indexOf("."));
89 String file = savePath+ name + "/"
90 + pic.suggestFullFileName();
91 FileUtils.makeDirs(file);
92 pic.writeImageContent(new FileOutputStream(file));
93 } catch (FileNotFoundException e) {
94 e.printStackTrace();
95 }
96 }
97 }
98 wordToHtmlConverter.processDocument(wordDocument);
99 Document htmlDocument = wordToHtmlConverter.getDocument();
100 ByteArrayOutputStream out = new ByteArrayOutputStream();
101 DOMSource domSource = new DOMSource(htmlDocument);
102 StreamResult streamResult = new StreamResult(out);
103
104 TransformerFactory tf = TransformerFactory.newInstance();
105 Transformer serializer = tf.newTransformer();
106 serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
107 serializer.setOutputProperty(OutputKeys.INDENT, "yes");
108 serializer.setOutputProperty(OutputKeys.METHOD, "html");
109 serializer.transform(domSource, streamResult);
110 out.close();
111 //保存html文件
112 writeFile(new String(out.toByteArray()), outPutFile);
113 } catch (Exception e) {
114 e.printStackTrace();
115 }
116 }
117
118 /**
119 * 将html文件保存到sd卡
120 * */
121 public void writeFile(String content, String path) {
122 FileOutputStream fos = null;
123 BufferedWriter bw = null;
124 try {
125 File file = new File(path);
126 if(!file.exists()){
127 file.createNewFile();
128 }
129 fos = new FileOutputStream(file);
130 bw = new BufferedWriter(new OutputStreamWriter(fos,"utf-8"));
131 bw.write(content);
132 } catch (FileNotFoundException fnfe) {
133 fnfe.printStackTrace();
134 } catch (IOException ioe) {
135 ioe.printStackTrace();
136 } finally {
137 try {
138 if (bw != null)
139 bw.close();
140 if (fos != null)
141 fos.close();
142 } catch (IOException ie) {
143 }
144 }
145 }
146 }
http://poi.apache.org/apidocs/overview-summary.html