Jump to content


Photo

exportar Dataset a HTML


  • Please log in to reply
6 replies to this topic

#1 tacubo

tacubo

    Advanced Member

  • Miembro Platino
  • PipPipPip
  • 130 posts

Posted 28 April 2010 - 07:52 AM

hola pues estoy tratando de exportar un Dataset a Html
pero la verdad no se como podria empezar alguien me puede
orientar el como hacer  :embarrassed: saludos a to2
  • 0

#2 Rolphy Reyes

Rolphy Reyes

    Advanced Member

  • Moderadores
  • PipPipPip
  • 2092 posts
  • LocationRepública Dominicana

Posted 28 April 2010 - 08:55 AM

Saludos.

No he hecho esta operación directamente en el DataSet, sino que el Grid contiene propiedades y/o métodos capaces de realizar la tarea.

Pero encontré que los JEDI tienen un componente llamado TJvDBGridHTMLExport que simplemente le indicas cual es tu Grid y este lo exporta.

Los JEDI son una suite de componentes gratuita y bastante útiles para realizar aplicaciones robustas.
  • 0

#3 enecumene

enecumene

    Webmaster

  • Administrador
  • 7419 posts
  • LocationRepública Dominicana

Posted 28 April 2010 - 08:57 AM



delphi
  1. function DataSetAHTML(DataSet: TDataSet): string;
  2. var
  3.   intX: Integer;
  4.   strLine: string;
  5. begin
  6.     if not Dataset.Active then dataset.Open;
  7.     DataSet.DisableControls;
  8.     // Escribimos el encabezado del archivo
  9.     Result := '';
  10.     result := Result + '';
  11.     for intX := 0 to dataset.FieldCount-1 do
  12.         if DataSet.Fields[intX].Visible then
  13.             Result := Result + '';
  14.     Result := Result + '';
  15.     // Recorremos cada Item del Dataset
  16.     DataSet.First;
  17.     while not DataSet.Eof do
  18.     begin
  19.           strLine := '';
  20.           for intX := 0 to DataSet.FieldCount-1 do
  21.               if DataSet.Fields[intX].Visible then
  22.                   strLine := strLine +'';
  23.           strLine := strLine + '';
  24.           Result := Result + strLine;
  25.           DataSet.Next;
  26.     end;
  27.     // ok
  28.     Result := Result + '' + '' + DataSet.Fields[intx].DisplayLabel + '' + '' + DataSet.Fields[intx].Text + '';
  29.     DataSet.EnableControls;
  30. end;



Esto lo puedes mostrarlo en un Memo y posteriormente guardarlo como HTML desde Delphi (Me refiero crear un archivo con extensión .html).

Saludos.
  • 0

#4 tacubo

tacubo

    Advanced Member

  • Miembro Platino
  • PipPipPip
  • 130 posts

Posted 28 April 2010 - 02:38 PM

perdon voy a probar lo que me comentan y les dire del avance
es que me encargaron otras cosas y apenas me desocupe
gracias por la ayuda
  • 0

#5 bigleaguer

bigleaguer

    Advanced Member

  • Miembros
  • PipPipPip
  • 66 posts

Posted 28 April 2010 - 07:58 PM

Saludos amigo. A continuación te dejó otra forma de hacerlo. Los parámetros del Método son el DataSet y el Archivo Html donde lo quieres guardar.



delphi
  1. procedure DataSetToHTML(DataSet: TDataSet; Archivo: TFileName);
  2. var
  3.   i: Integer;
  4.   Contenido: TStringList;
  5. begin
  6.   // Abrimos la Tabla
  7.   if not Dataset.Active then dataset.Open;
  8.  
  9.   Contenido := TStringList.Create;
  10.  
  11.   try
  12.     // Agregamos el Tag que define la tabla
  13.     Contenido.Add('<table border="1">');
  14.  
  15.     // Escribimos los Nombres de los Campos como Encabezado
  16.     Contenido.Add('<tr>');
  17.     for i := 0 to DataSet.FieldCount -1 do
  18.       Contenido.Add('<th BgColor="gray">'+DataSet.Fields[i].FieldName+'</th>');
  19.     Contenido.Add('</tr>');
  20.  
  21.     // Recorremos todos los registros del DataSet y creamos las celdas
  22.     DataSet.First;
  23.     while not DataSet.Eof do
  24.     begin
  25.  
  26.       Contenido.Add('<tr>');
  27.       for i := 0 to DataSet.FieldCount -1 do
  28.         Contenido.Add('<td>'+DataSet.Fields[i].AsString+'&nbsp;</td>');
  29.       Contenido.Add('<tr>');
  30.  
  31.       DataSet.Next;
  32.     end;
  33.  
  34.     // Cerramos el Tag <table>
  35.     Contenido.Add('</table>');
  36.  
  37.     // Guardamos el Contenido en el archivo
  38.     Contenido.SaveToFile(Archivo);
  39.   finally
  40.     // Liberamos el Objecto de Memoria
  41.     Contenido.Free;
  42.   end;
  43. end;



El código es algo sencillo, lo que se hace en el código es construir un archivo Html usando los tags para crear la tabla y las celdas. Primero recorreremos todos los campos para crear los encabezados con los nombres de los campos, luego recorremos todos los registros del DataSet y lo vamos agregando al contenido. Finalmente guardamos el contenido en el archivo que le digamos.

Su uso sería de la siguiente forma:



delphi
  1. DataSetToHTML(Tabla, 'c:\test.html');



Espero te sirve amigo ;)


  • 0

#6 tacubo

tacubo

    Advanced Member

  • Miembro Platino
  • PipPipPip
  • 130 posts

Posted 30 April 2010 - 03:04 PM

Perdon por la tardanza es que soy nuevo en esto pero pues gracias a todos los que me ayudaron con sus valiosos comentarios lo que se paso decirles es que estaba haciendo un datamodule y pues ahi meti mis metodos de crear parametros y ejecutar el Sp despues de eso sigue lo que acontinuacion hice:

en un boton al cual llamare ejecutar es donde pongo lo del exportar el dataset al html y que posterior mente lo muestro tanto en un memo
y dentro de este muestro el codigo html necesario



delphi
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. Var
  3.   LDmDatos    : TDataModule2;
  4.   intX        : Integer;
  5.   Contenido  : string;
  6.   LsMensaje  : String;
  7.   begin
  8.     LDmDatos := TDataModule2.Create(self);
  9.     LsMensaje := LDmDatos.ConVacAut('2010-04-15');
  10.     if LsMensaje = 'Nada' Then
  11.       Try
  12.         Memo1.Lines.Add( '<table border="1"><tr><td>ID_TICKET</td><td>Fecha Creacion</td><td>Creador</td><td>Estatus</td><td>Solicitadas</td><td>Fechas Solicitas</td><td>Dias Disponibles</td><td>Fecha limite</td></tr>');
  13.         //Recorremos cada item del Dataset
  14.           while not LDmDatos.AdsDatos.Eof Do
  15.             begin
  16.               Contenido :=  '<tr>';
  17.               for intX :=0 to LDmDatos.AdsDatos.FieldCount-1 Do
  18.                 Contenido := Contenido + '<td>' + LDmDatos.AdsDatos.Fields[IntX].AsString + '</td>';
  19.               Contenido :=  Contenido + '</tr>';
  20.               LDmDatos.AdsDatos.Next;
  21.               Memo1.Lines.Add(Contenido);
  22.             end;
  23.           Memo1.Lines.Add('</table>')
  24.         Except
  25.         showmessage('Ocurrio error al Generar el archivo Html');
  26.       end
  27.       else
  28.       showmessage(LsMensaje);
  29.   end;
  30. end.


  • 0

#7 genyus00

genyus00

    Advanced Member

  • Miembros
  • PipPipPip
  • 52 posts
  • LocationBogota

Posted 26 March 2012 - 05:49 AM

Buenas, man te felicito por tu aporte, me ayudastes a solucionar un chicharron que tenia con una aplicacion intraweb, asi que tambien les doy mi aporte, mi caso trataba de exportar un maestro detalle a html, muy alegremente inicie con quickreport, pero no se que pasara con los componentes para D2007, ya que en XE me andan sin problemas, claro la version de D2007 es 4.07 y la de XE 5.05. Bueno a la final pense hasta en renunciar del trabajo  :cry: , hasta que buscando llegue aqui.. y bueno lo que hice fue hacerme un curso extrarapido de creacion de tablas con HTML, ya que solo sabia lo basico como se definian mas no como personalizarla, bueno una vez echo esto, cree la siguiente funcion.



delphi
  1. function DataSetToHTML(master,detail: TDataSet):string;//OD241
  2. var i: Integer;
  3. Contenido: TStringList;
  4. Archivo: Integer;
  5. direccion:string;
  6. gen:TGenerador;
  7. begin
  8. // Abrimos la Tabla
  9. if not master.Active then
  10.   master.Open;
  11.  
  12. if not detail.Active then
  13.   detail.Open;
  14.  
  15.  
  16. Contenido := TStringList.Create;
  17. try
  18.   gen:=TGenerador.create;
  19.   Archivo := gen.CreaArchivo(Direccion);
  20.   //Direccion := 'http://'+UserSession.ptrServidor.strHost+':'+UserSession.ptrServidor.strPuertoHTTPConsulta+'/Files';
  21.  
  22.   // Agregamos el Tag que define la tabla
  23.   Contenido.Add('<TABLE BGCOLOR="white" WIDTH = "100%"  CELLPADDING="2" CELLSPACING="0" BORDER ="1" BORDERCOLOR="black">');
  24.   Contenido.Add('<colgroup><col ALIGN="center"></colgroup>');
  25.   Contenido.Add('<TR><TD BGCOLOR="yellow" >REPORTE HISTORICO DE TURNO</TD></TR>');
  26.   master.First;
  27.   while not master.eof do
  28.         begin
  29.         Contenido.Add('<TR><TD BGCOLOR="lime" ALIGN="left" STYLE="font-size:8pt;">INFORMACION DE TURNO</TD></TR>');
  30.         Contenido.Add('<TR><TD><TABLE WIDTH = "100%" HEIGHT="25" CELLPADDING="0" CELLSPACING="0" BORDER ="1" BORDERCOLOR="black" STYLE="font-size:8pt;" >');
  31.         Contenido.Add('<colgroup><col WIDTH = "10%" STYLE="background-color:yellow;text-align:left;"><col ></colgroup>');
  32.         Contenido.Add('<TR><TH>Fecha</TH><TD>'+master.Fields.FieldByNumber(1).asstring+'</TD></TR>');
  33.         Contenido.Add('<TR><TH>Oficina</TH><TD>'+master.Fields.FieldByNumber(2).asstring+'</TD></TR>');
  34.         Contenido.Add('<TR><TH>Sala</TH><TD>'+master.Fields.FieldByNumber(3).asstring+'</TD></TR>');
  35.         Contenido.Add('<TR><TH>Turno</TH><TD>'+master.Fields.FieldByNumber(4).asstring+'</TD></TR>');
  36.         Contenido.Add('</TABLE>');
  37.         if detail.RecordCount>0 then
  38.             begin
  39.             Contenido.Add('</TD></TR><TR><TD><TABLE WIDTH = "100%" HEIGHT="25" CELLPADDING="0" CELLSPACING="0" BORDER ="1" BORDERCOLOR="black" STYLE="font-size:8pt;">');
  40.             Contenido.Add('<colgroup><TD BGCOLOR="yellow" COLSPAN="'+inttostr(detail.FieldCount)+'" ALIGN="center">INFORMACION ADICIONAL</TD></colgroup>');
  41.             // Escribimos los Nombres de los Campos como Encabezado
  42.             Contenido.Add('<TR BGCOLOR="lime">');
  43.             for i := 0 to detail.FieldCount -1 do
  44.                 Contenido.Add('<TH>'+detail.Fields[i].FieldName+'</TH>');
  45.             Contenido.Add('<TR>');
  46.             // Recorremos todos los registros del DataSet y creamos las celdas
  47.             detail.First;
  48.             while not detail.Eof do
  49.                   begin
  50.                   Contenido.Add('<TR>');
  51.                   for i := 0 to detail.FieldCount -1 do
  52.                       Contenido.Add('<TD ALIGN="center">'+detail.Fields[i].AsString+'&nbsp;</TD>');
  53.                   Contenido.Add('</TR>');
  54.                   detail.Next;
  55.                   end;
  56.             // Cerramos el Tag <table>
  57.             Contenido.Add('</TABLE>');
  58.             end;
  59.  
  60.         Contenido.Add('<BR></TD></TR>');
  61.         master.next;
  62.         end;
  63.  
  64.   // Guardamos el Contenido en el archivo
  65.   FileWrite(Archivo, Contenido.text[1], Length(contenido.text));
  66.   FileClose(Archivo);
  67.   Result:=Direccion;
  68.  
  69.   finally
  70.         // Liberamos el Objecto de Memoria
  71.         Contenido.Free;
  72.         end;
  73. end;
  74.  
  75. //esta funcion pertenece a una clase, pero igual creo se puede definir por aparte y a de funcionar
  76. function TGenerador.CreaArchivo(var Direccion: String): Integer;
  77. begin
  78. Direccion := GuidToString(uniGuidReporte)+'.HTML';
  79. Result := FileCreate(GetCurrentDir+'\Files\Temp\'+Direccion);
  80. end;
  81.  
  82. //Llamada despues de tener los datasets con los datos que se van a exportar
  83. if C_Maestro.recordcount>0 then
  84.   begin
  85.   strDireccion:=DataSetToHTML(C_Maestro,C_Detalle);
  86.     if strDireccion<>'' then
  87.       begin
  88.       //si tienen un aplicacion intraweb esto hara que el reporte en html se haga visible en la maquina desde la cual se accede la aplicacion intraweb
  89.       strDireccion := 'http://'+UserSession.ptrServidor.strHost+':'+UserSession.ptrServidor.strPuertoHTTPConsulta+'/Files/Temp/'+strDireccion;
  90.       WebApplication.NewWindow(strDireccion, '', -1, -1,[woStatusBar, woScrollBars, woResizable], -1, -1);
  91.       end
  92.   end
  93. else
  94.     WebApplication.ShowMessage('No existen datos historicos para generar el reporte');




y este es el archivo .html que cree por codigo y del cual tome las lineas para generar mi archivo como queria


<TABLE BGCOLOR="white" WIDTH = "100%"  CELLPADDING="2" CELLSPACING="0" BORDER ="1" BORDERCOLOR="black">
  <colgroup>
            <col ALIGN="center">
            </colgroup>
<TR><TD BGCOLOR="yellow" >REPORTE HISTORICO DE TURNO</TD></TR>
<TR><TD BGCOLOR="lime" ALIGN="left" STYLE="font-size:8pt;">INFORMACION DE TURNO</TD></TR>
<TR><TD><TABLE WIDTH = "100%" HEIGHT="25" CELLPADDING="0" CELLSPACING="0" BORDER ="1" BORDERCOLOR="black" STYLE="font-size:8pt;" >
          <colgroup>
          <col WIDTH = "10%" STYLE="background-color:yellow;text-align:left;">
          <col >
        </colgroup>
          <TR><TH>Fecha</TH><TD>celda(1,1)</TD></TR>
          <TR><TH>Oficina</TH><TD>Celda(1,1)</TD></TR>
          <TR><TH>Sala</TH><TD>&nbsp</TD></TR>
          <TR><TH>Turno</TH><TD>celda(1,1)</TD></TR>
        </TABLE>
</TD></TR>
 
<TR><TD><TABLE WIDTH = "100%" HEIGHT="25" CELLPADDING="0" CELLSPACING="0" BORDER ="1" BORDERCOLOR="black" STYLE="font-size:8pt;">
        <colgroup>
          <TD BGCOLOR="yellow" COLSPAN="3" ALIGN="center">INFORMACION ADICIONAL</TD>         
        </colgroup>
          <TR BGCOLOR="lime"><TH>celda(1,1)</TH><TH>celda(1,2)</TH><TH>celda(1,3)</TH></TR>
          <TR><TD ALIGN="left">celda(2,1)</TD><TD ALIGN="center">celda(2,2)</TD><TD ALIGN="left">celda(2,3)</TD></TR>
          <TR><TD ALIGN="center">celda(2,1)</TD><TD ALIGN="left">celda(2,2)</TD><TD ALIGN="center">celda(2,3)</TD></TR>
          <TR><TD ALIGN="left">celda(2,1)</TD><TD ALIGN="center">celda(2,2)</TD><TD ALIGN="left">celda(2,3)</TD></TR>
        </TABLE>
</TD></TR>

<TR><TD><TABLE WIDTH = "100%" HEIGHT="25" CELLPADDING="0" CELLSPACING="0" BORDER ="1" BORDERCOLOR="black" STYLE="font-size:8pt;" >
          <colgroup>
          <col WIDTH = "10%" STYLE="background-color:yellow;text-align:left;">
          <col >
        </colgroup>
          <TR><TH>Fecha</TH><TD>celda(1,1)</TD></TR>
          <TR><TH>Oficina</TH><TD>Celda(1,1)</TD></TR>
          <TR><TH>Sala</TH><TD>&nbsp</TD></TR>
          <TR><TH>Turno</TH><TD>celda(1,1)</TD></TR>
        </TABLE>
</TD></TR>
 
<TR><TD><TABLE WIDTH = "100%" HEIGHT="25" CELLPADDING="0" CELLSPACING="0" BORDER ="1" BORDERCOLOR="black" STYLE="font-size:8pt;">
        <colgroup>
          <TD BGCOLOR="yellow" COLSPAN="3" ALIGN="center">INFORMACION ADICIONAL</TD>         
        </colgroup>
          <TR BGCOLOR="lime"><TH>celda(1,1)</TH><TH>celda(1,2)</TH><TH>celda(1,3)</TH></TR>
          <TR><TD ALIGN="left">celda(2,1)</TD><TD ALIGN="center">celda(2,2)</TD><TD ALIGN="left">celda(2,3)</TD></TR>
          <TR><TD ALIGN="center">celda(2,1)</TD><TD ALIGN="left">celda(2,2)</TD><TD ALIGN="center">celda(2,3)</TD></TR>
          <TR><TD ALIGN="left">celda(2,1)</TD><TD ALIGN="center">celda(2,2)</TD><TD ALIGN="left">celda(2,3)</TD></TR>
        </TABLE>
</TD></TR>
</TABLE>



Y cmo toca ser agradecido, en este link me hice el cursito de creacion de tablas..  :angel:
http://foro.univisio...6#axzz1qBQLtfBf

Bueno espero haberles aportado alguito.. la verdad esta vaina casi me vuelve loco  :tongue:  :cheesy:  :cool:


  • 0




IP.Board spam blocked by CleanTalk.