post icon

Leer ficheros .csv con Delphi

Me hubiera gustado que el articulo se titule “Importar datos de ficheros CSV desde Firebird” asi como ya escribi sobre MySQL y SQL Server, pero lamentablemente, este motor no puede hacerlo nativamente, motivo que me llevo a crear la importación desde una aplicación existente y escrita en Delphi.

Primeramente busque algunos componentes VCL para hacerlo rápidamente ya que no quería perder tiempo con él, pero entre la documentación que pude leer, es más sencillo hacerlo con los objetos nativos del Delphi mismo.

Así que le agrego un par de modificaciones para subirlo a devtroce y sea más generico que mi código original:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var
  csv : TStringList;
  fila : TStringList;
  i, j : Integer;
begin
	try
		csv := TStringList.Create;
		// cargar a partir del fichero csv
		csv.LoadFromFile(FolderDialog.Directory + 'datos.csv');
 
		fila := TStringList.Create;
 
		ProgressBar.Max := csv.Count -1;
 
		// recorrer las filas
		for i := 0 to csv.Count -1 do
		begin
			ProgressBar.Position := i;
 
			fila.CommaText := csv.Strings[i];
 
			// recorrer las columnas
			for j := 0 to fila.Count -1 do
			begin
				lista.Items.Add(fila[j]);
			end;
		end;
		MessageDlg('Lectura Exitosa..', mtInformation, [mbOK], 0);
	Except
	    on E : Exception do
	    begin
			MessageDlg('Ocurrio un Error: ' + E.Message, mtInformation, [mbOK], 0);
	    end;
	end;
	// liberar la memoria
	csv.Free;
end;

Articulos Relacionados:

2 Comentarios

Deja tu Comentario
  1. Santiago ARGENTINA Opera Windows
    22 abril 2011 at 10:25 #

    Que mas puedo decir, nuevamente mis felicitaciones para el programador.

  2. Santiago ARGENTINA Opera Windows
    19 abril 2011 at 14:50 #

    Genial, me sirvió de mucho.

Responder