Exl轉換成html的方法

來源:互聯網
上載者:User

標籤:excel   office   jacob   

此方法採用了一個外掛程式:jacob-1.15-M4,這個外掛程式可以將office轉換成html,在此我只介紹了一種,將Exl轉換成html,轉換後的html中有的border線為0.5px,在有的瀏覽器中不識別0.5px的border線,需要將產生的css中的0.5px,轉換成1px,在系統中如果需要多次轉換的話建議寫一個單例模式,否則的話轉換幾次之後你電腦的記憶體就被撐爆了,原因很簡單,就是每一次轉換都會啟動一次Exl程式,啟動的多了系統記憶體就沾滿了,所以,建議還是寫成單例模式,廢話不多說,直接貼代碼:

外掛程式需要下載的可以點擊此處下載:http://download.csdn.net/detail/s592652578/8446163


package com.odon.common.util;

 

import java.io.BufferedReader;

import java.io.File;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.IOException;

 

import com.jacob.activeX.ActiveXComponent;

import com.jacob.com.Dispatch;

import com.jacob.com.Variant;

 

public classExlToHtm {

 

   //單例模式

   private static ExlToHtm exlToHtm=null;

 

   public static ExlToHtm getExlToHtm(){

      if(exlToHtm==null)

         exlToHtm=new ExlToHtm();

      return exlToHtm;

   }

 

   public static final int EXCEL_HTML = 44; 

   public static final ActiveXComponent app = new ActiveXComponent("Excel.Application");//啟動exl

 

   /** 

    * EXCEL轉HTML 

    * @param xlsfile EXCEL檔案全路徑 

    * @param htmlfile轉換後HTML存放路徑 

    */ 

   public synchronized void  excelToHtml(String xlsfile, Stringhtmlfile){ 

      //    ActiveXComponentapp = new ActiveXComponent("Excel.Application"); //啟動exl

      try { 

         app.setProperty("Visible",newVariant(false));  

         Dispatchexcels = app.getProperty("Workbooks").toDispatch();  

         Dispatchexcel = 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,newObject[] {  

                htmlfile,newVariant(EXCEL_HTML)},newint[1]);  

         Variantf = newVariant(false);  

         Dispatch.call(excel,"Close",f); 

      }catch (Exception e){  

         e.printStackTrace();  

      }finally{

         app.invoke("Quit",new Variant[] {});

      }  

   }

 

   /**

    * 修改css樣式

    * @param filePath

    * @param cssPath

    */

   public static void readCss(StringfilePath,String cssPath) { 

      BufferedReaderbr = null; 

      Stringline = null; 

      StringBufferbuf = newStringBuffer(); 

      Stringcontent = null;

      Filetxt=newFile(cssPath);

      FileOutputStreamfos = null;

      try { 

         fos= newFileOutputStream(txt);

         // 根據檔案路徑建立緩衝輸入資料流 

         br= newBufferedReader(newFileReader(filePath));

         // 迴圈讀取檔案的每一行,對需要修改的行進行修改,放入緩衝對象中 

         while ((line = br.readLine())!=null){ 

            // 此處根據實際需要修改某些行的內容 

            if(line.indexOf(":.5pt")>-1){

                line= line.replace(":.5pt",":1pt");

                line= line + "\r";

            }

            buf.append(line);

         } 

         //判斷檔案是否存在

         if(!txt.exists()){

            txt.createNewFile();

         }

         content= buf.toString();

         byte bytes[]=newbyte[1024];

         bytes=content.getBytes();   //新加的

         intb=content.length();  //改

         fos.write(bytes,0,b);

      }catch(Exception e) { 

         e.printStackTrace(); 

      }finally{ 

         // 關閉流 

         if (br !=null) { 

            try { 

                br.close(); 

            }catch(IOException e) { 

                br= null; 

            } 

         } 

         if( fos !=null){

            try {

                fos.close();

            }catch(Exception e2) {

                e2.printStackTrace();

            }

         }

      } 

   } 

}

 

Exl轉換成html的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.