Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Varios
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 22-06-2015
wolfran_hack wolfran_hack is offline
Miembro
 
Registrado: abr 2013
Posts: 97
Poder: 12
wolfran_hack Va por buen camino
problema al Guardar Archivo de ListView en Delphi

Hola a todos, estoy creando un pequeño soft que tiene una lista con varios datos de alumnos, estos datos se van modificando y creo un archivo especial ejemplo "NombredelArchivo.XXX" donde voy guardando los cambios y lo uso como base de datos temporal del programa, el problema que estoy teniendo es el siguiente:

tengo varios botones, uno para agregar un nuevo alumno, otro para editar una linea, otro para eliminar y otro para cambiar un valor y colorear una linea de la lista. Entonces utilizo el siguiente codigo para en cada cambio guardar en el fichero el registro nuevo:

Código Delphi [-]
procedure TForm1.SaveListViewToFile(AListView: TListView; sFileName: string);
var
  idxItem, idxSub, IdxImage: Integer;
  F: TFileStream;
  pText: PChar;
  sText: string;
  W, ItemCount, SubCount: Word;
  MySignature: array [0..2] of Char;
begin
  // Inicio
  with ListView1 do
  begin
    ItemCount := 0;
    SubCount  := 0;
    //****
    MySignature := 'LVF';
    //  ListViewFile
    F := TFileStream.Create('NombredelArchivo.XXX', fmCreate or fmOpenWrite);
    F.Write(MySignature, SizeOf(MySignature));
 
    if Items.Count = 0 then
      // List is empty
      ItemCount := 0
    else
      ItemCount := Items.Count;
    F.Write(ItemCount, SizeOf(ItemCount));
 
    if Items.Count > 0 then
    begin
      for idxItem := 1 to ItemCount do
      begin
        with Items[idxItem - 1] do
        begin
          // Guardamos los SubItems
          if SubItems.Count = 0 then
            SubCount := 0
          else
            SubCount := Subitems.Count;
          F.Write(SubCount, SizeOf(SubCount));
          // Guardamos el Index
          IdxImage := ImageIndex;
          F.Write(IdxImage, SizeOf(IdxImage));
          // Guardamos la Caption
          sText := Caption;
          w     := Length(sText);
          pText := StrAlloc(Length(sText) + 1);
          StrPLCopy(pText, sText, Length(sText));
          F.Write(w, SizeOf(w));
          F.Write(pText^, w);
          StrDispose(pText);
          if SubCount > 0 then
          begin
            for idxSub := 0 to SubItems.Count - 1 do
            begin
              // Guardamos los Items y SubItems
              sText := SubItems[idxSub];
              w     := Length(sText);
              pText := StrAlloc(Length(sText) + 1);
              StrPLCopy(pText, sText, Length(sText));
              F.Write(w, SizeOf(w));
              F.Write(pText^, w);
              StrDispose(pText);
            end;
          end;
        end;
      end;
    end;
    F.Free;
  end;
end;

Código Delphi [-]
procedure TForm1.Button1Click(Sender: TObject);
begin
  // Guardamos los items
  SaveListViewToFile(ListView1, 'NombredelArchivo.XXX');
end;

Con esto esta perfecto y funciona en todos los botones, ahora agregue varias opciones para cargar en la lista como, cargar archivo csv, xml, etc... código:

Código Delphi [-]
procedure ListViewFromCSV(
          theListView: TListView;
          const FileName: String);
var item: TListItem;
    index,
    comPos,
    subIndex: Integer;
    theFile: TStringList;
    Line: String;
begin
     theFile := TStringList.Create;
     theFile.LoadFromFile(FileName);
     for index := 0 to theFile.Count -1 do begin
         Line := theFile[index];
         item := theListView.Items.Add;
         comPos := Pos(';', Line);
         item.Caption := Copy(Line, 1, comPos -1);
         Delete(Line, 1, comPos);
         comPos := Pos(';', Line);
         while comPos > 0 do begin
               item.SubItems.Add(Copy(Line, 1, comPos -1));
               Delete(Line, 1, comPos);
               comPos := Pos(';', Line);
         end;
         item.SubItems.Add(Line);
     end;
     FreeAndNil(theFile);
end;

