Newsgroup: it.comp.lang.delphi
Titolo: questa volta posto tutta la unit

unit provaexportexcel;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Grids, Excel97, OleServer;
type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    esportastringgridinexcel: TButton;
    ExcelWorksheet1: TExcelWorksheet;
    ExcelWorkbook1: TExcelWorkbook;
    ExcelApplication1: TExcelApplication;
    procedure esportastringgridinexcelClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
uses activex;
{$R *.DFM}
procedure TForm1.esportastringgridinexcelClick(Sender: TObject);
 var lcid:integer;
    Row: integer;
    ColumnTitles: olevariant;
    x,y:integer;
    invert:boolean;
begin
  lcid := GetUserDefaultLCID;
  ExcelApplication1.Visible[lcid]:=True;
  // connetto ad un workbook un foglio di lavoro
excelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Add(TOleEnum(xlWBATWork
sheet), lcid));
  excelworksheet1.ConnectTo(excelWorkbook1.Worksheets[1] as _Worksheet);
 
excelworksheet1.name
 := 'Dati';
  ExcelApplication1.ScreenUpdating[lcid] := false;
  screen.cursor:=crhourglass;
  try
    invert:=stringgrid1.colcount>255;// siccome non può superare le 255
colonne lo inverto
      if invert then  begin  // inverto righe e colonne se necessario
    // riempio le celle
    for x:= 0 to stringgrid1.colcount-1 do
    for y:= 0 to stringgrid1.rowcount-1 do begin
   // application.processmessages;
     excelworksheet1.Cells.items[x+1, y+1].text :=stringgrid1.cells[x,y]
                                  end;
                       end
                else begin
    for x:= 0 to stringgrid1.colcount-1 do
    for y:= 0 to stringgrid1.rowcount-1 do begin
    //application.processmessages;
     excelworksheet1.Cells.items[y+1, x+1].text :=stringgrid1.cells[x,y];
                                  end;
                     end;
  finally // disconnetto
  ExcelApplication1.ScreenUpdating[lcid] := True;
  excelworksheet1.Disconnect;
  excelworkbook1.Disconnect;
  ExcelApplication1.Disconnect;
  screen.cursor:=crdefault;
end;
end;
procedure TForm1.FormActivate(Sender: TObject);
var x,y:integer;
begin
stringgrid1.colcount:=91;
stringgrid1.rowcount:=4000;  // se provate a diminure il valore funziona
for x:=0 to stringgrid1.colcount-1 do
for y:=0 to stringgrid1.rowcount-1 do
stringgrid1.Cells[x,y]:=format('%d %d',[x,y]);
end;
end.
un form con una stringgrid e un button con associato la procedura
d'esportazione...
a me si blocca e devo resettare il PC
ringrazierei chiounque riuscisse a dirmi come farlka funzionare..
tenete presente che con valori bassi di righe (row count:=10) funziona.

scusami...
ti avevo suggerito di usare direttamente ole senza passare per i server.
Lo hai provato?
AS


Si ho provato... ma l'errore me lo da lo stesso...
i server sono piu' comodi...
anche se forse fanno casino!

 Si ho provato... ma l'errore me lo da lo stesso...
puoi postare il codice?
AS


Se ti fa piacere....
ci ho messo un paio di spinedit in piu'....ripetto al precedente
unit provaexportexcel;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Grids, Excel97, OleServer, ComObj, ActiveX, ComCtrls, Spin;
type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    esportastringgridinexcel: TButton;
    ProgressBar1: TProgressBar;
    SpinEdit1: TSpinEdit;
    SpinEdit2: TSpinEdit;
    Button1: TButton;
    procedure esportastringgridinexcelClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.esportastringgridinexcelClick(Sender: TObject);
  var
    ExcelAPPLICATION: Variant;
    EXCELWORKBOOK:OLEVARIANT;
    EXCELWORKSHEETS:OLEVARIANT;
    SHEETNAME:OLEVARIANT;
    lcid:integer;
    Row: integer;
    ColumnTitles: olevariant;
    x,y:integer;
    invert:boolean;
begin
      lcid := GetUserDefaultLCID;
      ExcelAPPLICATION := CreateOleObject('Excel.Application');
      ExcelAPPLICATION.Visible := True;
      // connetto ad un workbook un foglio di lavoro
  excelWorkbook:= ExcelAPPLICATION.WorkBooks.Add;
  EXCELWORKBOOK.WORKSHEETS[1].NAME:='sHEET1';
  EXCELWORKSHEETS := EXCELWorkBook.Worksheets['sHEET1'];
  EXCELWORKSHEETS.Activate;
  ExcelApplication.ScreenUpdating:= false;
  screen.cursor:=crhourglass;
  try
    invert:=stringgrid1.colcount>255;// siccome non può superare le 255
colonne lo inverto
      if invert then  begin  // inverto righe e colonne se necessario
    // riempio le celle
    for x:= 0 to stringgrid1.colcount-1 do
    for y:= 0 to stringgrid1.rowcount-1 do begin
   // application.processmessages;
     excelworksheetS.Cells[x+1, y+1].text :=stringgrid1.cells[x,y]
                                  end;
                       end
                else begin
    PROGRESSBAR1.MAX:=STRINGGRID1.COLCOUNT-1;
    for x:= 0 to stringgrid1.colcount-1 do BEGIN
    PROGRESSBAR1.Position:=X;
    for y:= 0 to stringgrid1.rowcount-1 do begin
    //application.processmessages;
     excelworksheetS.Cells[y+1, x+1] :=stringgrid1.cells[x,y];
                                  end;
                                           END;
                     end;
  finally // disconnetto
  ExcelApplication.ScreenUpdating := True;
  excelapplication.close;
  screen.cursor:=crdefault;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var x,y:integer;
begin
stringgrid1.colcount:=spinedit1.value;
stringgrid1.rowcount:=spinedit2.value;
//con valori bassi funziona
