关于做数据库的技巧,再贴一次,马上送分! (200分)

  • 主题发起人 主题发起人 wishme
  • 开始时间 开始时间
W

wishme

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样把查询出来的数据集转到EXCEL中?
我想把DBgrid上显示的我查询出来的结果转到EXCEL表
中,应该是有这样的控件,能否提供?或告知哪里有下
载?

 
去delphi窑洞看看吧
 
http://www.torry.net/gridsother.htm
 
根本不用控件,直接用ADO再加几行代码就搞定了。
 
示例中,dbgrid(DBGrid1)具有一个弹出菜单,它给出两个选项:"Send to Excel" 和 "Copy".
下面给出用到的方法:
//注意:下面的方法必须包含 ComObj, Excel97 单元
//-----------------------------------------------------------
// if toExcel = false, export dbgrid contents to the Clipboard
// if toExcel = true, export dbgrid to Microsoft Excel
procedure ExportDBGrid(toExcel: Boolean);
var
bm: TBookmark;
col, row: Integer;
sline: String;
mem: TMemo;
ExcelApp: Variant;
begin
Screen.Cursor := crHourglass;
DBGrid1.DataSource.DataSet.DisableControls;
bm := DBGrid1.DataSource.DataSet.GetBookmark;
DBGrid1.DataSource.DataSet.First;

// create the Excel object
if toExcel then
begin
ExcelApp := CreateOleObject('Excel.Application');
ExcelApp.WorkBooks.Add(xlWBatWorkSheet);
ExcelApp.WorkBooks[1].WorkSheets[1].Name := 'Grid Data';
end;

// First we send the data to a memo
// works faster than doing it directly to Excel
mem := TMemo.Create(Self);
mem.Visible := false;
mem.Parent := MainForm;
mem.Clear;
sline := '';

// add the info for the column names
for col := 0 to DBGrid1.FieldCount-1 do
sline := sline + DBGrid1.Fields[col].DisplayLabel + #9;
mem.Lines.Add(sline);

// get the data into the memo
for row := 0 to DBGrid1.DataSource.DataSet.RecordCount-1 do
begin
sline := '';
for col := 0 to DBGrid1.FieldCount-1 do
sline := sline + DBGrid1.Fields[col].AsString + #9;
mem.Lines.Add(sline);
DBGrid1.DataSource.DataSet.Next;
end;

// we copy the data to the clipboard
mem.SelectAll;
mem.CopyToClipboard;

// if needed, send it to Excel
// if not, we already have it in the clipboard
if toExcel then
begin
ExcelApp.Workbooks[1].WorkSheets['Grid Data'].Paste;
ExcelApp.Visible := true;
end;

FreeAndNil(mem);
// FreeAndNil(ExcelApp);
DBGrid1.DataSource.DataSet.GotoBookmark(bm);
DBGrid1.DataSource.DataSet.FreeBookmark(bm);
DBGrid1.DataSource.DataSet.EnableControls;
Screen.Cursor := crDefault;
end;
 
就用Server控件页的ExcelApplication就行了.
下面是相关的贴子,自己看看吧.
759711,743162,734554,707641,701040,691080太多了.足够你用了.
 
找本李维的ADO的书
 
看一下《delphi深度历险》
 
用剪贴板方式速度最快!
uses clipbrd, comobj


var
ExcelApp,Sheet : Variant;
i : Integer;
sData : string;
DataSet : TDataSet;
begin
if not adoquery1.Active then
begin
application.MessageBox(pchar('数据集尚未打开!'),pchar(application.title),mb_ok+mb_iconhand);
abort;
end;
try
ExcelApp := CreateOleObject('Excel.AppliCation');
except
application.MessageBox(pchar('本机没有安装EXCEL!'),pchar(application.title),mb_ok+mb_iconhand);
abort;
end;
DataSet := DBGrid1.DataSource.DataSet;
if DataSet.IsEmpty then Exit;
DataSet.DisableControls;
gauge1.Visible:=true; //放一个进度条,先设置为不可见,在此处显示
Gauge1.MaxValue:=dataset.RecordCount;
sData := '';
for i := 0 to DBGrid1.Columns.Count - 1 do {标题}
if DBGrid1.Columns.Items.Visible then
sData := sData + DBGrid1.Columns.Items.Title.Caption + Char(9);
sData := sData + char(13);
DataSet.First;
while not(DataSet.eof) do {数据}
begin
for i := 0 to DataSet.FieldCount - 1 do
if DBGrid1.Columns.Items.Visible then
sData := sData + DataSet.Fields.AsString + char(9);
sData := sData + char(13);
DataSet.Next;
Gauge1.Progress:=dataset.RecNo;
end;

ClipBoard.Clear;
ClipBoard.Open;
ClipBoard.asText := sData;
ClipBoard.Close;
ExcelApp.Visible := True;
ExcelApp.Caption:='××管理系统查询结果';
ExcelApp.WorkBooks.Add(-4167);
Sheet := ExcelApp.WorkBooks[1].WorkSheets[1];
sheet.Cells.NumberFormatLocal := '@';
Sheet.Paste;
dataset.EnableControls;
gauge1.Visible:=false;
 
老兄,自己写代码来的快些。
 
有一个方法,无需用到其他控件
var
i:integer;
str:String;
StrList:TStringList;//用于存储数据的字符列表
begin
try
table1.Active:=true;
with table1 do
begin
First;
DisableControls;
while not EOF do
begin
str:='';
for i:=0 to FieldCount-1 do
str:=str+Fields.AsString+#9;
StrList.Add(str);
// strlist.Sort;
Next;
end;
StrList.SaveToFile('c:/a.xls');
EnableControls;
end;
finally
StrList.free;
end;
这样的优缺点:
1、导出数据无需Excel。
2、导出速度较快。
3、读取速度较慢。(Excel要转化数据格式)
 
后退
顶部