Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Impresión (https://www.clubdelphi.com/foros/forumdisplay.php?f=4)
-   -   Imprimir texto en archivo *.txt (https://www.clubdelphi.com/foros/showthread.php?t=83872)

exequielmatias1 09-08-2013 18:15:52

Imprimir texto en archivo *.txt
 
Tengo una ventana con varios edit y un DBMemo, los mismos muestran los datos de un DBGrid conectado a un ADOQuery. Quiero poner un Boton el cual genere un archivo txt en una ubicacion especifica con el texto de los edit y del dbmemo ó con los datos seleccionados del dbgrid.
Para esto encontre el componente SaveDialog, pero este solo me guarda el DBMemo, y no puedo agregar texto de un edit o cualquier texto.
Quisiera saber si existe otro componente o un codigo ya que no encuentro nada :(

movorack 09-08-2013 18:24:34

Hola,

Puedes utilizar un TStringList para ir guardando los textos que requieres (Edits, Memos, Grids, ...) y al final solo le dices donde guardarlo.

ej:

Código Delphi [-]
procedure GuardarEnArchivo;
  var
    StringList : TStringList;
begin
  StringList := TSTringList.Create;
  try
    StringList.Add(Edit1.Text);
    StringList.Add(Memo.Text);
    StringList.Add('Abc...');
    StringList.SaveToFile('C:\MiArchivodetexto.txt');
  finally
    StringList.Free;
  end;
end;

oscarac 09-08-2013 19:02:15

yo hacia algo como esto
Código Delphi [-]
 _FileData := 'Envios\CD' + frmMain._Serie + FormatDateTime('ddmmyyyy', dtFechaIni.Date) + '-' + FormatDateTime('ddmmyyyy', dtFechaFin.Date);
if chbMovimiento.Checked then
  Begin
    StrSql := 'Select C.*, d.* ' +
              'From (tblconsumoc C with (nolock) ' +
              'Left Join tblConsumod D with (nolock) on (C.TDC = D.TDC and C.doc = d.DOC)) ' +
              'Where C.F_DOC Between ' + QuotedStr(FormatDateTime('DD/MM/YYYY', dtFechaIni.Date)) + ' and ' +
              QuotedStr(FormatDateTime('DD/MM/YYYY', dtFechaFin.Date)) +  ' and Isnull(D.TDC, ' + QuotedStr('') + ') <> ' + quotedstr('') +
              ' order by C.TDC, C.DOC';
    qryTemporal.Close;
    qryTemporal.SQL.Clear;
    qryTemporal.sql.Add(StrSql);
    qryTemporal.Open;
    qryTemporal.First; // Por siacaso
  ExportedData := TStringList.Create;
    try
      while not qryTemporal.Eof do
      begin
        Line := '';
        for I := 0 to Pred(qryTemporal.FieldCount) do
          Line := Format('%s%s¦', [Line, qryTemporal.Fields[i].Value]);
        ExportedData.Add(Line);
        qryTemporal.Next;
      end;
      ExportedData.SaveToFile(ExtractFilePath( Application.ExeName ) + _FileData + '.txt')
    finally
      ExportedData.Free
    end
  end;

ecfisa 09-08-2013 23:55:38

Hola exequielmatias1.

Si buscar guardar todos los campos del registro seleccionado actualmente en el TADOQuery, otra opción es:
Código Delphi [-]
procedure SaveAsText(Qry: TADOQuery; const aFileName: TFileName);
var
  i   : Integer;
  str : string;
begin
  with TStringList.Create do
  try
    for i := 0 to Qry.FieldCount-1 do
       str := str + Qry.Fields[i].AsString + ' ';
    SetLength(str, Length(str) -1);
    Text := str;
    SaveToFile(ChangeFileExt(aFileName, '.TXT'));
  finally
    Free;
  end;
end;

Llamada ejemplo:
Código Delphi [-]
procedure TForm1.Button1Click(Sender: TObject);
begin
  if SaveDialog1.Execute then
    SaveAsText(ADOQuery1, SaveDialog1.FileName);
end;

Saludos :)

TiammatMX 10-08-2013 00:27:10

Cita:

Empezado por ecfisa (Mensaje 465297)
...SaveToFile(ChangeFileExt(aFileName, '.TXT'));...

O si vas a guardar un CSV para ser usado en Excel (como es éste caso particular), te recomiendo que lo hagas así:

Código Delphi [-]
   AssignFile(atArchivo, wsNombreArchivo);
   Rewrite(atArchivo);
   Write(atArchivo,wsContenidoCSV);
   CloseFile(atArchivo);

Por que a veces, inyecta "caracteres que pueden ser confusos" y confundidos con cualquier otra cantidad de cosas excepto datos...

santiago14 11-08-2013 01:15:21

Cita:

Empezado por movorack (Mensaje 465259)
Hola,

Puedes utilizar un TStringList para ir guardando los textos que requieres (Edits, Memos, Grids, ...) y al final solo le dices donde guardarlo.

ej:

Código Delphi [-]
procedure GuardarEnArchivo;
  var
    StringList : TStringList;
begin
  StringList := TSTringList.Create;
  try
    StringList.Add(Edit1.Text);
    StringList.Add(Memo.Text);
    StringList.Add('Abc...');
    StringList.SaveToFile('C:\MiArchivodetexto.txt');
  finally
    StringList.Free;
  end;
end;

Me parece que esta opción es buena. Si querés guardar el archivo como .csv cambiamos StringList.SaveToFile('C:\MiArchivodetexto.txt'); por StringList.SaveToFile('C:\MiArchivodetexto.csv');


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

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