怎样操作EXCEL文件?(100分)

  • 主题发起人 主题发起人 hewen7509
  • 开始时间 开始时间
H

hewen7509

Unregistered / Unconfirmed
GUEST, unregistred user!
我能够用以下程序新建一个 xls文件,填写数据;可以打开它修改,但无法读出xls的数据;
var
excel1,excelwork1:variant;
filename:string;
begin

filename:='c:/fb.xls';
Excel1:=CreateOleObject('Excel.Application');
excelwork1:=createoleobject('excel.sheet');
Excel1.Workbooks.Add(xlWBatWorkSheet);
excelwork1:=Excel1.Workbooks[1].Worksheets['sheet1'];
excel1.cells(1,1):='测试';
excel1.cells(1,2):='EXCEL';
excel1.cells(2,1):='数据';
excel1.cells(2,2):='转换';
excelwork1.saveas(filename);
excel1.quit;
excel1:=unassigned;
end;
请问,如何能够读出一个xls文件的数据,读出到表格或用变量依次读出;或者用控件也行
 
读excel

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,Trad
eMark,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;
 
use comobj;
 
我读入到一个StringGrid中
var
Excel,Sheet:OleVariant;
filename:string;
I,j:Integer;
begin
if not OpenDialog1.Execute then exit;
FileName:=OpenDialog1.filename;
Excel:=CreateOleObject('Excel.Application');
Excel.WorkBooks.Open(FileName);
Sheet:=Excel.ActiveSheet;
sg.RowCount:=Sheet.UsedRange.Rows.Count;//sg为StringGrid
sg.ColCount:=Sheet.UsedRange.Columns.Count;
for i:=0 to Sg.RowCount-1 do
begin
for j:=0 to sg.ColCount-1 do
begin
sg.Cells[j,i]:=Sheet.UsedRange.Cells[i+1,j+1];
end;
end;
end;
 
powersite 的答案我测试通过了,lcl_003的答案我还没有测试;
不过,我有一个问题不知道大家注意了没有:用comobj或OLE 在一般的情况下,我们可以
运行通过,但一旦装有NORTON的杀毒工具后,会提示:“呼叫服务被终止”。程序无法打开
文件,但不会死机;
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
781
import
I
I
回复
0
查看
618
import
I
I
回复
0
查看
547
import
I
I
回复
0
查看
654
import
I
I
回复
0
查看
610
import
I
后退
顶部