• 将 DBGrid 中的内容输出至 Excel 或 ClipBoard


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

  • 相关阅读:
    结构体怎么组包发送
    开源语音代码eSpeak1.06 的移植到单片机的过程(二)之分析下speak.c 文件
    看看深圳的房价
    开源语音代码eSpeak1.06 的移植到单片机的过程(一)0之分析下espeak.c 文件
    开源语音代码eSpeak1.06 的学习入门
    利尔达模组CAT1 UIS8910指令的 TCP相关中文解释
    将博客搬至CSDN
    【原创】大叔问题定位分享(39)azkaban定期出现fullgc
    【原创】大叔经验分享(129)mac下启动MAT报错
    【原创】大数据基础之Doris(1)编译安装和启动
  • 原文地址:https://www.cnblogs.com/beeone/p/1792346.html
Copyright © 2020-2023  润新知