;
帖子
3919 
精华
积分
11710 
鱼币
0 ¥ 
来自
大连 
在线时间
1858 小时 
注册时间
2007-6-11 
最后登录
2009-10-20 

贴图大师勋章新人进步勋章宣传大使勋章论坛元老勋章

3
发表于 2009-1-5 19:48 | 只看该作者
灏 DBGrid 涓殑鍐呭杈撳嚭鑷 Excel 鎴 ClipBoard - 缂栫▼搴 - Powered by SupeSite

浣犵殑浣嶇疆锛缂栫▼搴 >> 璧勮 >> Delphi >> 鎺т欢搴旂敤 >> 璇︾粏鍐呭 鍦ㄧ嚎鎶曠ǹ

灏 DBGrid 涓殑鍐呭杈撳嚭鑷 Excel 鎴 ClipBoard

鍙戝竷: 2008-6-26 17:59 |  浣滆: admin |   鏌ョ湅: 23娆

//娉ㄦ剰:涓嬮潰鐨勬柟娉曞繀椤诲寘鍚 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 .WorkSheets .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 .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;

锛堝嚭澶勶細www

鎵撳嵃 | 鏀惰棌姝ら〉 |  鎺ㄨ崘缁欏ソ鍙 | 涓炬姤
涓婁竴绡 涓嬩竴绡
 

璇勫垎锛0

鍙戣〃璇勮
鏌ョ湅鍏ㄩ儴鍥炲銆愬凡鏈0浣嶇綉鍙嬪彂琛ㄤ簡鐪嬫硶銆