紧急!!如何获取当前EXCEL中的内容!(200分)

  • 主题发起人 主题发起人 明月几时有
  • 开始时间 开始时间

明月几时有

Unregistered / Unconfirmed
GUEST, unregistred user!
我做了EXCEL的COM ADDINS即COM加载项,里面封装了一个窗体,我在这个窗体里面想调用当前EXCEL中的数据,然后将调用的数据导入到Listview控件中去,请问应该怎么样调用(比如说我要调用其中A列或者B列的数据)?我以前用VBA做的时候是用的Activeworksheet这一类的属性。(我的COM加载项是用VB做的,所以最好是VB的解决方案,但是DELPHI的也可以,我仿照也能做的出来)
 
现在最主要的问题就是我不知道怎么调用当前EXCEL中的数据~
 
急急急,请各位大侠帮忙啊!~
 
是不是读excel数据吗?
 
http://delphibbs.com/keylife/iblog_show.asp?xid=19564
 
我最近在写Excel的COM Addin 看你这样急 代码懒得全贴出来 给你几个代码的片断
前提: 你把导入部分已经做完并理解
uses
Excel2000

v_Sheet: ExcelWorksheet;

v_Sheet := g_App.ActiveSheet as ExcelWorksheet;

v_Name := f_GetColums(v_CountC) + IntToStr(v_CountR); //组合Cell的位置值 如: A1
v_Value := VarToStr(v_Sheet.Range[v_Name, EmptyParam].Value); //获取这个Cell下的数据
 
TO sbzldlb
你的那个网址我打不开,能直接贴上来么?
 
TO Avalon
谢谢热心帮助,导入部分是我以前就做好了的,但解决不了怎么读取当前EXCEL数据的功能,现在正在试你刚说的办法,就是声明一个Excel对象的ActiveSheet是吧,再次感谢!!!
 
{---------------------------------------------------}
{ 操作EXCEL }
{ 作者:SUBIN 时间:2006.01.04 }
{---------------------------------------------------}

unit UExcel;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,ComObj,DB, ADODB,IniFiles ;

type
TBaseExcel=Class
private
FSheetTitle: String;
FSheetIndex: Integer;
FFileName : String;
FExcelApp: Variant;
FExcelSheet: Variant;
procedure SetSheetTitle(const Value: String);
procedure SetSheetIndex(const Value: Integer);
function GetColColunt: Integer;
function GetRowCount: Integer;
function GetSheetCount: Integer;
procedure SetFileName(const Value: String);
procedure SetExcelApp(const Value: Variant);
procedure SetExcelSheet(const Value: Variant);
protected
function InitExcel:Boolean;virtual;
function FinallyExcel:Boolean;virtual;
function GetCellValue(Row,Col:Integer):String;
procedure SetCellValue(Row,Col:Integer;Value:String);
public
property SheetTitle:String read FSheetTitle write SetSheetTitle;
property SheetIndex:Integer read FSheetIndex write SetSheetIndex;
property FileName:String read FFileName write SetFileName;
property ExcelApp : Variant read FExcelApp write SetExcelApp;
property ExcelSheet : Variant read FExcelSheet write SetExcelSheet;
property ColCount:Integer read GetColColunt;
property RowCount:Integer read GetRowCount;
property SheetCount:Integer read GetSheetCount;
end;


type
TFieldInfo=Record
FieldName:String[20];
FieldIndex:Integer;
end;

TExcelToDataBase=Class(TBaseExcel)
private
FTabelName: String;
function GetFieldCount: Integer;
function GetFieldName:Boolean;
function GetFieldIndex:Boolean;
procedure SetTableName(const Value: String);
protected
FieldInfo:Array of TFieldInfo;
property ColCount;
property RowCount;
property ExcelApp;
property ExcelSheet;
property SheetTitle;
property FieldCount : Integer read GetFieldCount;
property TabelName : String read FTabelName write SetTableName;
public
function InitExcel:Boolean;overload;
function FinallyExcel:Boolean;overload;
function ExcelToDataBase(SheetTitle,TabelName:String):Boolean;
property FileName;
end;
implementation

uses GlobalData;

{ TBaseExcel }
function TBaseExcel.InitExcel: Boolean;
begin
Result:=False;
try
ExcelApp := CreateOleObject( 'Excel.Application' );
except
exit;
end;

if not FileExists(FileName) then Exit;
ExcelApp.Visible := false;
try
ExcelApp.WorkBooks.Open(FileName);
except
exit;
end;
Result:=True;
end;

function TBaseExcel.FinallyExcel: Boolean;
begin
Result:=False;
try
ExcelApp.WorkBooks.Close;
ExcelApp:=UnAssigned;
ExcelSheet:=UnAssigned;
Result:=True;
except
end;
end;

function TBaseExcel.GetCellValue(Row, Col: Integer): String;
begin
Result:=ExcelSheet.Cells[Row,Col].Value;
end;

function TBaseExcel.GetColColunt: Integer;
begin
Result:=ExcelSheet.UsedRange.columns.Count;
end;

function TBaseExcel.GetRowCount: Integer;
begin
Result:=ExcelSheet.UsedRange.Rows.Count;
end;

