Excel转html(不带图片)

发布时间:2020-06-26 09:01:26 作者:Red_Ant_hoyl
来源:网络 阅读:1288

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

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("<h3>Sheet1</h3>", "")
                                                 .replace("<h3>Sheet2</h3>", "")
                                                 .replace("<h3>Sheet3</h3>", "")
                                                 .replace("<h3>Sheet4</h3>", "")
                                                 .replace("<h3>Sheet5</h3>", "");

                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的效果。

Excel转html(不带图片)

Excel转html(不带图片)

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

Excel转html(不带图片)

推荐阅读:
  1. HTML转Excel并实现打印,下载功能的案例
  2. php怎么实现抓取不带后缀的图片

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

excel html ce

上一篇:浅谈数据中心布线拓扑结构

下一篇:创建Maven项目下的Dubbo+Zookeeper框架

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》