Importing data from a TXT to a Stringgrid

Asked

Viewed 2,266 times

3

I have a stringgrid, and I need to import a file txt for him, however I need to take all lines according to positions, and I have no idea how to do, I did research, but without success.

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

  • Welcome to Stack Overflow in Portuguese.To prevent your response from being flagged and consequently closed, I suggest you add what you searched for, if you found anything, what you found, or what you know about Delphi that can help you with this task. Here the community usually reward the effort to ask a good question with great answers, so good luck!

1 answer

4


I’m not really in favor of giving the fish to the guys, so I’m going try to teach you to fish:

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

123456789012345678901234567890
COL1      COL2      COL3      
DADO1     DADO2     DADO3     
TESTE1    TESTE2    TESTE3    

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

Considering your txt is small, we’ll use a StringList to capture his data as follows:

procedure TForm1.Button1Click(Sender: TObject);
var 
  arquivo : TStringList;
  i : integer;
begin
  arquivo := TStringList.Create;
  arquivo.LoadFromFile('teste.txt');
end;

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

  for i := 0 to arquivo.Count - 1 do
  begin
    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
  end;

From there stays with you the insertion in the StringGrid, the validation and use of the DLL from Bematech, Daruma or whatever the manufacturer. The created loop will go through the entire StringList. Remembering that it starts at 0 and goes up arquivo.count - 1 by the index of TStringList start at 0 and not at 1.

  • 2

    Thank you very much, the point is that I am restarting my training in Delhi, and I am not very familiar with the language, but I will solve based on your answer, which by the way was great, now I have an idea where to start, as soon as possible I will post my codes. Obrigado Filipe! ;]

  • @lost Thanks! I’ll use that notation for code from now on.

  • 1

    @Filipe.Fonseca, when the tag Highlight does not catch you put manually, here have a list if you need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.