如何读出excel表内容?(20分)

  • 主题发起人 主题发起人 lcl_003
  • 开始时间 开始时间
L

lcl_003

Unregistered / Unconfirmed
GUEST, unregistred user!
如何读出excel表内容?很多问题都是问如何将数据导出到excel的,我刚好相反
我想将一个excel表的内容读出,在程序中要用这些数据,怎么实现?
 
Delphi里不是有一系列的控件吗? 用一用如何 ?
 
控件没有用过
程序如下
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls;

type
TfrmMain = class(TForm)
btnCreateOLE: TButton;
btnFreeOLE: TButton;
btnGetData: TButton;
lstbxSheetData: TListBox;
procedure btnCreateOLEClick(Sender: TObject);
procedure btnFreeOLEClick(Sender: TObject);
procedure btnGetDataClick(Sender: TObject);
private
MsExcel: Variant;
MsExcelWorkBook: Variant;
MsExcelWorkSheet: Variant;
public
{ Public declarations }
end;

var
frmMain: TfrmMain;

implementation

uses ComObj;

{$R *.DFM}

procedure TfrmMain.btnCreateOLEClick(Sender: TObject);
var
i: integer;
begin
try
MsExcel := CreateOleObject('Excel.Application.8');
MsExcelWorkBook := MsExcel.WorkBooks.Add;
MsExcelWorkSheet := MsExcel.WorkSheets.Add;
except
MessageDlg('Can nott creat Excel 97 !', mtWarning, [mbOK], 0);
exit;
end;

MsExcel.Visible := True;
for i := 1 to 10 do
MsExcelWorkSheet.Range['A' + IntToStr(i)].Value := i * i;

try
MsExcelWorkSheet.SaveAs(ExtractFilePath(Application.EXEName) + 'TEMP.XLS');
except
MessageDlg('Can not save as TEMP.XLS!', mtInformation, [mbOK], 0);
end;


end;

procedure TfrmMain.btnFreeOLEClick(Sender: TObject);
begin
MsExcel.Quit;
end;

procedure TfrmMain.btnGetDataClick(Sender: TObject);
var
i: integer;
begin
lstbxSheetData.Items.Clear;
try
for i := 1 to 10 do
lstbxSheetData.Items.Add(MsExcelWorkSheet.Range['A' + IntToStr(i)].Value);

except
MessageDlg('Can not open Excel Sheet!', mtWarning, [mbOK], 0);
end;

end;

end.
 
来晚了,楼上的说的很好.
 
楼上说会很慢!
看我的程序
procedure TF_Main.ToolButtonExcelClick(Sender: TObject);
var
OpenDialog:TOpenDialog;
iCount:integer;
begin
if ShowIYN('是否真的要导入Excel数据,要生的数据将被删除?')<>IDCANCEL then
try
OpenDialog:=TOpenDialog.Create(Self);
OpenDialog.DefaultExt := 'xls';
OpenDialog.Filter := 'Microsoft Excel 文件 (*.xls)|*.xls';
OpenDialog.Execute;
if OpenDialog.FileName<>'' then
try
Screen.Cursor:=crSqlWait;
ADOConExcel.Close;
ADOConExcel.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='
+OpenDialog.FileName+';Extended Properties=Excel 8.0;Persist Security Info=False';
ADOConExcel.Open;
with ADOQryExcel do
begin
Close;
Sql.Clear;
Sql.Add('SELECT 材料编号,百平米用量,预算价,最低价 FROM [Sheet1$]');
Prepared:=true;
Open;
end;
iCount:=ADOQryExcel.RecordCount;
ADOConExcel.BeginTrans;
with F_DataModule.ADOCom do
begin
CommandText:='DELETE FROM MatPriCal WHERE cYearMonth='+''''+FormatDateTime('yyyymm',DateTimePicker.DateTime)+'''';
Execute;
end;
ADOQryExcel.First;
while not ADOQryExcel.Eof do
begin
with F_DataModule.ADOCom do
begin
CommandText:='INSERT INTO MatPriCal VALUES('+''''+FormatDateTime('yyyymm',DateTimePicker.DateTime)+''''+','+''''+Trim(ADOQryExcel.FieldByName('材料编号').AsString)+''''+','+Trim(ADOQryExcel.FieldByName('百平米用量').AsString)+','+Trim(ADOQryExcel.FieldByName('预算价').AsString)+','+Trim(ADOQryExcel.FieldByName('最低价').AsString)+')';
Execute;
end;
ADOQryExcel.Next;
F_Main.StatusBar.Refresh;
F_Main.StatusBar.Panels[2].text:='当前数:'+IntToStr(ADOQryExcel.RecNo)+'(共'+IntToStr(iCount)+'条)';
end;
ADOConExcel.CommitTrans;
ShowWIE(2,'导入成功!');
except
ADOConExcel.RollbackTrans;
ShowWIE(2,'导入失败!');
end;
finally
ADOQryExcel.Close;
ADOConExcel.Close;
OpenDialog.Free;
Screen.Cursor:=crDefault;
F_Main.StatusBar.Panels[2].Text:='';
end;
end;

