1、我发现不少文章都介绍了如何将数据库中的数据导入到一个Excel文件中。但是如果要将一个已有的Excel文件中的数据导入数据库,应该如何编写代码?也就是Delphi如何调用已存在的Excel文件呢?
2、Delphi7中Servers工具中有ExcelApplication和ExcelOleObject两个控件,请问有何区别?在哪里能找到关于他们的介绍?
非常感谢。
6 个解决方案
#1
请各位解答。谢谢。
#2
分数太少了.
#3
function TxRCFPortTest.GetDataFromExcel(AFileName: String): TDataSet;
const
ExcelDrever = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source = "%s";Extended Properties="Excel 8.0;HDR=YES;IMEX=1;"';
var
rec: _Recordset;
adoc: TADOConnection;
cds: TClientDataSet;
AType: string;
begin
Result := nil ;
cds := TClientDataSet.Create(Self);
adoc := TADOConnection.Create(Self);
try
cds.FieldDefs.Add('I_CODE', ftString, 20) ;
cds.FieldDefs.Add('M_TYPE', ftString, 20) ;
cds.FieldDefs.Add('A_TYPE', ftString, 20) ;
cds.FieldDefs.Add('B_NAME', ftString, 50) ;
cds.FieldDefs.Add('H_COUNT', ftFloat) ;
cds.FieldDefs.Add('H_PRICE', ftFloat) ;
cds.CreateDataSet ;
try
adoc.LoginPrompt := False ;
adoc.ConnectionString := Format(ExcelDrever, [AFileName]);
rec := adoc.Execute('select * from [Sheet1$A1:IV65535]') ;
if rec.RecordCount > 0 then
begin
rec.MoveFirst;
while not rec.EOF do
begin
cds.Append;
cds.FieldByName('I_CODE').Value := rec.Fields['债券代码'].Value;
cds.FieldByName('A_TYPE').Value := rec.Fields['债券类型'].Value;
cds.FieldByName('M_TYPE').Value := rec.Fields['交易市场'].Value;
cds.FieldByName('H_COUNT').Value := rec.Fields['数量'].Value;
cds.FieldByName('B_NAME').Value := rec.Fields['债券名称'].Value;
cds.FieldByName('H_PRICE').Value := rec.Fields['会计价格'].Value;
cds.Post ;
rec.MoveNext;
end;
end;
Result := cds;
except
on E: Exception do
begin
FreeAndNil(cds);
xqMsgBox('从Excel中导入持仓失败,原因为:'+E.Message, '导入');
end;
end ;
finally
FreeAndNil(adoc);
end;
end;
const
ExcelDrever = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source = "%s";Extended Properties="Excel 8.0;HDR=YES;IMEX=1;"';
var
rec: _Recordset;
adoc: TADOConnection;
cds: TClientDataSet;
AType: string;
begin
Result := nil ;
cds := TClientDataSet.Create(Self);
adoc := TADOConnection.Create(Self);
try
cds.FieldDefs.Add('I_CODE', ftString, 20) ;
cds.FieldDefs.Add('M_TYPE', ftString, 20) ;
cds.FieldDefs.Add('A_TYPE', ftString, 20) ;
cds.FieldDefs.Add('B_NAME', ftString, 50) ;
cds.FieldDefs.Add('H_COUNT', ftFloat) ;
cds.FieldDefs.Add('H_PRICE', ftFloat) ;
cds.CreateDataSet ;
try
adoc.LoginPrompt := False ;
adoc.ConnectionString := Format(ExcelDrever, [AFileName]);
rec := adoc.Execute('select * from [Sheet1$A1:IV65535]') ;
if rec.RecordCount > 0 then
begin
rec.MoveFirst;
while not rec.EOF do
begin
cds.Append;
cds.FieldByName('I_CODE').Value := rec.Fields['债券代码'].Value;
cds.FieldByName('A_TYPE').Value := rec.Fields['债券类型'].Value;
cds.FieldByName('M_TYPE').Value := rec.Fields['交易市场'].Value;
cds.FieldByName('H_COUNT').Value := rec.Fields['数量'].Value;
cds.FieldByName('B_NAME').Value := rec.Fields['债券名称'].Value;
cds.FieldByName('H_PRICE').Value := rec.Fields['会计价格'].Value;
cds.Post ;
rec.MoveNext;
end;
end;
Result := cds;
except
on E: Exception do
begin
FreeAndNil(cds);
xqMsgBox('从Excel中导入持仓失败,原因为:'+E.Message, '导入');
end;
end ;
finally
FreeAndNil(adoc);
end;
end;
#4
楼主,还不给分!!!
#5
http://community.csdn.net/Expert/topic/3750/3750785.xml?temp=.6158869
#6
非常感谢你,xiaonan(我爱罗) 。
高手啊,佩服佩服。
高手啊,佩服佩服。
#1
请各位解答。谢谢。
#2
分数太少了.
#3
function TxRCFPortTest.GetDataFromExcel(AFileName: String): TDataSet;
const
ExcelDrever = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source = "%s";Extended Properties="Excel 8.0;HDR=YES;IMEX=1;"';
var
rec: _Recordset;
adoc: TADOConnection;
cds: TClientDataSet;
AType: string;
begin
Result := nil ;
cds := TClientDataSet.Create(Self);
adoc := TADOConnection.Create(Self);
try
cds.FieldDefs.Add('I_CODE', ftString, 20) ;
cds.FieldDefs.Add('M_TYPE', ftString, 20) ;
cds.FieldDefs.Add('A_TYPE', ftString, 20) ;
cds.FieldDefs.Add('B_NAME', ftString, 50) ;
cds.FieldDefs.Add('H_COUNT', ftFloat) ;
cds.FieldDefs.Add('H_PRICE', ftFloat) ;
cds.CreateDataSet ;
try
adoc.LoginPrompt := False ;
adoc.ConnectionString := Format(ExcelDrever, [AFileName]);
rec := adoc.Execute('select * from [Sheet1$A1:IV65535]') ;
if rec.RecordCount > 0 then
begin
rec.MoveFirst;
while not rec.EOF do
begin
cds.Append;
cds.FieldByName('I_CODE').Value := rec.Fields['债券代码'].Value;
cds.FieldByName('A_TYPE').Value := rec.Fields['债券类型'].Value;
cds.FieldByName('M_TYPE').Value := rec.Fields['交易市场'].Value;
cds.FieldByName('H_COUNT').Value := rec.Fields['数量'].Value;
cds.FieldByName('B_NAME').Value := rec.Fields['债券名称'].Value;
cds.FieldByName('H_PRICE').Value := rec.Fields['会计价格'].Value;
cds.Post ;
rec.MoveNext;
end;
end;
Result := cds;
except
on E: Exception do
begin
FreeAndNil(cds);
xqMsgBox('从Excel中导入持仓失败,原因为:'+E.Message, '导入');
end;
end ;
finally
FreeAndNil(adoc);
end;
end;
const
ExcelDrever = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source = "%s";Extended Properties="Excel 8.0;HDR=YES;IMEX=1;"';
var
rec: _Recordset;
adoc: TADOConnection;
cds: TClientDataSet;
AType: string;
begin
Result := nil ;
cds := TClientDataSet.Create(Self);
adoc := TADOConnection.Create(Self);
try
cds.FieldDefs.Add('I_CODE', ftString, 20) ;
cds.FieldDefs.Add('M_TYPE', ftString, 20) ;
cds.FieldDefs.Add('A_TYPE', ftString, 20) ;
cds.FieldDefs.Add('B_NAME', ftString, 50) ;
cds.FieldDefs.Add('H_COUNT', ftFloat) ;
cds.FieldDefs.Add('H_PRICE', ftFloat) ;
cds.CreateDataSet ;
try
adoc.LoginPrompt := False ;
adoc.ConnectionString := Format(ExcelDrever, [AFileName]);
rec := adoc.Execute('select * from [Sheet1$A1:IV65535]') ;
if rec.RecordCount > 0 then
begin
rec.MoveFirst;
while not rec.EOF do
begin
cds.Append;
cds.FieldByName('I_CODE').Value := rec.Fields['债券代码'].Value;
cds.FieldByName('A_TYPE').Value := rec.Fields['债券类型'].Value;
cds.FieldByName('M_TYPE').Value := rec.Fields['交易市场'].Value;
cds.FieldByName('H_COUNT').Value := rec.Fields['数量'].Value;
cds.FieldByName('B_NAME').Value := rec.Fields['债券名称'].Value;
cds.FieldByName('H_PRICE').Value := rec.Fields['会计价格'].Value;
cds.Post ;
rec.MoveNext;
end;
end;
Result := cds;
except
on E: Exception do
begin
FreeAndNil(cds);
xqMsgBox('从Excel中导入持仓失败,原因为:'+E.Message, '导入');
end;
end ;
finally
FreeAndNil(adoc);
end;
end;
#4
楼主,还不给分!!!
#5
http://community.csdn.net/Expert/topic/3750/3750785.xml?temp=.6158869
#6
非常感谢你,xiaonan(我爱罗) 。
高手啊,佩服佩服。
高手啊,佩服佩服。