| 网站首页 | JAVA文章 | AppServers | Web开发 | 应用开发 | 资源下载 | 论坛
    想学好编程,学好外语很重要  [enadd  2006年12月25日]        
设为首页 加入收藏 联系站长
您现在的位置: 编程笔记网 >> 应用开发 >> delphi >> 控件应用 >> 文章正文
将 DBGrid 中的内容输出至 Excel 或 ClipBoard            【字体:
将 DBGrid 中的内容输出至 Excel 或 ClipBoard
作者:-    文章来源:-    点击数:    更新时间:2006-12-31

//注意:下面的方法必须包含 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;  

(出处:www

[1] [2] 下一页  

文章录入:enadd    责任编辑:enadd 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
  • 用DbGrid制作edit录入时的下…

  • Delphi的dbgrid中根据数据的…

  • 给DBGrid加入排序功能

  • 怎样获得DBGrid中的cell的坐…

  • 多层表头的DBGrid

  • 在 dbgrid 中实现 copy、pas…

  • 禁止在DBGrid中按delete删除…

  • 给 DBGrid 添加搜索功能

  • 数据网格自动适应宽度

  • 移除DBGrid的垂直滚动条

  •   网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 管理登录 |