Excel转html(不带图片)
这种方法,比较不合时宜。因此,直接贴出代码,只做记录。
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的效果。
可以看出,格式错乱,不推荐使用,这里只做记录探索。且不支持07以上版本,对一般格式的xls文档的支持还可以。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。