function TBaseExcel.GetSheetCount: Integer;
begin
Result:=ExcelApp.WorkSheets.Count;
end;

procedure TBaseExcel.SetCellValue(Row, Col: Integer; Value: String);
begin
ExcelSheet.Cells[Row,Col].Value:=Value;
end;

procedure TBaseExcel.SetExcelApp(const Value: Variant);
begin
FExcelApp := Value;
end;

procedure TBaseExcel.SetExcelSheet(const Value: Variant);
begin
FExcelSheet := Value;
end;

procedure TBaseExcel.SetFileName(const Value: String);
begin
FFileName := Value;
end;

procedure TBaseExcel.SetSheetIndex(const Value: Integer);
begin
FSheetIndex := Value;
end;

procedure TBaseExcel.SetSheetTitle(const Value: String);
begin
FSheetTitle := Value;
end;

{ TExcelToDataBase }

function TExcelToDataBase.FinallyExcel: Boolean;
begin
Result:=inherited FinallyExcel;
end;

function TExcelToDataBase.InitExcel: Boolean;
begin
Result:=inherited InitExcel;
end;

function TExcelToDataBase.GetFieldCount: Integer;
var
IniFile:TIniFile;
begin
IniFile:=TIniFile.Create(GetCurrentDir+'/'+'FieldConfig.ini');
try
Result:= IniFile.ReadInteger(TabelName,'fieldCount',0);
finally
IniFile.Free;
end;
end;

function TExcelToDataBase.ExcelToDataBase(SheetTitle,
TabelName: String): Boolean;
var
I,J:Integer;
CmdQuery:TAdoQuery;
begin
Result:=False;
Self.TabelName:=TabelName;
Self.SheetTitle:=SheetTitle;
if FieldCount>0 then
begin
{Select Excel ActiveSheet}
ExcelApp.WorkSheets[Self.SheetTitle].Activate;
ExcelSheet:=ExcelApp.ActiveSheet;
{Init FieldInfo}
SetLength(FieldInfo,FieldCount);
if not GetFieldName then Exit;
if not GetFieldIndex then Exit;

try
CmdQuery:=TAdoQuery.Create(Nil);
CmdQuery.Close;
CmdQuery.Connection:=ADOConnection;
CmdQuery.SQL.Text:='Select * From '+TabelName;
CmdQuery.Open;

for I:=2 to RowCount do
begin
CmdQuery.Insert;
for J:=0 to FieldCount-1 do
begin
if FieldInfo[j].FieldIndex>=0 then
begin
CmdQuery.FieldByName(FieldInfo[j].FieldName).AsString:=ExcelSheet.Cells[i,FieldInfo[j].FieldIndex].value;
end;
end;
CmdQuery.Post;
end;
Result:=True;
finally
CmdQuery.Free;
end;
end;
end;

function TExcelToDataBase.GetFieldIndex: Boolean;
var
I:Integer;
J:Integer;
begin
Result:=False;
for I:=1 to ColCount do
begin
for J:=0 to FieldCount-1 do
begin
if ExcelSheet.Cells[1,i].value=FieldInfo[j].FieldName then
begin
FieldInfo[j].FieldIndex:=I;
end;
end;
end;
Result:=True;
end;

function TExcelToDataBase.GetFieldName: Boolean;
var
IniFile:TIniFile;
I:Integer;
begin
Result:=False;
IniFile:=TIniFile.Create(GetCurrentDir+'/'+'FieldConfig.ini');
try
for i:=1 to FieldCount do
begin
FieldInfo[i-1].FieldName:=IniFile.ReadString(TabelName,'FieldName'+IntToStr(i),'');
FieldInfo[i-1].FieldIndex:=-1;
end;
Result:=True;
finally
IniFile.Free;
end;
end;

procedure TExcelToDataBase.SetTableName(const Value: String);
begin
FTabelName := Value;
end;

end.
 
g_App: ExcelApplication;

g_App := ExcelApplication(p_Application);
 
把excel作为一个数据库来处理。或者你就按cell中的内容读取
 
总算找到一个delphi写COM Addin的知音了 呵呵
原来我也打算用VB写但是毕竟写了那么多年的delphi
所以还是用Delphi了 尽管实现麻烦一些
 
帮顶!

http://www.source520.com

站长开发推广同盟 站长朋友的终极驿站
同时拥有海量源码电子经典书籍下载

http://www.source520.com/search/search.asp

"编程.站长"论坛搜索引擎-----为中国站长注入动力!
 
to sbzldlb:
你的方法关闭excel 不彻底!
任务管理器里还可以看到excel!
 
怎么我用Active方法还是得不到当前的EXCEL中的内容啊,不知道我的程序到底是有什么问题啊?

Dim cell As Range

For Each cell In Range(ActiveWindow.RangeSelection.address)
这样为什么还是去不到当前EXCEL的内容?请指教!
 
active 也可以,我的酒是excel.ocx啊!
 
帮顶!

╭=========================================╮

80G海量源代码,控件,书籍全免费狂下不停!

http://www.source520.com

╰=========================================╯
 
帮顶!

╭=========================================╮

80G海量源代码,控件,书籍全免费狂下不停!

http://www.source520.com

╰=========================================╯
 
多人接受答案了。
 
后退
顶部