或用sql 自带的openrowset,参考程序
if ShowIYN('是否真的要导入Excel数据?')<>IDCANCEL then
try
ADOConInfoExcel.Open;
OpenDialog:=TOpenDialog.Create(Self);
OpenDialog.DefaultExt := 'xls';
OpenDialog.Filter := 'Microsoft Excel 文件 (*.xls)|*.xls';
OpenDialog.Execute;
if OpenDialog.FileName<>'' then
try
with ADOQryInfoExcel do
begin
Close;
Sql.Clear;
Sql.Add(' SELECT TOP 1 * FROM OPENROWSET');
Sql.Add('(');
Sql.Add('''MSDASQL.1''');
Sql.Add(',');
Sql.Add('''DRIVER=Microsoft Excel Driver (*.xls);');
Sql.Add('DBQ='+OpenDialog.FileName+'''');
Sql.Add(',');
Sql.Add('''SELECT * FROM [Sheet1$]''');
Sql.Add(')');
Open;
end;
if Copy(ADOQryInfoExcel.Fields[2].AsString,1,4)<>mUser.UserArea then
begin
ShowWIE(1,'不能导入其它地区数据!');
ADOQryInfoExcel.Close;
ADOConInfoExcel.Close;
exit;
end;
//ADOConInfoExcel.BeginTrans;
with ADOComInfoExcel do
begin
CommandText:=' UPDATE C';
CommandText:=CommandText+' SET C.MNo=A.类别编码,C.MName=A.材料名称,C.spec=A.材料规格,C.unit=A.材料单位,C.Factory=A.生产厂家,C.TradeMark=A.厂牌,C.Price=A.材料单价,C.pingyin=A.材料拼音,C.hs=A.换算系数 FROM InfoPrice'+mUser.UserArea;
CommandText:=CommandText+' C, OPENROWSET';
CommandText:=CommandText+'(';
CommandText:=CommandText+'''MSDASQL.1''';
CommandText:=CommandText+',';
CommandText:=CommandText+'''DRIVER=Microsoft Excel Driver (*.xls);';
CommandText:=CommandText+'DBQ='+OpenDialog.FileName+'''';
CommandText:=CommandText+',';
CommandText:=CommandText+'''SELECT * FROM [Sheet1$]''';
CommandText:=CommandText+') A WHERE C.RDate=A.发布日期 AND C.RNO=A.发布编号 AND C.Area=A.地区编码';
Execute;
CommandText:=' INSERT INTO InfoPrice'+mUser.UserArea;
CommandText:=CommandText+'(RDate,RNO,Area,MNo,MName,spec,unit,Factory,TradeMark,Price,pingyin,hs) ';
CommandText:=CommandText+' SELECT 发布日期,发布编号,地区编码,类别编码,材料名称,材料规格,材料单位,生产厂家,厂牌,材料单价,材料拼音,换算系数 FROM OPENROWSET';
CommandText:=CommandText+'(';
CommandText:=CommandText+'''MSDASQL.1''';
CommandText:=CommandText+',';
CommandText:=CommandText+'''DRIVER=Microsoft Excel Driver (*.xls);';
CommandText:=CommandText+'DBQ='+OpenDialog.FileName+'''';
CommandText:=CommandText+',';
CommandText:=CommandText+'''SELECT * FROM [Sheet1$]''';
CommandText:=CommandText+') A';
CommandText:=CommandText+' WHERE NOT EXISTS(SELECT 1 FROM InfoPrice'+mUser.UserArea+' C';
CommandText:=CommandText+' WHERE C.RDate=A.发布日期 AND C.RNO=A.发布编号 AND C.Area=A.地区编码)';
Execute;
end;
//ADOConInfoExcel.CommitTrans;
RefreshGridData;
ShowWIE(2,'导入成功!');
except
//ADOConInfoExcel.RollbackTrans;
ShowWIE(2,'导入失败!');
end;
finally
ADOQryInfoExcel.Close;
ADOConInfoExcel.Close;
OpenDialog.Free;
end;

不过要文件在本地才好做,参考http://www.delphibbs.com/delphibbs/dispq.asp?lid=1291308
 
怎么样,说句话吧!
 
呵呵,还挺着急
 
后退
顶部