资源简介

java实现在线预览,将word,excel转换成html

资源截图

代码片段和文件信息

package com.citic.util;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.poi.hssf.converter.ExcelToHtmlConverter;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.w3c.dom.Document;

public class PoiExcelToHtml2 {
public static void main(String[] args) throws Throwable {

excelToHtml(null “2017评分表样例.xls“);
}


 /**
     * Excel 转为 HTML
     * @param fileName
     * @param outputFile
     * @throws FileNotFoundException
     * @throws IOException
     * @throws ParserConfigurationException
     * @throws TransformerConfigurationException
     * @throws TransformerException 
     */
    public static void excelToHtml(String fileName String outputFile)
            throws FileNotFoundException IOException ParserConfigurationException 
                TransformerConfigurationException TransformerException {
        InputStream is = new FileInputStream(fileName);

        HSSFWorkbook excelBook = new HSSFWorkbook(is);

        ExcelToHtmlConverter ethc = new ExcelToHtmlConverter(
                DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
        ethc.setOutputColumnHeaders(false);
        ethc.setOutputRowNumbers(false);

        ethc.processWorkbook(excelBook);

        Document htmlDocument = ethc.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 “gbk“);
        serializer.setOutputProperty(OutputKeys.INDENT “yes“);
        serializer.setOutputProperty(OutputKeys.METHOD “html“);
        serializer.transform(domSource streamResult);
        out.close();
        
        String htmlStr = new String(out.toByteArray());
        
        htmlStr = htmlStr.replace(“

Sheet1

“ ““)
                         .replace(“

Sheet2

“ ““)
                         .replace(“

Sheet3

“ ““)
               

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       4382  2017-06-05 13:36  java实现word和excel转换成html\PoiExcelToHtml2.java

     文件       3174  2017-08-28 14:00  java实现word和excel转换成html\PoiWordToHtml.java

     目录          0  2017-08-28 14:01  java实现word和excel转换成html

----------- ---------  ---------- -----  ----

                 7556                    3


评论

共有 条评论