请问要怎么样才能把dbgrid中的数据导入到excel表中呢?
谢谢各位,能帮我一下吗?
谢谢各位,能帮我一下吗?
解决方案 »
- 关于UDP使用过程中的一个问题处理
- 问一个绝对简单的打印问题
- 求助!nasm编译出来的bin文件怎么用?
- 最后80分了:“通”的Unicode值是901A,如果我用vacomm1.writetext(#$90#$1A),则正常,用vacomm1.writetext('#$90#$1A')则是乱码
- 在Delphi 7 怎样做一个飞屏窗口(快闪窗口,用来启动应用程序前显示版本信息的窗口)
- 请问为什么我把xpmenu用上后,别的控件是xp风格,可就是菜单不能变呢?
- 怎样读取RES中的SWF文件,然后用Flash控件播放,请指点!
- 急求Delphi5下载
- 请教: 如果想取到小数的整数位数,用哪个function?
- 请教下各位前辈 学习delphi要注意什么 要看哪些资料
- 读取文本文件的问题,
- 关于Active Document的问题?
你可以读取单元格的值,然后给adoTable的值
var
str:string;
i:Integer;
excelapp,sheet:Variant;
begin
// lbl2.Caption:=DateTimeToStr(Now);
str:='';
dbgrd1.DataSource.DataSet.DisableControls;
for i:=0 to dbgrd1.DataSource.DataSet.FieldCount-1 do
str:=str+dbgrd1.DataSource.DataSet.fields[i].DisplayLabel+char(9);
str:=str+#13;
dbgrd1.DataSource.DataSet.First;
while not(dbgrd1.DataSource.DataSet.eof) do begin
for i:=0 to dbgrd1.DataSource.DataSet.FieldCount-1 do
str:=str+dbgrd1.DataSource.DataSet.Fields[i].AsString+char(9);
str:=str+#13;
dbgrd1.DataSource.DataSet.next; lbl1.Caption:=IntToStr(dbgrd1.DataSource.DataSet.RecNo);
Application.ProcessMessages;
end;//end while dbgrd1.DataSource.DataSet.EnableControls; clipboard.Clear;
Clipboard.Open;
Clipboard.AsText:=str;
Clipboard.Close;
excelapp:=createoleobject('excel.application');
excelapp.workbooks.add(1);
sheet:=excelapp.workbooks[1].worksheets[1];
sheet.name:='sheet1';
sheet.paste;
Clipboard.Clear;
excelapp.visible:=true;end;//---Code From CSDN
unit ExcelButton;
{$R TExcelButton.dcr}interfaceuses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Buttons,
DB, OleServer, Excel2000;type
TExcelButton = class(TBitBtn)
private
fSheetName,fFilePath : string;
fDataSource : TDataSource;
protected
procedure SetDataSource(Value : TDataSource);
procedure ExportToExcel(SheetName : string;FilePath : string);
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer); override;
public
{ Public declarations }
procedure ExportFile;
published
property SheetName : string read fSheetName write fSheetName;
property FilePath : string read fFilePath write fFilePath;
property DataSource : TDataSource read fDataSource write SetDataSource;
end;procedure Register;implementationprocedure Register;
begin
RegisterComponents('Additional', [TExcelButton]);
end;procedure TExcelButton.SetDataSource(Value : TDataSource);
begin
fDataSource := Value;
end;procedure TExcelButton.ExportFile;
begin
ExportToExcel(fSheetName,fFilePath);
end;procedure TExcelButton.MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer);
begin
inherited;
ExportToExcel(fSheetName,fFilePath);
end;procedure TExcelButton.ExportToExcel(SheetName : string; FilePath : string);
var
col, row: Integer;
WorkBk : _WorkBook; //Define a WorkBook
WorkSheet : _WorkSheet; //Define a WorkSheet
IIndex : OleVariant;
ExcelApp : TExcelApplication;
begin
if (fDataSource.DataSet.Active) AND
(SheetName <> '') AND
(FilePath <> '') then
begin IIndex := 1;
Try
ExcelApp := TExcelApplication.Create(Self); // create the Excel object
ExcelApp.Connect;
ExcelApp.WorkBooks.Add(xlWBatWorkSheet,0); // Select the first WorkBook
WorkBk := ExcelApp.WorkBooks.Item[IIndex]; // Define the first WorkSheet
WorkSheet := WorkBk.WorkSheets.Get_Item(1) as _WorkSheet; //WorkSheet.Name := SheetName;
Worksheet.Columns.HorizontalAlignment := xlLeft;
WorkSheet.Columns.ColumnWidth := 14;
WorkSheet.Columns.RowHeight := 16;
// add the info for the column names
for col := 1 to fDataSource.DataSet.FieldCount do
WorkSheet.Cells.Item[1,col] := fDataSource.DataSet.Fields[col-1].DisplayLabel; // get the data into the memo
with fDataSource do
begin
DataSet.First;
row := 2;
while not DataSet.Eof do
begin
for col := 1 to DataSet.FieldCount do
begin
WorkSheet.Cells.Item[row,col] := DataSet.Fields[col-1].AsString;
end;
INC(row,1);
DataSet.Next;
end;
end; ExcelApp.DisplayAlerts[0] := False;
WorkBk.SaveCopyAs(FilePath,0);
ExcelApp.Workbooks.Close(0);
ExcelApp.Quit;
Finally
ExcelApp.Free;
end;
end;
end;end.
procedure TForm1.Button1Click(Sender: TObject);
var
ls_str,ls_tab :string;
i,j : integer;
begin
SaveDialog1.FileName:='文件名';
if SaveDialog1.Execute then
begin
ls_tab:=#9;
dbgrid1.DataSource.DataSet.First;
memo1.clear;
memo1.Lines.add(文件名);
for i:=0 to dbgrid1.Columns.Count-1 do
begin
ls_str:= ls_str+dbgrid1.Columns[i].Title.Caption+ls_tab;
end;
memo1.Lines.add(ls_str);
while not dbgrid1.DataSource.DataSet.Eof do
begin
ls_str:='';
for i:=0 to dbgrid1.Columns.Count-1 do
begin
ls_str:= ls_str+dbgrid1.Columns[i].field.asstring+ls_tab;
end;
memo1.Lines.add(ls_str);
dbgrid1.DataSource.DataSet.next;
end;
memo1.Lines.SaveToFile(SaveDialog1.FileName+'.XLS');
end;
application.MessageBox('导出成功,请用EXCEL打开文件!','提示',64);end;