在 cxgrid 里的 cxgridpopopmenu 里加入导出功能。

因为使用这个工具很方便,但这个菜单里没有导出选项,觉得是一。。。
于是,加上之。

在cxGridHeaderPopupMenuItems.pas中。第199行开始,加入如下内容,定义菜单项

 1/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 2  TcxGridExpandAllGroup = class(TcxGridHeaderPopupMenuOperation)
 3  protected
 4    procedure Execute(Sender: TObject); override;
 5    function GetEnabled: Boolean; override;
 6    function GetImageResourceName: stringoverride;
 7  public
 8    constructor Create; override;
 9  end;
10
11   TcxGridCollapseAllGroup = class(TcxGridHeaderPopupMenuOperation)
12  protected
13    procedure Execute(Sender: TObject); override;
14    function GetEnabled: Boolean; override;
15  public
16    constructor Create; override;
17  end;
18
19  TcxGridExport = class(TcxGridHeaderPopupMenuOperation)
20  protected
21    procedure Execute(Sender: TObject); override;
22    function GetImageResourceName: stringoverride;
23  public
24    constructor Create; override;
25  end;
26
27  TcxGridPrint = class(TcxGridHeaderPopupMenuOperation)
28  protected
29    procedure Execute(Sender: TObject); override;
30  public
31    constructor Create; override;
32  end;
33
34//////////////////////////////////////////////////////////////////////////////////////////////
35
36


在 procedure TcxGridHeaderPopupMenuOperations.AddItems; 过程中,加入新的菜单项

procedure TcxGridHeaderPopupMenuOperations.AddItems;
begin
  AddItem(TcxGridSortColumnAsc);
   .
  AddItem(TcxGridBestFitAllColumns).BeginGroup :
= True;
  AddItem(TcxGridExpandAllGroup).BeginGroup:
=true;
  AddItem(TcxGridCollapseAllGroup);
  AddItem(TcxGridExport);
  AddItem(TcxGridPrint);
end;


 

接下来是实现:

////////////////////////////////////////////////////////////////////////////////
///
///
///

{TcxGridExpandAllGroup}
procedure TcxGridExpandAllGroup.Execute(Sender: TObject);
begin
  HitGridView.DataController.Groups.FullExpand;
end;

function TcxGridExpandAllGroup.GetEnabled;
begin
  Result:
= Boolean (HitGridView.DataController.GridView.GroupedItemCount );
end;

constructor TcxGridExpandAllGroup.Create;
begin
  inherited;
  ResCaption :
= @cxSGridExpandAllGroup;
end;

function TcxGridExpandAllGroup.GetImageResourceName;
begin
  Result :
= AImageName + '21';
end;


{TcxGridCollapseAllGroup}
procedure TcxGridCollapseAllGroup.Execute(Sender: TObject);
begin
  HitGridView.DataController.Groups.FullCollapse;
end;

function TcxGridCollapseAllGroup.GetEnabled;
begin
  Result:
= Boolean (HitGridView.DataController.GridView.GroupedItemCount );
end;

constructor TcxGridCollapseAllGroup.Create;
begin
  inherited
  ResCaption :
= @cxSGridCollapseAllGroup;
  self.ImageIndex:
=99;
end;

{TcxGridExport}
procedure TcxGridExport.Execute(Sender: TObject);
var
  SFD:TSaveDialog;
  FileName,FileExt:String;
begin
  SFD:
=TSaveDialog.Create(nil);
  SFD.Filter:
='Excel文件 (*.xls)|*.xls|XML文件 (*.xml)|*.xml|文本文件 (*.txt)|*.txt|网页文件 (*.html)|*.html';
  SFD.Title:
='导出为';
  SFD.DefaultExt:
='.XLS';
  SFD.Options:
=[ofOverwritePrompt, ofEnableSizing];

  
if not SFD.Execute then exit;
  FileName:
=SFD.FileName;
  FileExt:
=UpperCase(ExtractFileExt(FileName));

  
if FileExt='.XLS' then
    ExportGridToExcel(FileName,Self.GridPopupMenu.Grid)
    
else if FileExt='.XML' then
         ExportGridToXML(FileName,self.GridPopupMenu.Grid)
         
else if FileExt='.TXT' then
              ExportGridToText(FileName,Self.GridPopupMenu.Grid)
              
else if FileExt='.HTML' then
                   ExportGridToHTML(FileName,Self.GridPopupMenu.Grid)
                   
else
                   begin
                      Application.MessageBox(
'不支持的导出格式''xx');
                      Exit;
                   end;
   SFD.Free;                
end;

constructor TcxGridExport.Create;
begin
  inherited Create;
  ResCaption :
= @cxSGridExport;
end;

function TcxGridExport.GetImageResourceName;
begin
  Result :
= AImageName + '23';
end;


procedure TcxGridPrint.Execute(Sender: TObject);
begin
// todo:Addcode
end;

constructor TcxGridPrint.Create;
begin
  inherited
  ResCaption:
=@cxSGridPrint;
  Self.ImageIndex:
=99;
end;

 

最后还要在cxGridPopupMenuConsts.pas中定义资源

 cxSGridExpandAllGroup = '展开全部分组';
  cxSGridCollapseAllGroup 
= '收起所有分组';
  cxSGridExport 
= '导出所有数据';
  cxSGridPrint 
= '打印表格';


还有一点要声明一下,打印表格的代码没有实现,嘿嘿。

posted on 2007-08-06 15:59  qufo  阅读(1265)  评论(0编辑  收藏  举报

导航