简体   繁体   English

通过ADO将TDataSet复制到XLS

[英]Copy TDataSet to XLS via ADO

I'm trying to copy the content from a TDBGrid to an Excel file using an ADO connection to transfer the data. 我正在尝试使用ADO连接将内容从TDBGrid复制到Excel文件以传输数据。 This works for values that are <= 255 characters but fails for longer strings. 这适用于<= 255个字符的值,但不能用于较长的字符串。 What can I do to copy strings that are longer than 255 characters? 如何复制长度超过255个字符的字符串?

Changing the DataType to adLongVarWChar tbl.Columns.Append doesn't work. 将DataType更改为adLongVarWChar tbl.Columns.Append不起作用。 The ADOQuery gets a varchar field with Size 255 regardsless what I use when I create the table. ADOQuery获取大小为255的varchar字段,无论创建表时使用什么。

procedure DBGridToExcelADO(DBGrid: TDBGrid; FileName: string; SheetName: string);
var
  cat               : _Catalog;
  tbl               : _Table;
  col               : _Column;
  i                 : integer;
  ADOConnection     : TADOConnection;
  ADOConnectionExcel: TADOConnection;
  ADOQuery          : TADOQuery;
  ScrollEvents      : TScrollEvents;
  SavePlace         : TBookmark;
begin
  //exporting
  ADOConnectionExcel := TADOConnection.Create(nil);
  try
    ADOConnectionExcel.LoginPrompt      := False;
    ADOConnectionExcel.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0; Data Source=' + FileName + ';Extended Properties=Excel 8.0';

    ADOConnectionExcel.Open;
    //WorkBook creation (database)
    cat := CoCatalog.Create;
    cat._Set_ActiveConnection(ADOConnectionExcel.ConnectionObject);
    //WorkSheet creation (table)
    tbl := CoTable.Create;
    tbl.Set_Name(SheetName);
    //Columns creation (fields)
    DBGrid.DataSource.DataSet.First;

    with DBGrid.Columns do
    begin
      for i := 0 to Count - 1 do
        if Items[i].Visible then
        begin
          col := nil;
          col := CoColumn.Create;
          with col do
          begin
            Set_Name(Items[i].Title.Caption);
            Set_Type_(adVarWChar);
          end;
          //add column to table
          tbl.Columns.Append(col, adVarWChar, 20);
        end;
    end;
    //add table to database
    cat.Tables.Append(tbl);

    col := nil;
    tbl := nil;
    cat := nil;

    //exporting
    ADOConnection                  := TADOConnection.Create(nil);
    ADOConnection.LoginPrompt      := False;
    ADOConnection.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0; Data Source=' + FileName + ';Extended Properties=Excel 8.0';
    ADOQuery            := TADOQuery.Create(nil);
    ADOQuery.Connection := ADOConnection;
    ADOQuery.SQL.Text   := 'Select * from [' + SheetName + '$]';
    ADOQuery.Open;

    DisableDependencies(DBGrid.DataSource.DataSet, ScrollEvents);
    SavePlace := DBGrid.DataSource.DataSet.GetBookmark;
    try
      with DBGrid.DataSource.DataSet do
      begin
        First;
        while not Eof do
        begin
          ADOQuery.Append;
          with DBGrid.Columns do
          begin
            ADOQuery.Edit;
            for i := 0 to Count - 1 do
              if Items[i].Visible then
              begin
                //Fails if Length > 255
                ADOQuery.FieldByName(Items[i].Title.Caption).AsString := FieldByName(Items[i].FieldName).AsString;
              end;
            ADOQuery.Post;
          end;
          Next;
        end;
      end;

    finally
      DBGrid.DataSource.DataSet.GotoBookmark(SavePlace);
      DBGrid.DataSource.DataSet.FreeBookmark(SavePlace);
      EnableDependencies(DBGrid.DataSource.DataSet, ScrollEvents);

      ADOQuery.Close;
      ADOConnection.Close;

      ADOQuery.Free;
      ADOConnection.Free;
    end;
  finally
    if Assigned(ADOConnection) and ADOConnection.Connected then ADOConnectionExcel.Close;
    ADOConnectionExcel.Free;
  end;
end;

You can use TJvDBGridExcelExport JVCL coming in the Jedi (www.delphi-jedi.org/). 您可以使用Jedi(www.delphi-jedi.org/)中的TJvDBGridExcelExport JVCL。 I have used this with good results, and this is opensource. 我用它取得了很好的效果,这是开源的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM