怎么把cxgrid数据导到exce

ttily77 2005-11-18 02:48:38
怎么把cxgrid数据导到exce
...全文
292 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaocuo_zrf 2005-11-18
  • 打赏
  • 举报
回复
xixihahahehe(嘻嘻哈哈) ( ) 信誉:100 正解
uses

cxExportGrid4Link


//------导出函数
ExportGrid4ToExcel(lFielName, cxGrid1, True, True, True);

xixihahahehe 2005-11-18
  • 打赏
  • 举报
回复
cxGrid不是有自带的导出函数么?

Unit cxExportGrid4Link

Declaration :
procedure ExportGrid4ToExcel(const AFileName: string; AGrid: TcxGrid; AExpand: Boolean = True; ASaveAll: Boolean = True; AUseNativeFormat: Boolean = False);
konhon 2005-11-18
  • 打赏
  • 举报
回复
轉:

写了一个将 DBGrid 到处到 Excel 的过程,功能简单,但也实用。

procedure ExportDBGridToExcel(Grid: TDBGrid; DisableScreenUpdating: Boolean);
const
CLASS_ExcelApplication: TGUID = '{00024500-0000-0000-C000-000000000046}';

var
ExcelApp: OleVariant;
Unknown: IUnknown;
Bm: TBookmarkStr;
Col, Row: Integer;
I: Integer;
begin
if (Grid.DataSource <> nil) and (Grid.DataSource.DataSet <> nil) then
with Grid.DataSource.DataSet do
begin
try
if not Succeeded(GetActiveObject(CLASS_ExcelApplication, nil, Unknown)) then
Unknown := CreateComObject(CLASS_ExcelApplication);
except
raise Exception.Create('不能启动 Microsoft Excel,请确认 Microsoft Excel 已正确安装在本机上');
end;
ExcelApp := Unknown as IDispatch;
ExcelApp.Visible := True;
ExcelApp.Workbooks.Add;
if DisableScreenUpdating then
ExcelApp.ScreenUpdating := False;
DisableControls;
try
Bm := Bookmark;
First;
Row := 1;
Col := 1;
for I := 0 to Grid.Columns.Count - 1 do
begin
if Grid.Columns[I].Visible then
ExcelApp.Cells[Row, Col] := Grid.Columns[I].Title.Caption;
Inc(Col);
end;
Inc(Row);
while not EOF do
begin
Col := 1;
for I := 0 to Grid.Columns.Count - 1 do
begin
if Grid.Columns[I].Visible then
ExcelApp.Cells[Row, Col] := Grid.Columns[I].Field.DisplayText;
Inc(Col);
end;
Inc(Row);
Next;
end;
Col := 1;
for I := 0 to Grid.Columns.Count - 1 do
begin
if Grid.Columns[I].Visible then
ExcelApp.Columns[Col].AutoFit;;
Inc(Col);
end;
Bookmark := Bm;
finally
EnableControls;
if not ExcelApp.ScreenUpdating then
ExcelApp.ScreenUpdating := True;
end;
end;
end;


qiuliang
http://qiuliang.mycool.net

5,928

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