Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Servers (https://www.clubdelphi.com/foros/forumdisplay.php?f=9)
-   -   Error con exportacion a Excel (https://www.clubdelphi.com/foros/showthread.php?t=86523)

jafera 26-08-2014 12:12:30

Error con exportacion a Excel
 
Buenas a todos.

Como comenté en algún otro post, hice el cambio de D6 a DXE5, no sin tener más de un problema con la conversión de código.
De momento todos los problemas se han ido solucionando y aplicando mejoras en el código a fin de aligerar un poco los ejecutables que han crecido enormemente en relación a los viejos D6.

El problema con el que no doy la solución por ahora es la exportación a excel del contenido de una sql a fin de que sea transparente al usuario.
Hasta el momento usaba el código siguiente:

Código Delphi [-]
procedure TF_GeneracioFullPagament.BtnExcelClick(Sender: TObject);
var
        RangeE: Excel2000.ExcelRange;                                //linea 263
        I, Row: Integer;
        Bookmark: TBookmarkStr;
begin
        IBExcel1.Close;
        IBExcel1.SQL[6] := 'AND C.ANIO='+ComboBox1.Text;
        IBExcel1.Open;
        ExcelApplication1.Visible [0] := True;
        ExcelApplication1.Workbooks.Add (NULL, 0);
        RangeE := ExcelApplication1.ActiveCell;
        for I := 0 to IBExcel1.Fields.Count - 1 do
        begin
                RangeE.Value := IBExcel1.Fields [i].DisplayLabel;
                RangeE := RangeE.Next;
        end;
                IBExcel1.DisableControls;
        try
                Bookmark := IBExcel1.Bookmark;                          //linea 280
                try
                        IBExcel1.First;
                        Row := 2;
                        while not IBExcel1.EOF do
                        begin
                                RangeE := ExcelApplication1.Range ['A' + IntToStr (Row),'A' + IntToStr (Row)];
                                for I := 0 to IBExcel1.Fields.Count - 1 do
                                begin
                                        RangeE.Value := IBExcel1.Fields [i].AsString;
                                        RangeE := RangeE.Next;
                                end;
                                begin
                                        IBExcel1.Next;
                                        Inc (Row);
                                        ExcelApplication1.Range['G1','G'+IntToStr(Row)].NumberFormat := '@';
                                        ExcelApplication1.Range['J1','J'+IntToStr(Row)].NumberFormat := '0.00';
                                        ExcelApplication1.Range['K1','K'+IntToStr(Row)].NumberFormat := '0.00';
                                        ExcelApplication1.Range['L1','L'+IntToStr(Row)].NumberFormat := '0.00';
                                        ExcelApplication1.Range['M1','M'+IntToStr(Row)].NumberFormat := '0.00';
                                        ExcelApplication1.Range['N1','N'+IntToStr(Row)].NumberFormat := '0.00';
                                end;
                        end;
                finally
                        IBExcel1.Bookmark := Bookmark;                                      //linea 304
                end;
        finally
                IBExcel1.EnableControls;
        end;
        RangeE := ExcelApplication1.Range ['A1', 'N' + IntToStr (Row - 1)];
        RangeE.AutoFormat (3, NULL, NULL, NULL, NULL, NULL, NULL);
end;

y no le gusta las lineas:

[dcc32 Warning] Inici.pas(9): W1005 Unit 'Vcl.FileCtrl' is specific to a platform
[dcc32 Warning] GenerarFullPagament.pas(263): W1002 Symbol 'ExcelRange' is specific to a platform
[dcc32 Error] GenerarFullPagament.pas(280): E2010 Incompatible types: 'AnsiString' and 'System.TArray<System.Byte>'
[dcc32 Error] GenerarFullPagament.pas(304): E2010 Incompatible types: 'System.TArray<System.Byte>' and 'AnsiString'

He cambiado los servers a XP en lugar de 2000 y el código queda así:

Código Delphi [-]
procedure TF_GeneracioFullPagament.BtnExcelClick(Sender: TObject);
var
        RangeE: ExcelXP.ExcelRange;
        I, Row: Integer;
        Bookmark: TBookmarkStr;
begin
        IBExcel1.Close;
        IBExcel1.SQL[6] := 'AND C.ANIO='+ComboBox1.Text;
        IBExcel1.Open;
        ExcelApplication1.Visible [0] := True;
        ExcelApplication1.Workbooks.Add (NULL, 0);
        RangeE := ExcelApplication1.ActiveCell;
        for I := 0 to IBExcel1.Fields.Count - 1 do
        begin
                RangeE.Value := IBExcel1.Fields [i].DisplayLabel;       // linea 275
                RangeE := RangeE.Next;
        end;
                IBExcel1.DisableControls;
        try
                Bookmark := IBExcel1.Bookmark;                            // linea 280
                try
                        IBExcel1.First;
                        Row := 2;
                        while not IBExcel1.EOF do
                        begin
                                RangeE := ExcelApplication1.Range ['A' + IntToStr (Row),'A' + IntToStr (Row)];
                                for I := 0 to IBExcel1.Fields.Count - 1 do
                                begin
                                        RangeE.Value := IBExcel1.Fields [i].AsString;              //linea 289
                                        RangeE := RangeE.Next;
                                end;
                                begin
                                        IBExcel1.Next;
                                        Inc (Row);
                                        ExcelApplication1.Range['G1','G'+IntToStr(Row)].NumberFormat := '@';
                                        ExcelApplication1.Range['J1','J'+IntToStr(Row)].NumberFormat := '0.00';
                                        ExcelApplication1.Range['K1','K'+IntToStr(Row)].NumberFormat := '0.00';
                                        ExcelApplication1.Range['L1','L'+IntToStr(Row)].NumberFormat := '0.00';
                                        ExcelApplication1.Range['M1','M'+IntToStr(Row)].NumberFormat := '0.00';
                                        ExcelApplication1.Range['N1','N'+IntToStr(Row)].NumberFormat := '0.00';
                                end;
                        end;
                finally
                        IBExcel1.Bookmark := Bookmark;                                           //linea 304
                end;
        finally
                IBExcel1.EnableControls;
        end;
        RangeE := ExcelApplication1.Range ['A1', 'N' + IntToStr (Row - 1)];
        RangeE.AutoFormat (3, NULL, NULL, NULL, NULL, NULL, NULL);
end;

Y ahora me da error en las lineas:

[dcc32 Error] GenerarFullPagament.pas(275): E2029 '[' expected but ':=' found
[dcc32 Error] GenerarFullPagament.pas(275): E2010 Incompatible types: 'Procedure' and 'string'
[dcc32 Error] GenerarFullPagament.pas(280): E2010 Incompatible types: 'AnsiString' and 'System.TArray<System.Byte>'
[dcc32 Error] GenerarFullPagament.pas(289): E2029 '[' expected but ':=' found
[dcc32 Error] GenerarFullPagament.pas(289): E2010 Incompatible types: 'Procedure' and 'string'
[dcc32 Error] GenerarFullPagament.pas(304): E2010 Incompatible types: 'System.TArray<System.Byte>' and 'AnsiString'

Alguien me puede iluminar un poco?

Llevo dias dándole vueltas y no encuentro por dónde atajarlo.

Saludos

Josep

oscarac 27-08-2014 06:20:24

me parece que el tema del range debe estar en este formato


Código Delphi [-]
ExcelApplication1.Range['G1:G'+IntToStr(Row)].NumberFormat

jafera 28-08-2014 11:53:15

Gracias por la respuesta Oscarac.

Al final he optado por el uso del componente SCExcelExport que simplifica bastante el trabajo, os adjunto el código:

Código Delphi [-]
procedure TF_GeneracioFullPagament.BtnExcelClick(Sender: TObject);
begin
        scExcelExport1.WorksheetName := 'Subvencions Carnaval '+ComboBox1.Text;
        scExcelExport1.Dataset:=IBExcel1;
        IBExcel1.Close;
        IBExcel1.SQL[6] := 'AND C.ANIO='+ComboBox1.Text;
        IBExcel1.Open;
        scExcelExport1.StyleColumnWidth:=cwOwnerWidth;
        scExcelExport1.ColumnWidth := 20;
        scExcelExport1.HeaderText.Text:='Sunvenció colles carnaval any '+ComboBox1.Text;
        scExcelExport1.FontHeader.Size:=12;
        scExcelExport1.BeginRowHeader:=2;
        scExcelExport1.BeginRowTitles:=4;
        scExcelExport1.BorderTitles.BackColor := clBlue;
        scExcelExport1.FontTitles.Color:=clWhite;
        scExcelExport1.SummarySelection := ssValues;
        scExcelExport1.SummaryCalculation := scSUM;
        scExcelExport1.StyleColumnWidth:=cwAutoFit;
        //scExcelExport1.ExcelWorkSheet.PageSetup.Orientation:= xlLandscape;
        scExcelExport1.ExcelVisible:=True;

        try
                scExcelExport1.ExportDataset;
        if Assigned(scExcelExport1.ExcelWorkSheet) then
                scExcelExport1.SaveAs('C:\Carnaval_1.0\Fulles Excel\Subvencio Carnaval '+ComboBox1.Text+'.xls',ffXLS);
        finally
                scExcelExport1.Disconnect;
        end;
end;

Funciona bien excepto la linea comentada que me arroja un error de Interface Not Supported.
Olvido algun paso o colocar algo en uses?

Gracias

Josep


La franja horaria es GMT +2. Ahora son las 07:33:19.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi