1

我正在尝试使用 ADO 连接将内容从 TDBGrid 复制到 Excel 文件以传输数据。这适用于 <= 255 个字符的值,但不适用于较长的字符串。如何复制长度超过 255 个字符的字符串?

将 DataType 更改为 adLongVarWChar tbl.Columns.Append 不起作用。无论我在创建表时使用什么,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;
4

1 回答 1

1

您可以使用 Jedi 中的 TJvDBGridExcelExport JVCL (www.delphi-jedi.org/)。我用这个效果很好,这是开源的。

于 2012-12-03T13:00:07.807 回答