07word 轉html 包括圖片轉換

要求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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章