Excel文件转成html页面代码
main类:启动类
- public class testToHtml {
复制代码- public static void main(String[] args) {
复制代码- // TODO Auto-generated method stub
复制代码- // System.out.println(System.getProperty("java.library.path"));
复制代码- //-Djava.library.path=D:\jar\jacob_1.9
复制代码- th.excelToHtml("d:/excel/运维门户通讯录.xlsx", "d:/test.html");
复制代码 代码:
- import java.io.BufferedReader;
复制代码- import java.io.BufferedWriter;
复制代码- 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 void wordToHtml(String docfile, String htmlfile)
复制代码- ActiveXComponent app = new ActiveXComponent("Word.Application"); // word
复制代码- 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);
复制代码- app.invoke("Quit", new Variant[] {});
复制代码- public void excelToHtml(String xlsfile, String htmlfile)
复制代码- ActiveXComponent app = new ActiveXComponent("Excel.Application"); // excel
复制代码- 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);
复制代码- app.invoke("Quit", new Variant[] {});
复制代码- public void delFolder(String folderPath)
复制代码- delAllFile(folderPath); //
复制代码- String filePath = folderPath;
复制代码- filePath = filePath.toString();
复制代码- java.io.File myFilePath = new java.io.File(filePath);
复制代码- } catch (Exception e) {e.printStackTrace();}
复制代码- public boolean delAllFile(String path)
复制代码- File file = new File(path);
复制代码- String[] tempList = file.list();
复制代码- for (int i = 0; i < tempList.length; i++)
复制代码- if (path.endsWith(File.separator))
复制代码- temp = new File(path + tempList[i]);
复制代码- temp = new File(path + File.separator + tempList[i]);
复制代码- delAllFile(path + "/" + tempList[i]);//
复制代码- delFolder(path + "/" + tempList[i]);//
复制代码 [code][/code]
|
|