delphi – Importing data from a TXT to a StringGrid


I have a stringgrid , and I need to import a txt file into it, but I need to get all the lines according to their positions, and I have no idea how to do it, I did searches, but without success.

I need this type of code to tell me how to compare two stringgrids with a DLL of a fiscal printer, and I have to copy these columns respectively according to the position of each one.


I'm not very much in favor of giving the fish to everyone, so I'll try to teach you how to fish:

We have the following text file, called test.txt ( which must be in the same folder as the executable, to facilitate our testing ):

COL1      COL2      COL3      
DADO1     DADO2     DADO3     
TESTE1    TESTE2    TESTE3    

Note that line 123456789012345678901234567890 should not go to the text file, consider it a ruler, assuming the TXT you are working on is typed.

Since your txt is small, we will use a StringList to capture its data as follows:

procedure TForm1.Button1Click(Sender: TObject);
  arquivo : TStringList;
  i : integer;
  arquivo := TStringList.Create;

Now your file is all in the StringList File and is ready to be compared to any value as follows:

  for i := 0 to arquivo.Count - 1 do
    Showmessage('Primeiro campo: ' + copy(arquivo[i],1,10));   //Primeiro Campo
    Showmessage('Segundo campo: ' + copy(arquivo[i],11,10));  //Segundo Campo
    Showmessage('Terceiro campo: ' + copy(arquivo[i],21,10));  //Terceiro Campo

From there, it is up to you to insert it into the StringGrid , validate and use the DLL from Bematech, Daruma or whatever the manufacturer. The loop created will loop through the entire StringList . Remembering that it starts at 0 and goes to arquivo.count - 1 by the TStringList index if it starts at 0 and not 1.

Scroll to Top