将stringgrid传入excelprocedure Tdm.CreateExcel(Sender:Tobject;ATitle:String); var RowCount,iCounter:integer; i,j:integer; Excelapp:OleVariant; begin Excelapp:=CreateOLEObject('excel.Application'); Excelapp.Visible := true ; ;//ok! if (sender is TstringGrid) then begin RowCount:=(sender as TStringGrid).RowCount;; icounter:=(sender as TStringGrid).ColCount;; Excelapp.workbooks.add ; Excelapp.Cells(1,1):=ATitle; for i:=1 to RowCount do begin for j:=1 to iCounter do begin Excelapp.Cells(i+1,j):=(sender as TStringGrid).Cells[j-1,i-1]; end; end; end; end;
给你个大概 procedure TF_Ypzbwh.N_DcClick(Sender: TObject); var eclApp:Variant; xlsFileName:String; i,j:Integer; begin if not SD_ExcelName.Execute then Exit; XlsFileName:=SD_ExcelName.FileName; if Trim(XlsFileName)='' then Exit; if FileExists(XlsFileName) then DeleteFile(XlsFileName); Try StartWait('正在将数据另存为Excel文件,请稍候...'); eclApp:=CreateOleObject('Excel.Application'); eclApp.WorkBooks.Add; Except MessageBox(self.Handle,'你的机器里未安装MicroSoft Excel!','错误',MB_ICONERROR); StopWait; Exit; End; Try //MessageBox(self.Handle,'下面演示新建一个XLS文件,并写入数据,最后关闭它!','信息',MB_ICONINFORMATION); With DM.Qcomm do begin Close; SQL.Clear; SQL.Add('select * from v_YpxxZw '); Open; for i:=1 to FieldCount do begin eclApp.WorkBooks[1].WorkSheets[1].Cells(1,i):=Fields.Fields[i-1].FieldName; eclApp.WorkBooks[1].WorkSheets[1].Columns[i].Font.Name:='宋体'; eclApp.WorkBooks[1].WorkSheets[1].Columns[i].Font.Size:=10; eclApp.WorkBooks[1].WorkSheets[1].Columns[i].NumberFormatLocal:='@'; eclApp.WorkBooks[1].WorkSheets[1].Columns[i].HorizontalAlignment := xlCenter; end; for j:=2 to RecordCount+1 do begin for i:=1 to FieldCount do begin eclApp.WorkBooks[1].WorkSheets[1].Cells(j,i):=Fields.Fields[i-1].AsString; WaitProgress(Round((((j-2)*FieldCount+i)*100)/(RecordCount*FieldCount))); end; Next; end; end; eclApp.WorkBooks[1].SaveAs(xlsFileName); eclApp.WorkBooks[1].Close; eclApp.Quit; eclApp:=UnAssigned; StopWait; WinExec(PChar(xlsFileName),SW_SHOWNORMAL); Except MessageBox(self.Handle,'不能正确操作Excel文件。可能是该文件已被其它程序打开,或系统错误!','错误',MB_ICONERROR); eclApp.WorkBooks[1].Close; eclApp.Quit; eclApp:=UnAssigned; StopWait; End; end;
其实远在天边,近在眼前。如果你的电脑中安装了EXCEL, 那么请搜索文件'VBAXL9',找到后打开这个文件,你想要的一切都在里面。
var
RowCount,iCounter:integer;
i,j:integer;
Excelapp:OleVariant;
begin
Excelapp:=CreateOLEObject('excel.Application');
Excelapp.Visible := true ; ;//ok!
if (sender is TstringGrid) then
begin
RowCount:=(sender as TStringGrid).RowCount;;
icounter:=(sender as TStringGrid).ColCount;;
Excelapp.workbooks.add ;
Excelapp.Cells(1,1):=ATitle;
for i:=1 to RowCount do
begin
for j:=1 to iCounter do
begin
Excelapp.Cells(i+1,j):=(sender as TStringGrid).Cells[j-1,i-1];
end;
end;
end;
end;
OFFICE2000版本,全部安装都没有
不会啊,它是随Office 2000安装的帮助文件。 这个文件的全名是 "vbaxl9.chm"。里面有Excel中所有的对象、属性、方法的说明和使用详解。
procedure TF_Ypzbwh.N_DcClick(Sender: TObject);
var eclApp:Variant;
xlsFileName:String;
i,j:Integer;
begin
if not SD_ExcelName.Execute then Exit;
XlsFileName:=SD_ExcelName.FileName;
if Trim(XlsFileName)='' then Exit;
if FileExists(XlsFileName) then
DeleteFile(XlsFileName);
Try
StartWait('正在将数据另存为Excel文件,请稍候...');
eclApp:=CreateOleObject('Excel.Application');
eclApp.WorkBooks.Add;
Except
MessageBox(self.Handle,'你的机器里未安装MicroSoft Excel!','错误',MB_ICONERROR);
StopWait;
Exit;
End;
Try
//MessageBox(self.Handle,'下面演示新建一个XLS文件,并写入数据,最后关闭它!','信息',MB_ICONINFORMATION);
With DM.Qcomm do begin
Close;
SQL.Clear;
SQL.Add('select * from v_YpxxZw ');
Open;
for i:=1 to FieldCount do begin
eclApp.WorkBooks[1].WorkSheets[1].Cells(1,i):=Fields.Fields[i-1].FieldName;
eclApp.WorkBooks[1].WorkSheets[1].Columns[i].Font.Name:='宋体';
eclApp.WorkBooks[1].WorkSheets[1].Columns[i].Font.Size:=10;
eclApp.WorkBooks[1].WorkSheets[1].Columns[i].NumberFormatLocal:='@';
eclApp.WorkBooks[1].WorkSheets[1].Columns[i].HorizontalAlignment := xlCenter;
end;
for j:=2 to RecordCount+1 do begin
for i:=1 to FieldCount do begin
eclApp.WorkBooks[1].WorkSheets[1].Cells(j,i):=Fields.Fields[i-1].AsString;
WaitProgress(Round((((j-2)*FieldCount+i)*100)/(RecordCount*FieldCount)));
end;
Next;
end;
end;
eclApp.WorkBooks[1].SaveAs(xlsFileName);
eclApp.WorkBooks[1].Close;
eclApp.Quit;
eclApp:=UnAssigned;
StopWait;
WinExec(PChar(xlsFileName),SW_SHOWNORMAL);
Except
MessageBox(self.Handle,'不能正确操作Excel文件。可能是该文件已被其它程序打开,或系统错误!','错误',MB_ICONERROR);
eclApp.WorkBooks[1].Close;
eclApp.Quit;
eclApp:=UnAssigned;
StopWait;
End;
end;
中 ,delphi 编译时说没有定义 xlCenter 变量