Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Importar desde Excel campo vacio (https://www.clubdelphi.com/foros/showthread.php?t=65859)

sonjeux 18-01-2010 17:52:48

Importar desde Excel campo vacio
 
SAludos, deseo saber como puedo saber si un determinado campo en excel esta vacio, estoy usando el siguiente codigo:

Código Delphi [-]
procedure TProductoListado.ImportarDatosdesdeExcel1Click(Sender: TObject);
const
  xlCellTypeLastCell = $0000000B;
var   Libro, hoja : OleVariant;
      //Hoja : _WORKSHEET;
      i:Integer;
      id, fila1:String;
begin
   OpenDialog1.Execute;
   if OpenDialog1.Files.Text<>'' then
   if Application.MessageBox('Desea importar el archivo seleccionado?.','Advertencia', MB_YESNO+MB_ICONQUESTION)=IDYES then
      begin
         i:=1;
         Libro:=CreateOleObject('Excel.Application');
         (*Libro:=ExcelApplication1.Workbooks.Open('c:\productos.xls', EmptyParam, EmptyParam, EmptyParam,
                                         EmptyParam, EmptyParam, EmptyParam, EmptyParam,
                                         EmptyParam, EmptyParam, EmptyParam, EmptyParam,
                                         EmptyParam, EmptyParam, EmptyParam, 0);*)
         Libro.Workbooks.Open(OpenDialog1.FileName);

         hoja := Libro.Workbooks[ExtractFileName(OpenDialog1.FileName)].WorkSheets[1];
         hoja.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate;

         //ShowMessage(IntToStr(Libro.ActiveCell.Row));

         fila1:=hoja.Cells.Item[1,1];
         if fila1='ID' then
            begin
               Medidor.Show;
               Medidor.ProgressBar1.Max:=Libro.ActiveCell.Row;
               Medidor.ProgressBar1.Step:=1;
               for i:=2 to Libro.ActiveCell.Row do
                  begin
                     id:=hoja.Cells.Item[i,1];
                     if id<>''then
                        begin
                           if id<>'0' then
                              begin
                                 IBQConsulta.Close;
                                 IBQConsulta.ParamByName('codigo').AsString:=hoja.Cells.Item[i,2];
                                 IBQConsulta.ParamByName('nombre').AsString:=hoja.Cells.Item[i,4];
                                 IBQConsulta.ParamByName('comprasf').AsFloat:=hoja.Cells.Item[i,6];
                                 IBQConsulta.ParamByName('comprascf').AsFloat:=hoja.Cells.Item[i,7];
                                 IBQConsulta.ParamByName('precioa').AsFloat:=hoja.Cells.Item[i,8];
                                 IBQConsulta.ParamByName('preciob').AsFloat:=hoja.Cells.Item[i,9];
                                 IBQConsulta.ParamByName('precioc').AsFloat:=hoja.Cells.Item[i,10];
                                 IBQConsulta.ParamByName('preciod').AsFloat:=hoja.Cells.Item[i,11];
                                 IBQConsulta.ParamByName('id').AsString:=hoja.Cells.Item[i,1];
                                 IBQConsulta.Open;
                              end
                           else
                              begin
                                 IBQProducto.Close;
                                 IBQProducto.ParamByName('id_categoria').AsString:=hoja.Cells.Item[i,14];
                                 IBQProducto.ParamByName('id_sub_categoria').AsString:='0';
                                 IBQProducto.ParamByName('id_marca').AsString:='0';
                                 IBQProducto.ParamByName('id_medida').AsString:='0';
                                 IBQProducto.ParamByName('codigo').AsString:=hoja.Cells.Item[i,2];
                                 IBQProducto.ParamByName('serie').AsString:=hoja.Cells.Item[i,3];
                                 IBQProducto.ParamByName('nombre').AsString:=hoja.Cells.Item[i,4];
                                 IBQProducto.ParamByName('modelo').AsString:=hoja.Cells.Item[i,5];
                                 IBQProducto.ParamByName('descripcion').AsString:='0';
                                 IBQProducto.ParamByName('preciocsf').AsFloat:=hoja.Cells.Item[i,6];
                                 IBQProducto.ParamByName('precioccf').AsFloat:=hoja.Cells.Item[i,7];
                                 IBQProducto.ParamByName('precioventaa').AsFloat:=hoja.Cells.Item[i,8];
                                 IBQProducto.ParamByName('precioventab').AsFloat:=hoja.Cells.Item[i,9];
                                 IBQProducto.ParamByName('precioventac').AsFloat:=hoja.Cells.Item[i,10];
                                 IBQProducto.ParamByName('precioventad').AsFloat:=hoja.Cells.Item[i,11];
                                 IBQProducto.ParamByName('imagen').AsString:='0';
                                 IBQProducto.ParamByName('observaciones').AsString:='0';
                                 IBQProducto.ParamByName('cantidad_min').AsString:=hoja.Cells.Item[i,12];
                                 IBQProducto.ParamByName('cantidad_max').AsString:=hoja.Cells.Item[i,13];
                                 IBQProducto.ParamByName('activo').AsString:='1';
                                 IBQProducto.ParamByName('servicio').AsString:='0';
                                 IBQProducto.ParamByName('compuesto').AsString:='0';
                                 IBQProducto.Open;
                              end
                        end;
                     Medidor.ProgressBar1.StepIt
                  end;
               IBQProductos.Close;
               IBQProductos.Open;
               //IBProducto.Refresh;
               Medidor.Close;
            end
         else
            Application.MessageBox('No se puede importar los datos!.','Error', MB_OK+MB_ICONERROR);
         Libro.Quit;
      end
end;

Y lo que quiero hacer, es ver si determinado campo no tiene ningun valor. Como puedo hacerlo? Gracias

delphi.com.ar 18-01-2010 18:35:25

Pregunta... (sin haberlo hecho previamente)... Las celdas no te retornan un variant?....
En ese caso asigná el la propiedad "Value" del campo directamente al valor de la celda, si es en ese caso quedará como NULL.

Saludos!

sonjeux 18-01-2010 23:58:59

Hola gracias por contestar, probe lo que me dijiste de ponerle value y no funciono, me sigue saliendo error, el problema es cuando la celda de excel esta vacia.

Tambien probe asi y nada:

Código Delphi [-]
IBQConsulta.ParamByName('codigo').AsString:=Libro.cells[i,2].value;
                                 IBQConsulta.ParamByName('nombre').Value:=Libro.Cells[i,4].Value;
                                 IBQConsulta.ParamByName('comprasf').Value:=Libro.Cells[i,6].Value;

y el error que me sale es el siguiente:



Como puedo hacer para que no me salga error si la celda de excel esta vacia?

ElKurgan 19-01-2010 07:08:20

Hablo de memoria, por lo que no se si he acertado o no, pero creo que al tratarse de Variants, hay una función que te devuelve "True" si el valor es nulo (VarIsNull o algo así). Puedes usarlo en algún if, del tipo que si es nulo se cargue un cero.

Un saludo

delphi.com.ar 19-01-2010 15:21:30

Creo que el tema no debería pasar por averiguar si estos datos son null o no, para este caso una opción es utilizar VarIsNull como responde ElKurgan. Pero en realidad creo que pasa por decidir un poco sobre tu modelo de datos, si tu modelo no acepta un campo null... ¿Es correcto que se ingrese ese registro?... si es correcto, entonces tu modelo no tendría que tener esa restricción, si el campo null es cambiado por otro valor, también pondría esto en el diseño de tu base de datos.

Saludos!


La franja horaria es GMT +2. Ahora son las 00:45:35.

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