Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Servers (https://www.clubdelphi.com/foros/forumdisplay.php?f=9)
-   -   Problemas importando archivo Excel (https://www.clubdelphi.com/foros/showthread.php?t=48184)

duilioisola 18-09-2007 10:52:18

Problemas importando archivo Excel
 
Nota : a algún administrador o moderador. Este mensaje no está en el foro correcto. Agradecería lo movierais a Servers. (Creo que sería el correcto)
MODERADOR: Pues ya está movido. De nada.

Hola a todos ... espero que me puedan ayudar en esto.
Estoy importando una planilla Excel con varias hojas y la primera columna de cada hoja que tiene datos me la importa siempre como 0 (ceros)

Estoy importando con un TADOConnection y una TADOTable.

El código:
Código Delphi [-]
DBExcel : TADOConnection 
TExcel : TADOTable.
(...)
procedure TDMMain.ImportarExcel;
var
   TableNames : TStrings;
   i : integer;
   j : integer;
   s : string;
begin
   try
      {conecto con la planilla excel}
      DBExcel.ConnectionString :=
      wideString('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + archivo + ';Extended Properties=Excel 8.0;Persist Security Info=False');
      DBExcel.Open;
      try
         {me paseo por todas las hojas de la planilla}
         TableNames := TStringList.Create;
         DBExcel.GetTableNames(TableNames,False);
         for i := 0 to (TableNames.Count - 1) do
         begin
            try
               try
                  if TExcel.Active then TExcel.Close;
                  TExcel.TableName := TableNames[i];
                  TExcel.Open;
                  TExcel.First;
                  {me paseo por cada linea de la hoja}
                  while not TExcel.Eof do
                  begin
                     s := '';
                     for j := 0 to TExcel.FieldCount -1 do
                        s := s + TExcel.Fields[j].AsString+' | ';
                     FMMain.MemoLog.Lines.Add(TExcel.TableName+' --> '+s);   
                     if ((Trim(TExcel.Fields[6].AsString)>'') and (Trim(TExcel.Fields[2].AsString)>'') and (Trim(TExcel.Fields[0].AsString)<>'RECHNUNG')) then
                     begin
                        with QGeneral do
                        begin
                           if (not Transaction.InTransaction) then Transaction.StartTransaction;
                           Close;
                           SQL.Text := 'insert into hobby_caravanas_rec(ID,CARPETA,FACTURA,FECHA,ARTICULO,PROGRAMA,IMPORTE1,NRO_SERIE,IMPORTE2) '+
                                       'values (?ID,?CARPETA,?FACTURA,?FECHA,?ARTICULO,?PROGRAMA,?IMPORTE1,?NRO_SERIE,?IMPORTE2)';
                           Params.ByName['ID'].AsInteger := 0;
                           Params.ByName['CARPETA'].AsString := copy(TExcel.TableName,1,length(TExcel.TableName)-1);
                              Params.ByName['FACTURA'].AsString := TExcel.Fields[0].AsString;
                           end;
                           Params.ByName['FECHA'].AsDateTime := TExcel.Fields[1].AsDateTime;
                           Params.ByName['ARTICULO'].AsString := TExcel.Fields[2].AsString;
                           Params.ByName['PROGRAMA'].AsString := TExcel.Fields[4].AsString;
                           try
                              Params.ByName['IMPORTE1'].AsFloat := TExcel.Fields[5].AsFloat;
                           except
                              Params.ByName['IMPORTE1'].AsFloat := -1;
                           end;
                           Params.ByName['NRO_SERIE'].AsString := TExcel.Fields[6].AsString;
                           try
                              Params.ByName['IMPORTE2'].AsFloat := TExcel.Fields[8].AsFloat;
                           except
                              Params.ByName['IMPORTE2'].AsFloat := -1;
                           end;
                           try
                              ExecQuery;
                              if (Transaction.InTransaction) then Transaction.Commit;
                           except
                              on E:exception do
                              begin
                                 if (Transaction.InTransaction) then Transaction.Rollback;
                                 FMMain.MemoLog.Lines.Add(TExcel.TableName+' --> '+s+#10+#13+E.Message);
                              end;
                           end;
                        end; {with}
                     end;
                     TExcel.Next;
                  end; // while not TExcel.Eof
               except
                  On E:Exception do MessageDlg('No puedo abrir al Hoja Excel : '+TExcel.TableName+#10+#13+E.Message, mtError,[mbOk], 0);
               end;
            finally
               TExcel.Close;
            end;
         end; //for TableNames.Count
      finally
         TableNames.Free;
      end;
      DBExcel.Close;

La tabla Excel:
La primera columna (A) está en blanco
La segunda columna (B) es la que tiene el texto RECHNUNG y luego numeros debajo.
La columna C tiene fechas
La columna D tiene un codigo alfanumerico
La columna E está en blanco
La columna F tiene un codigo alfanumerico
La columna G tiene un importe
La columna H tiene un codigo alfanumerico
La columna I tiene un importe
Código:

                                   
    CARAVANAS RECIBIDAS EN ESPAÑA                               
                            PARA                               
          CARAVANING CANARIAS,S.L.                               
                                   
                                   
    RECHNUNG        Artikelbezeichnung        Program.        FAHRGESTELL - Nr.       
                                   
    6407436    09/05/2006    T-650-FSC ALK        AKTION 3        WF0VXXTTFV5E68938        32.733,99
    6407536    31/05/2006    T-550-FS teleintegriet        AKTION 3        WF0VXXTTFV6S34452        26.527,09
    6407579    07/06/2006    T-550-FS -ALK        AKTION 3        WF0VXXTTFV6S36221        25.862,07

El log de salida:
Como ven, se salta la primera columna (en blanco, supongo que será cosa del componente) luego importa la segunda (con texto RECHNUNG) pero debajo de esta, los número los deja en blanco.
Código:

Empieza importacion...
CANARIAS$ -->                          PARA |  |  |  |  |  |  |  |  |
CANARIAS$ -->        CARAVANING CANARIAS,S.L. |  |  |  |  |  |  |  |  |
CANARIAS$ -->  |  |  |  |  |  |  |  |  |
CANARIAS$ -->  |  |  |  |  |  |  |  |  |
CANARIAS$ --> RECHNUNG |  | Artikelbezeichnung |  | Program. |  | FAHRGESTELL - Nr. |  |  |
CANARIAS$ -->  |  |  |  |  |  |  |  |  |
CANARIAS$ -->  | 09/05/2006 | T-650-FSC ALK |  | AKTION 3 |  | WF0VXXTTFV5E68938 |  | 32733,99 |
CANARIAS$ -->  | 31/05/2006 | T-550-FS teleintegriet |  | AKTION 3 |  | WF0VXXTTFV6S34452 |  | 26527,09 |
CANARIAS$ -->  | 07/06/2006 | T-550-FS -ALK |  | AKTION 3 |  | WF0VXXTTFV6S36221 |  | 25862,07 |


duilioisola 18-09-2007 21:01:44

Bueno ... reavivo este hilo, a ver si alguien me puede hechar una mano ...

duilioisola 19-09-2007 18:31:46

Alguien tiene alguna pista de dónde puede estar el fallo?
Necesito que me de los datos de la primera columna en el Field[0] y viene vacío. Ya no se dónde buscar ...


La franja horaria es GMT +2. Ahora son las 12:14:33.

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