Código Delphi [-]
  OpenDialog1.Title := 'Importar Archivo *.CSV';
  OpenDialog1.InitialDir := GetCurrentDir;
  OpenDialog1.Filter := 'CSV (Formato de texto separado por comas) (*.csv)|*.csv';
  OpenDialog1.DefaultExt := 'csv';
     // Limpiamos por completo la Lista.
     ListView1.Clear;
     // Se abre un dialogo para abrir un archivo *.CSV
     if OpenDialog1.Execute then
     begin
        ListViewFromCSV(ListView1, OpenDialog1.FileName);

perfecto, se cargar el archivo todo sin problemas. Ahora el problema esta en que luego de este código coloque el:

Código Delphi [-]
SaveListViewToFile(ListView1, 'NombredelArchivo.XXX');

y no se guarda, y luego de esto los botones tampoco guardar nada en el NombredelArchivo.XXX, tampoco recibo ningún error, también probé de colocar el código directo:

Código Delphi [-]
procedure TForm1.CargarArchivoCSV1Click(Sender: TObject);
var
  idxItem, idxSub, IdxImage: Integer;
  Fe: TFileStream;
  pText: PChar;
  sText: string;
  //sFileName: string;
  W, ItemCount, SubCount: Word;
  MySignature: array [0..2] of Char;
begin
  OpenDialog1.Title := 'Importar Archivo *.CSV';
  OpenDialog1.InitialDir := GetCurrentDir;
  OpenDialog1.Filter := 'CSV (Formato de texto separado por comas) (*.csv)|*.csv';
  OpenDialog1.DefaultExt := 'csv';
     // Limpiamos por completo la Lista.
     ListView1.Clear;
     // Se abre un dialogo para abrir un archivo *.CSV
     if OpenDialog1.Execute then
     begin
        ListViewFromCSV(ListView1, OpenDialog1.FileName);
  //Initialization
  with ListView1 do
  begin
    ItemCount := 0;
    SubCount  := 0;
    //****
    MySignature := 'LVF';
    //  ListViewFile
    Fe := TFileStream.Create('NombredelArchivo.XXX', fmCreate or fmOpenWrite);
    Fe.Write(MySignature, SizeOf(MySignature));

    if Items.Count = 0 then
      // List is empty
      ItemCount := 0
    else
      ItemCount := Items.Count;
    Fe.Write(ItemCount, SizeOf(ItemCount));

    if Items.Count > 0 then
    begin
      for idxItem := 1 to ItemCount do
      begin
        with Items[idxItem - 1] do
        begin
          //Save subitems count
          if SubItems.Count = 0 then
            SubCount := 0
          else
            SubCount := Subitems.Count;
          Fe.Write(SubCount, SizeOf(SubCount));
          //Save ImageIndex
          IdxImage := ImageIndex;
          Fe.Write(IdxImage, SizeOf(IdxImage));
          //Save Caption
          sText := Caption;
          w     := Length(sText);
          pText := StrAlloc(Length(sText) + 1);
          StrPLCopy(pText, sText, Length(sText));
          Fe.Write(w, SizeOf(w));
          Fe.Write(pText^, w);
          StrDispose(pText);
          if SubCount > 0 then
          begin
            for idxSub := 0 to SubItems.Count - 1 do
            begin
              //Save Item's subitems
              sText := SubItems[idxSub];
              w     := Length(sText);
              pText := StrAlloc(Length(sText) + 1);
              StrPLCopy(pText, sText, Length(sText));
              Fe.Write(w, SizeOf(w));
              Fe.Write(pText^, w);
              StrDispose(pText);
            end;
          end;
        end;
      end;
    end;
    Fe.Free;
  end;
  end;
end;

luego deje todo como estaba pero sin guardar, que solo cargue el archivo y cree un FormOnCloseQuery y pense que si después de hacer varios cambios, cierro el programa y se guardaría el archivo con los datos:

Código Delphi [-]
procedure TForm1.FormOnCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
canClose:=False;
if Application.MessageBox('¿Desea salir?','Atención',mb_OkCancel + mb_IconQuestion)= idOk then begin
  SaveListViewToFile(ListView1, 'NombredelArchivo.XXX');
  canClose:=True;
end
else begin
  canClose := False;
end;
end;

Pero no, tampoco se guardan, ni recibo ningún error, por ende estoy mareado y sin saber que hacer, ahora cierro el programa y lo abro nuevamente y los botones guardar la info cargada o modificada, pero si pruebo de agregar un archivo, nada. Lo que creo es lo siguiente, que por algún motivo el archivo en algún caso no se cierra al momento de cárgalo y como el programa quiere reescribir el archivo no puede porque ya esta abierto en otro proceso interno.. o algo así. Ustedes que opinan?
Responder Con Cita
  #2  
Antiguo 22-06-2015
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Poder: 36
ecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to behold
Hola wolfran_hack.

De este modo me funciona correctamente.
Código Delphi [-]
procedure SaveListView(LV: TListView; const aFileName, Signature: string);
var
  i, j, aux: Integer;
  Stream: TStream;
  str: string;
  tmp: Word;
begin
  Stream := TFileStream.Create(aFileName, fmCreate or fmOpenWrite);
  try
    // guardar firma
    aux    := Length(Signature);
    Stream.Write(aux, SizeOf(aux));
    Stream.Write(PChar(Signature)^, aux);
    // guardar nro items
    tmp := LV.Items.Count;
    Stream.Write(tmp, SizeOf(tmp));
    if LV.Items.Count > 0 then
    begin
      for i := 0 to LV.Items.Count-1 do
      begin
        // Caption
        str := LV.Items[i].Caption;
        aux := Length(str);
        Stream.Write(aux, SizeOf(aux));
        Stream.Write(PChar(str)^, aux);
        // ImageIndex
        aux := LV.Items[i].ImageIndex;
        Stream.Write(aux, SizeOf(aux));
        // SubItems
        if LV.Items[i].Subitems.Count > 0 then
        begin
          tmp := LV.Items[i].Subitems.Count;
          Stream.Write(tmp, SizeOf(tmp));
          for j := 0 to LV.Items[i].Subitems.Count - 1 do
          begin
            // Caption
            str := LV.Items[i].SubItems[j];
            aux := Length(str);
            Stream.Write(aux, SizeOf(aux));
            Stream.Write(PChar(str)^, aux);
            // ImageIndex
            aux := LV.Items[i].SubItemImages[j];
            Stream.Write(aux, SizeOf(aux));
          end;
        end;
      end;
    end;
  finally
    Stream.Free;
  end;
end;

procedure LoadListView(LV: TListView; const aFileName, Signature: string);
var
  Stream: TFileStream;
  i, j, aux: Integer;
  ItemCount, SubCount: Word;
  str : string;
  it: TListItem;
begin
  // verificar existencia
  if not FileExists(aFileName) then
    raise Exception.Create(Format('No se encuentra el archivo %s',[aFileName]));
  Stream := TFileStream.Create(aFileName, fmOpenRead);
  try
    // leer firma
    Stream.Read(aux, SizeOf(aux));
    SetLength(str, aux);
    Stream.Read(PChar(str)^, aux);
    // verificar firma
    if str <> Signature then
      raise Exception.Create(Format('%s no es el archivo correcto',[aFileName]));
    // leer nro items
    Stream.Read(ItemCount, SizeOf(ItemCount));
    LV.Items.Clear;
    for i := 0 to ItemCount-1 do
    begin
      it:= LV.Items.Add;
      // Caption
      Stream.Read(aux, SizeOf(aux));
      SetLength(str, aux);
      Stream.Read(PChar(str)^, aux);
      it.Caption:= str;
      // ImageIndex
      Stream.Read(aux, SizeOf(aux));
      it.ImageIndex := aux;
      // SubItems
      Stream.Read(SubCount, SizeOf(SubCount));
      if SubCount > 0 then
      begin
        for j := 0 to SubCount-1 do
        begin
          // Caption
          Stream.Read(aux, SizeOf(aux));
          SetLength(str, aux);
          Stream.Read(PChar(str)^, aux);
          it.SubItems.Add(str);
          // ImageIndex
          Stream.Read(aux, SizeOf(aux));
          it.SubItemImages[j]:= aux;
        end;
      end;
    end;
  finally
    Stream.Free;
  end;
end;

Ejemplo de uso:
Código Delphi [-]
// Guardar
procedure TForm1.btnSaveClick(Sender: TObject);
const
  MSG = '¿ Desea sobreescribir el archivo %s ?';
begin
  with SaveDialog1 do
  begin
    Title       := 'Guarda su ListView como archivo CSV';
    InitialDir  := GetCurrentDir;
    Filter      := 'CSV file|*.CSV';
    DefaultExt  := 'CSV';
    FilterIndex := 1;
    if Execute then
    begin
      if FileExists(FileName) then
        if MessageDlg(Format(MSG,[FileName]), mtConfirmation,[mbYes,mbNo],0) = mrNo then
          Abort;
      SaveListView(ListView1, FileName, ListView1.Name);
      ShowMessage('Guardado con éxito');
    end;
  end;
end;

// Cargar
procedure TForm1.btnLoadClick(Sender: TObject);
begin
  with OpenDialog1 do
  begin
    Initialdir  := GetCurrentDir;
    Options     := [ofFileMustExist];
    Filter      := 'CSV file|*.CSV';
    DefaultExt  := 'CSV';
    FilterIndex := 1;
    if Execute then
      LoadListView(ListView1, FileName, ListView1.Name);
  end;
end;

Saludos
__________________
Daniel Didriksen

Guía de estilo - Uso de las etiquetas - La otra guía de estilo ....
Responder Con Cita
  #3  
Antiguo 22-06-2015
wolfran_hack wolfran_hack is offline
Miembro
 
Registrado: abr 2013
Posts: 97
Poder: 12
wolfran_hack Va por buen camino
al cargar me da:

Código:
 nombre.csv no es el archivo correcto
y estoy cargando un archivo csv creado con excel ejemplo:

Código:
nombre;apellido;curso;dato1;dato2;dato3;etc
nombre;apellido;curso;dato1;dato2;dato3;etc
nombre;apellido;curso;dato1;dato2;dato3;etc
nombre;apellido;curso;dato1;dato2;dato3;etc

Última edición por wolfran_hack fecha: 22-06-2015 a las 16:45:21.
Responder Con Cita
  #4  
Antiguo 22-06-2015
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Poder: 36
ecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to behold
Hola.

Revisa la firma que le estas enviando como argumento al parámetro Signature, en el ejemplo le envío el nombre del control (vg. 'ListView1').
Pero tenes que adaptarlo a tu caso, que por lo que ví en tu códig es 'LVF' y está definido en la línea:
Código Delphi [-]
 MySignature := 'LVF';

Otro detalle: Si intentas cargar un archivo que hayas guardado con tu código anterior, debes cambiar el órden en que guardas las propiedades de los Items. En tu código se guarda primero ImageIndex y luego Caption y en el ejemplo que te puse es al revés.

Saludos
__________________
Daniel Didriksen

Guía de estilo - Uso de las etiquetas - La otra guía de estilo ....
Responder Con Cita
  #5  
Antiguo 22-06-2015
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Poder: 36
ecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to behold
Hola de nuevo.

Una cosa que no mencionaste en tu primer mensaje y que olvidé comentar en mi último: No uso Excel y desconozco si usa alguna firma (signature) y si la usa cual es.

El código de ejemplo que te puse, guarda y recupera los items y subitems de un TListView correctamente, pero no sé si funcionará del mismo modo con un archivo generado por Excel...

Saludos
__________________
Daniel Didriksen

Guía de estilo - Uso de las etiquetas - La otra guía de estilo ....
Responder Con Cita
  #6  
Antiguo 22-06-2015
wolfran_hack wolfran_hack is offline
Miembro
 
Registrado: abr 2013
Posts: 97
Poder: 12
wolfran_hack Va por buen camino
Perdón me confundí con el que es para cargar archivo csv, entonces por eso me daba error, pero tampoco funciona, puse en el OnCloseQuery:

Código Delphi [-]
procedure TForm1.FormOnCloseQuery(Sender: TObject; var CanClose: Boolean);
begin

canClose:=False;
if Application.MessageBox('¿Desea salir?','Atención',mb_OkCancel + mb_IconQuestion)= idOk then begin
  SaveListView(ListView1, 'archivo.database', ListView1.Name);
  canClose:=True;
end
else begin
  canClose := False;
end;
end;

y en el dialogo al cargar el csv:

Código Delphi [-]
if OpenDialog1.Execute then
    begin
        ListViewFromCSV(ListView1, OpenDialog1.FileName);
        // Se Actualiza la Barra de Estado con la cantidad de Números cargados.
        StatusBar1.Panels[1].Text := Format('Cliente(s): %d ',[ListView1.Items.Count]);
        // Se crea un registro en el Log sobre el tema.
        Memo3.Lines.Add( formatdatetime('dd/mm/yy | hh:nn:ss',now) + ' ; ' + ' Se cargo el archivo: ' + OpenDialog1.FileName + '.')
    end;
    SaveListView(ListView1, 'archivo.database', ListView1.Name);

y no guarda nada ni crea el archivo.
Responder Con Cita
  #7  
Antiguo 22-06-2015
wolfran_hack wolfran_hack is offline
Miembro
 
Registrado: abr 2013
Posts: 97
Poder: 12
wolfran_hack Va por buen camino
ahora probe de agregar unos datos y cerrar y se guardar el archivo con tu código, por ende lo que creo es que el error debe estar entre:

Código Delphi [-]
(* this procedure loads the content of a CSV file *)
(* to a TListView      *)
procedure ListViewFromCSV(
          theListView: TListView;
          const FileName: String);
var item: TListItem;
    index,
    comPos,
    subIndex: Integer;
    theFile: TStringList;
    Line: String;
begin
     theFile := TStringList.Create;
     theFile.LoadFromFile(FileName);
     for index := 0 to theFile.Count -1 do begin
         Line := theFile[index];
         item := theListView.Items.Add;
         comPos := Pos(';', Line);
         item.Caption := Copy(Line, 1, comPos -1);
         Delete(Line, 1, comPos);

         comPos := Pos(';', Line);

         while comPos > 0 do begin
               item.SubItems.Add(Copy(Line, 1, comPos -1));
               Delete(Line, 1, comPos);
               comPos := Pos(';', Line);
         end;

         item.SubItems.Add(Line);
     end;

     FreeAndNil(theFile);
end;

y

Código Delphi [-]
procedure TForm1.CargarArchivoCSV1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
    begin
        ListViewFromCSV(ListView1, OpenDialog1.FileName);
    end;
    SaveListView(ListView1, 'archivo.database', ListView1.Name);
end;
Responder Con Cita
  #8  
Antiguo 22-06-2015
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Poder: 36
ecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to behold
Hola wolfran_hack.

Como te dije no uso Excel, pero supongo que debe ser similar a Calc de LibreOffice, así que proba si de este modo te funciona:
Código Delphi [-]
procedure LoadFromCSV(LV: TListView; const aFileName: string);
var
  LI : TListItem;
  TS1, TS2: TStrings;
  i, j: Integer;
begin
  TS1 := TStringList.Create;
  TS2 := TStringList.Create;
  try
    TS1.LoadFromFile(aFileName);
    // Columnas
    TS2.CommaText := TS1[0];
    for i := 0 to TS2.Count-1 do
    begin
      LV.Columns.Add;
      LV.Columns.Items[i].Caption := TS2[i];
      LV.Columns[i].Width:= 100; // esta línea es opcional
    end;
    // Items, Subitems
    for i := 1 to TS1.Count-1 do
    begin
      TS2.Clear;
      TS2.CommaText := TS1[i];
      LI := LV.Items.Add;
      LI.Caption := TS2[0];
      for j:= 1 to TS2.Count - 1 do
        LI.SubItems.Add(TS2[j]);
    end;
  finally
    TS1.Free;
    TS2.Free;
  end;
end;

Ejemplo de uso:
Código Delphi [-]
  LoadFromCSV(ListView1, ExtractFilePath(Application.ExeName)+'Archivo.csv');
El procedimiento carga un archivo .csv a un TListView en blanco.

Por si pudiera serte de ayuda, te adjunto la prueba con el archivito .csv que generé desde el Calc.

Saludos
Archivos Adjuntos
Tipo de Archivo: zip LoadFromCSV.zip (3,9 KB, 9 visitas)
__________________
Daniel Didriksen

Guía de estilo - Uso de las etiquetas - La otra guía de estilo ....
Responder Con Cita
  #9  
Antiguo 22-06-2015
wolfran_hack wolfran_hack is offline
Miembro
 
Registrado: abr 2013
Posts: 97
Poder: 12
wolfran_hack Va por buen camino
Gracias ecfisa, probé con tu condigo y si lo guarda, por ende algo en las 2000 lineas de mi programa esta impidiendo que se guarde el archivo, correctamente, revisare todo y comento.
Responder Con Cita
  #10  
Antiguo 23-06-2015
wolfran_hack wolfran_hack is offline
Miembro
 
