Excel文件转成html页面代码

论坛 期权论坛 期权     
程序员java笔记   2019-7-13 08:12   2983   0
Excel文件转成html页面代码

main类:启动类
  1. package com.test;
复制代码
复制代码
  1. import trans.toHtml;
复制代码
复制代码
  1. public class testToHtml {
复制代码
复制代码
  1. /**
复制代码
  1. * @param args
复制代码
  1. */
复制代码
  1. public static void main(String[] args) {
复制代码
  1. // TODO Auto-generated method stub
复制代码
  1.          toHtml th=new toHtml();
复制代码
  1.         // System.out.println(System.getProperty("java.library.path"));
复制代码
  1.          //-Djava.library.path=D:\jar\jacob_1.9
复制代码
  1.          th.excelToHtml("d:/excel/运维门户通讯录.xlsx", "d:/test.html");
复制代码
  1. }
复制代码
复制代码
  1. }
复制代码
代码:
  1. package trans;
复制代码
复制代码
  1. import java.io.BufferedReader;
复制代码
  1. import java.io.BufferedWriter;
复制代码
  1. import java.io.File;
复制代码
  1. import java.io.FileInputStream;
复制代码
  1. import java.io.FileNotFoundException;
复制代码
  1. import java.io.FileWriter;
复制代码
  1. import java.io.IOException;
复制代码
  1. import java.io.InputStreamReader;
复制代码
复制代码
  1. import com.jacob.activeX.ActiveXComponent;
复制代码
  1. import com.jacob.com.Dispatch;
复制代码
  1. import com.jacob.com.Variant;
复制代码
复制代码
  1. public class toHtml {
复制代码
  1. int WORD_HTML = 8;
复制代码
  1. int WORD_TXT = 7;
复制代码
  1. int EXCEL_HTML = 44;
复制代码
复制代码
  1. /**
复制代码
  1.   * WORDHTML
复制代码
  1.   * @param docfile WORD  ·
复制代码
  1.   * @param htmlfile     HTML   ·
复制代码
  1.   */
复制代码
  1. public void wordToHtml(String docfile, String htmlfile)
复制代码
  1. {
复制代码
  1.   ActiveXComponent app = new ActiveXComponent("Word.Application"); //     word
复制代码
  1.   try
复制代码
  1.   {
复制代码
  1.    app.setProperty("Visible", new Variant(false));
复制代码
  1.    Dispatch docs = app.getProperty("Documents").toDispatch();
复制代码
  1.    Dispatch doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] { docfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch();
复制代码
  1.    Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {htmlfile, new Variant(WORD_HTML) }, new int[1]);
复制代码
  1.    Variant f = new Variant(false);
复制代码
  1.    Dispatch.call(doc, "Close", f);
复制代码
  1.   }
复制代码
  1.   catch (Exception e)
复制代码
  1.   {
复制代码
  1.    e.printStackTrace();
复制代码
  1.   }
复制代码
  1.   finally
复制代码
  1.   {
复制代码
  1.    app.invoke("Quit", new Variant[] {});
复制代码
  1.   }
复制代码
  1. }
复制代码
复制代码
  1. /**
复制代码
  1.   * EXCELHTML
复制代码
  1.   * @param xlsfile EXCEL  ·
复制代码
  1.   * @param htmlfile     HTML   ·
复制代码
  1.   */
复制代码
  1. public void excelToHtml(String xlsfile, String htmlfile)
复制代码
  1. {
复制代码
  1.   ActiveXComponent app = new ActiveXComponent("Excel.Application"); //     excel
复制代码
  1.   try
复制代码
  1.   {
复制代码
  1.    app.setProperty("Visible", new Variant(false));
复制代码
  1.    Dispatch excels = app.getProperty("Workbooks").toDispatch();
复制代码
  1.    Dispatch excel = Dispatch.invoke(excels,"Open",Dispatch.Method,new Object[] { xlsfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch();
复制代码
  1.    Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {htmlfile, new Variant(EXCEL_HTML) }, new int[1]);
复制代码
  1.    Variant f = new Variant(false);
复制代码
  1.    Dispatch.call(excel, "Close", f);
复制代码
  1.   }
复制代码
  1.   catch (Exception e)
复制代码
  1.   {
复制代码
  1.    e.printStackTrace();
复制代码
  1.   }
复制代码
  1.   finally
复制代码
  1.   {
复制代码
  1.    app.invoke("Quit", new Variant[] {});
复制代码
  1.   }
复制代码
  1. }
复制代码
复制代码
  1. /**
复制代码
  1.   * /      
复制代码
  1.   * @param folderPath     ·
复制代码
  1.   * @param htmlfile     HTML   ·
复制代码
  1.   */
复制代码
  1.   public void delFolder(String folderPath)
复制代码
  1. {
复制代码
  1.       try
复制代码
  1.       {
复制代码
  1.          delAllFile(folderPath); //               
复制代码
  1.          String filePath = folderPath;
复制代码
  1.          filePath = filePath.toString();
复制代码
  1.          java.io.File myFilePath = new java.io.File(filePath);
复制代码
  1.          myFilePath.delete(); //      
复制代码
  1.       } catch (Exception e) {e.printStackTrace();}
复制代码
  1. }
复制代码
复制代码
  1. /**
复制代码
  1.   * /               
复制代码
  1.   * @param path   ·
复制代码
  1.   */
复制代码
  1. public boolean delAllFile(String path)
复制代码
  1. {
复制代码
  1.         boolean flag = false;
复制代码
  1.         File file = new File(path);
复制代码
  1.         if (!file.exists())
复制代码
  1.         {
复制代码
  1.           return flag;
复制代码
  1.         }
复制代码
  1.         if (!file.isDirectory())
复制代码
  1.         {
复制代码
  1.           return flag;
复制代码
  1.         }
复制代码
  1.         String[] tempList = file.list();
复制代码
  1.         File temp = null;
复制代码
  1.         for (int i = 0; i < tempList.length; i++)
复制代码
  1.         {
复制代码
  1.            if (path.endsWith(File.separator))
复制代码
  1.            {
复制代码
  1.               temp = new File(path + tempList[i]);
复制代码
  1.            }
复制代码
  1.            else
复制代码
  1.            {
复制代码
  1.                temp = new File(path + File.separator + tempList[i]);
复制代码
  1.            }
复制代码
  1.            if (temp.isFile())
复制代码
  1.            {
复制代码
  1.               temp.delete();
复制代码
  1.            }
复制代码
  1.            if (temp.isDirectory())
复制代码
  1.            {
复制代码
  1.               delAllFile(path + "/" + tempList[i]);//              
复制代码
  1.               delFolder(path + "/" + tempList[i]);//        
复制代码
  1.               flag = true;
复制代码
  1.            }
复制代码
  1.         }
复制代码
  1.         return flag;
复制代码
  1.              }
复制代码
  1. }
复制代码
  1. 需要的jar包
复制代码
  1. [/code][code]
复制代码
[code][/code]
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:5
帖子:1
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP