使用JACOB进行Excel读写控制

1、  下载JACOB包:http://danadler.com/jacob/

2、  安装配置

解压jacobBin_17.zip

1jacob.dll所在目录放到PATH

2jacob.jar放到CLASSPATH

3、使用jacob进行Excel控制,下面是我写的一个测试程序代码

 

import com.jacob.com.*;
import com.jacob.activeX.*;

class ExcelTest
{
  private static ActiveXComponent xl;
  private static Object workbooks = null;
  private static Object workbook = null;
  private static Object sheet = null;
  private static String filename =null;
  private static boolean readonly = false;
 
  public static void main(String[] args)
  {
     String file = "f://java//test.xls";
     OpenExcel(file,false);//false为不显示打开Excel
     SetValue("A1","Value","2");
     System.out.println(GetValue("A3"));
     CloseExcel(false);
  }
 
  //打开Excel文档
  private static void OpenExcel(String file,boolean f)
  {
    try
    {
        filename = file;
        xl = new ActiveXComponent("Excel.Application");
        xl.setProperty("Visible", new Variant(f));
        workbooks = xl.getProperty("Workbooks").toDispatch();
         workbook = Dispatch.invoke(workbooks,
                "Open",
                Dispatch.Method,
                                    new Object[]{filename,
                                    new Variant(false),
                                    new Variant(readonly)},//是否以只读方式打开
                                    new int[1] ).toDispatch();
    }catch(Exception e)
    {e.printStackTrace();}
  }
 
  //关闭Excel文档
  private static void CloseExcel(boolean f)
  {
   try
   {   
     Dispatch.call(workbook,"Save");
         Dispatch.call(workbook, "Close", new Variant(f));
      } catch (Exception e) {
         e.printStackTrace();
     } finally {
     xl.invoke("Quit", new Variant[] {});
      }
  }
 
  //写入值
  private static void SetValue(String position,String type,String value)
  {
     sheet = Dispatch.get(workbook,"ActiveSheet").toDispatch(); 
     Object cell = Dispatch.invoke(sheet, "Range",
             Dispatch.Get,
                                    new Object[] {position},
                                    new int[1]).toDispatch();
      Dispatch.put(cell, type, value);
  }

  //读取值  
  private static String GetValue(String position)
  {
    Object cell = Dispatch.invoke(sheet,"Range",Dispatch.Get,new Object[] {position},new int[1]).toDispatch();
  String value = Dispatch.get(cell,"Value").toString();
  
  return value;
  }
}

相关推荐
一个简单示例 初始化当前Java线程STA ComThread InitSTA ; ActiveXComponent xl new ActiveXComponent "Excel Application" ; try{ 打开现有的Excel xl setProperty "Visible" new Variant false ; Dispatch workbooks xl getProperty "Workbooks" toDispatch ; Dispatch workbook Dispatch invoke workbooks "Open" Dispatch Method new Object[]{excelPath new Variant false 是否只读 new Variant false } 是否显示打开 new int[1] toDispatch ; 得到所有sheet Dispatch sheets Dispatch get workbook "sheets" toDispatch ; System out println "sheet数量:"+Dispatch get sheets "count" toString ; 根据索引得到sheet Dispatch sheet Dispatch invoke sheets "Item" Dispatch Get new Object[]{i} new int[1] toDispatch ; 绘图 Dispatch chartobjects Dispatch call sheet "ChartObjects" toDispatch ; Dispatch achart Dispatch invoke chartobjects "Add" Dispatch Method new Object[] { new Double 12 yHeight 横坐标 纵坐标 new Double 943 new Double 293 宽度 高度 } new int[1] toDispatch ; Dispatch chart Dispatch get achart "chart" toDispatch ; Dispatch range Dispatch invoke sheet "Range" Dispatch Get new Object[] {rangeX rangeY} 列名位置 数据位置 new int[2] toDispatch ; Dispatch series Dispatch call chart "seriescollection" toDispatch ; Dispatch call series "add" range new Variant true ; 生成图形 Dispatch put chart "PlotBy" "2" ; 纵向读取数据绘制chart 设置线条气泡颜色 黄色 Dispatch seriesChartFillFormat Dispatch get series "Fill" toDispatch ; Dispatch seriesForeColor Dispatch get seriesChartFillFormat "ForeColor" toDispatch ; Dispatch put seriesForeColor "SchemeColor" "18" ; 设置绘图区背景色为透明 Dispatch plotArea Dispatch get chart "PlotArea" toDispatch ; Dispatch plotAreaInterior Dispatch get plotArea "Interior" toDispatch ; Dispatch put plotAreaInterior "ColorIndex" " 4142" ; 设置标题 Dispatch chartTitle Dispatch get chart "ChartTitle" toDispatch ; Dispatch put chartTitle "Text" params[paramIndex][0] ; 设置图例位置 Dispatch legend Dispatch get chart "Legend" toDispatch ; Dispatch put legend "Position" " 4160" ; 设置图表背景色 Dispatch chartArea Dispatch get chart "ChartArea" toDispatch ; Dispatch chartFillFormat Dispatch get chartArea "Fill" toDispatch ; Dispatch fc Dispatch get chartFillFormat "ForeColor" toDispatch ; Dispatch put fc "SchemeColor" "35" ; 保存 Dispatch call workbook "Save" ; Dispatch call workbook "Close" new Variant false ; xl invoke "Quit" new Variant[] {} ; }catch Exception ex { ex printStackTrace ; } finally { 释放 com 资源 xl invoke "Quit" new Variant[] {} ; ComThread Release ; System out println " 处理完成 " ; }">一个简单示例 初始化当前Java线程STA ComThread InitSTA ; ActiveXComponent xl new ActiveXComponent "Excel Application" ; try{ 打开现有的Excel xl setProperty "Visible" new Variant false ; Dispatch workbooks xl getProperty "Workbooks" toDis [更多]
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页