Registrado: abr 2013
Posts: 97
Poder: 12
wolfran_hack Va por buen camino
Encontré el error, pero no lo solucione todavía, el problema esta en el OpenDialog, como no funcionaba con el CSV busque para hacerlo con un archivo XML, funciona perfecto lo carga, todo ahora, no se guardaba, y de la nada encuentro un archivo "NombredelArchivo.XXX" que yo queria crear para usarlo de archivo temporal, se creo en el mismo directorio que donde tenia el XML, se esta quedando el directorio del Opendialog con información, que por este motivo cuando hago algún cambio si se guarda si no utilizo un OpenDialog.
Archivos Adjuntos
Tipo de Archivo: rar ImportXML.rar (249,1 KB, 0 visitas)
Responder Con Cita
  #11  
Antiguo 23-06-2015
wolfran_hack wolfran_hack is offline
Miembro
 
Registrado: abr 2013
Posts: 97
Poder: 12
wolfran_hack Va por buen camino
Ahora digo, creo un botón al cual le pongo:

Código Delphi [-]
  SaveListViewToFile(ListView1, ExtractFilePath( Application.ExeName ) + 'NombredelArchivo.XXX');
  MessageDlg(ExtractFilePath( Application.ExeName ),mtInformation,[mbOK], 0);

entonces, cargo el archivo XML, todo perfecto, entonces digo ya cargue el XML y ahora si apretó el botón, tendría que escribir o reemplazar o crear el archivo NombredelArchivo.XXX en el directorio del EXE, pero no lo hace, por mas que le indico que tiene que utilizar el ExtractFilePath( Application.ExeName ), el archivo se crear en el directorio donde esta el XML.
Responder Con Cita
  #12  
Antiguo 23-06-2015
wolfran_hack wolfran_hack is offline
Miembro
 
Registrado: abr 2013
Posts: 97
Poder: 12
wolfran_hack Va por buen camino
Solucionado:

Código Delphi [-]
procedure SaveListViewToFile(AListView: TListView; sFileName: string);
var
  idxItem, idxSub, IdxImage: Integer;
  F: TFileStream;
  pText: PChar;
  sText: string;
  W, ItemCount, SubCount: Word;
  MySignature: array [0..2] of Char;
begin
  // Inicio
  with AListView do
  begin
    ItemCount := 0;
    SubCount  := 0;
    //****
    MySignature := 'LVF';
    //  ListViewFile
    F := TFileStream.Create( ExtractFilePath( Application.ExeName ) + 'NombredelArchivo.XXX', fmCreate or fmOpenWrite);
    F.Write(MySignature, SizeOf(MySignature));

    if Items.Count = 0 then
      // List is empty
      ItemCount := 0
    else
      ItemCount := Items.Count;
    F.Write(ItemCount, SizeOf(ItemCount));

    if Items.Count > 0 then
    begin
      for idxItem := 1 to ItemCount do
      begin
        with Items[idxItem - 1] do
        begin
          // Guardamos los SubItems
          if SubItems.Count = 0 then
            SubCount := 0
          else
            SubCount := Subitems.Count;
          F.Write(SubCount, SizeOf(SubCount));
          // Guardamos el Index
          IdxImage := ImageIndex;
          F.Write(IdxImage, SizeOf(IdxImage));
          // Guardamos la Caption
          sText := Caption;
          w     := Length(sText);
          pText := StrAlloc(Length(sText) + 1);
          StrPLCopy(pText, sText, Length(sText));
          F.Write(w, SizeOf(w));
          F.Write(pText^, w);
          StrDispose(pText);
          if SubCount > 0 then
          begin
            for idxSub := 0 to SubItems.Count - 1 do
            begin
              // Guardamos los Items y SubItems
              sText := SubItems[idxSub];
              w     := Length(sText);
              pText := StrAlloc(Length(sText) + 1);
              StrPLCopy(pText, sText, Length(sText));
              F.Write(w, SizeOf(w));
              F.Write(pText^, w);
              StrDispose(pText);
            end;
          end;
        end;
      end;
    end;
    F.Free;
  end;
end;
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
problemas al guardar listview demonio6 Varios 16 11-11-2012 05:35:52
Guardar archivo en delphi Maria85 Internet 1 04-02-2009 13:12:40
Guardar archivo excel desde delphi rruffino Servers 7 01-02-2008 18:20:32
Subir y guardar archivo PDF con Delphi fausto Internet 1 28-06-2006 10:20:54
Problema al guardar archivo adjunto IceJamp Internet 3 04-04-2006 19:21:45


La franja horaria es GMT +2. Ahora son las 23:03:17.


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
Copyright 1996-2007 Club Delphi