码迷,mamicode.com
首页 > Web开发 > 详细

Excel转html(不带图片)

时间:2018-09-27 23:54:13      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:pat   tco   oms   src   doc   learn   css   ges   res   

这种方法,比较不合时宜。因此,直接贴出代码,只做记录。

package com.css.java.learning.massbag;

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 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;
/**
 * Excel转html
 * @author Red_Ant
 * 20180927
 */
public class ExcelToHtml {

    public static void main(String[] args) {
        try {
            excelToHtml("D:\\red_ant_file\\20180927\\样例.xls", "D:\\red_ant_file\\20180927\\20180927.html");
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }
        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, "UTF-8");
                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("<h2>Sheet1</h2>", "")
                                                 .replace("<h2>Sheet2</h2>", "")
                                                 .replace("<h2>Sheet3</h2>", "")
                                                 .replace("<h2>Sheet4</h2>", "")
                                                 .replace("<h2>Sheet5</h2>", "");

                writeFile(htmlStr, outputFile);
        }

        public static void writeFile(String content, String path) {
                FileOutputStream fos = null;
                BufferedWriter bw = null;
                File file = new File(path);
                try {
                        fos = new FileOutputStream(file);
                        bw = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
                        bw.write(content);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                        try {
                                if (null != bw) {
                                        bw.close();
                                }
                                if (null != fos) {
                                        fos.close();
                                }
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                }
        }
}

比较low的效果。

技术分享图片

技术分享图片

可以看出,格式错乱,不推荐使用,这里只做记录探索。且不支持07以上版本,对一般格式的xls文档的支持还可以。

技术分享图片

Excel转html(不带图片)

标签:pat   tco   oms   src   doc   learn   css   ges   res   

原文地址:http://blog.51cto.com/13479739/2286906

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!