龙空技术网

程序员必备知识干货:Excel文件转成html页面代码

狮范客 160

前言:

眼前你们对“netexcel另存为html”大致比较着重,同学们都想要分析一些“netexcel另存为html”的相关资讯。那么小编也在网上收集了一些关于“netexcel另存为html””的相关文章,希望大家能喜欢,咱们一起来了解一下吧!

私信我或关注微信号:狮范儿,回复:学习,获取免费学习资源包。

Excel文件转成html页面代码

main类:启动类

package com.test; import trans.toHtml; public class testToHtml { /** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub toHtml th=new toHtml(); // System.out.println(System.getProperty("java.library.path")); //-Djava.library.path=D:\jar\jacob_1.9 th.excelToHtml("d:/excel/运维门户通讯录.xlsx", "d:/test.html");} }

代码:

package trans; import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileWriter;import java.io.IOException;import java.io.InputStreamReader; import com.jacob.activeX.ActiveXComponent;import com.jacob.com.Dispatch;import com.jacob.com.Variant; public class toHtml { int WORD_HTML = 8; int WORD_TXT = 7; int EXCEL_HTML = 44;  /** * WORDתHTML * @param docfile WORD ļ ȫ·  * @param htmlfile ת HTML ·  */ public void wordToHtml(String docfile, String htmlfile){ ActiveXComponent app = new ActiveXComponent("Word.Application"); // word try { app.setProperty("Visible", new Variant(false)); Dispatch docs = app.getProperty("Documents").toDispatch(); Dispatch doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] { docfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch(); Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {htmlfile, new Variant(WORD_HTML) }, new int[1]); Variant f = new Variant(false); Dispatch.call(doc, "Close", f); } catch (Exception e) { e.printStackTrace(); } finally { app.invoke("Quit", new Variant[] {}); } }  /** * EXCELתHTML * @param xlsfile EXCEL ļ ȫ·  * @param htmlfile ת HTML ·  */ public void excelToHtml(String xlsfile, String htmlfile){ ActiveXComponent app = new ActiveXComponent("Excel.Application"); // excel try { app.setProperty("Visible", new Variant(false)); Dispatch excels = app.getProperty("Workbooks").toDispatch(); Dispatch excel = Dispatch.invoke(excels,"Open",Dispatch.Method,new Object[] { xlsfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch(); Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {htmlfile, new Variant(EXCEL_HTML) }, new int[1]); Variant f = new Variant(false); Dispatch.call(excel, "Close", f); } catch (Exception e) { e.printStackTrace(); } finally { app.invoke("Quit", new Variant[] {}); } }  /** * /ɾ ļ  * @param folderPath ļ ȫ·  * @param htmlfile ת HTML ·  */ public void delFolder(String folderPath){ try { delAllFile(folderPath); //ɾ  String filePath = folderPath; filePath = filePath.toString(); java.io.File myFilePath = new java.io.File(filePath); myFilePath.delete(); //ɾ ļ  } catch (Exception e) {e.printStackTrace();} }  /** * /ɾ ļ ļ * @param path ļ ȫ·  */ public boolean delAllFile(String path){ boolean flag = false; File file = new File(path); if (!file.exists()) { return flag; } if (!file.isDirectory()) { return flag; } String[] tempList = file.list(); File temp = null; for (int i = 0; i < tempList.length; i++) { if (path.endsWith(File.separator)) { temp = new File(path + tempList[i]); } else { temp = new File(path + File.separator + tempList[i]); } if (temp.isFile()) { temp.delete(); } if (temp.isDirectory()) { delAllFile(path + "/" + tempList[i]);// ɾ ļ ļ delFolder(path + "/" + tempList[i]);// ɾ ļ  flag = true; } } return flag; }}需要的jar包<<jacob.jar>><<toHtml.java>><<testToHtml.java>>

来源网络,侵权联系删除

私信我或关注微信号:狮范儿,回复:学习,获取免费学习资源包。

标签: #netexcel另存为html