Recent

Author Topic: Adicionar o nome da coluna do arquivo csv no ComboBox  (Read 3830 times)

LeoGaucho

  • Newbie
  • Posts: 2
Adicionar o nome da coluna do arquivo csv no ComboBox
« on: March 29, 2016, 11:39:05 pm »
Olá!!

Não está adicionando o nome da última coluna do arquivo csv no ComboBox. Alguma sugestão?

procedure TFMF.Bt_LoadClick(Sender: TObject);
Var
    i,j,Inicol: Integer;
    SVal,Ch : String;
   TxtFile: TextFile;

begin
     OpenF.Filter:= 'CSV file|*.csv|Text file|*.txt';
     OpenF.Title:= 'Abrir arquivo';
     OpenF.FilterIndex:= 1;
     OpenF.Options:=[ofshowhelp,offilemustexist];
     OpenF.Execute;
     FName:=OpenF.FileName;
     AssignFile(TxtFile, FName);
     Reset(TxtFile);
     ReadLn(TxtFile, Ch);
     IniCol:=1;
     for j:=1 to Length(Ch)
     do begin
     if (Copy(Ch,j,1)=',') or (Copy(Ch,j,1)=';') or (Copy(Ch,j,1)='#9') or (Copy(Ch,j,1)='') then begin
        SVal:= Copy(Ch,IniCol,j-IniCol);
        ComboBox1.Items.Add(SVal);
        ComboBox2.Items.Add(SVal);
        ComboBox3.Items.Add(SVal);
        IniCol:=j+1;
     end;
     end;
end;


Obrigado.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Adicionar o nome da coluna do arquivo csv no ComboBox
« Reply #1 on: March 30, 2016, 12:47:21 am »
Sorry, i don't speak portugese. For me, there are quite a few things that don't make any sense in your example but i can't properly explain by using google translate.

Hopefully the code pasted below is able to help you out

Translate:
Desculpe, eu não falo português. Para mim, existem algumas coisas que não fazem nenhum sentido no seu exemplo, mas eu não posso explicar corretamente usando Traduz Google.

Esperemos que o código colado abaixo é capaz de ajudá-lo
Code: Pascal  [Select][+][-]
  1. program testcsv;
  2.  
  3. {$MODE OBJFPC}{$H+}
  4.  
  5. Uses
  6.   StrUtils;
  7.  
  8. procedure SimClick;
  9. var
  10.   TxtFile : TextFile;
  11.   SVal,Ch : String;
  12.  
  13.   FName   : String;
  14.   wCnt    : Integer; // number of textual words
  15.   wIndex  : Integer; // index to textual words
  16. begin
  17.   FName := 'test.csv';
  18.  
  19.   AssignFile(TxtFile, FName);
  20.   Reset(TxtFile);
  21.   ReadLn(TxtFile, Ch);
  22.   // You are required to close the file when you are done reading from it
  23.   CloseFile(TxtFile);
  24.  
  25.   // Retrieve number of textual words (separated by given delimiters) from string
  26.   wCnt := WordCount(Ch, [',' , ';' , #9]);
  27.  
  28.   // For every textual word
  29.   for wIndex := 1 to WCnt do
  30.   begin
  31.     // 'extract' the current textual word with index wIndex from string Ch
  32.     SVal := ExtractWord(wIndex, Ch, [',' , ';' , #9]);
  33.     // Depending on which word(index) was 'extracted', add the actual text of the filedname to the corresponding Combobox
  34.     Case wIndex of
  35.      1 : WriteLn('Add field 1 with name "', SVal, '" to combobox1');  // ComboBox1.Items.Add(SVal);
  36.      2 : WriteLn('Add field 2 with name "', SVal, '" to combobox2');  // ComboBox2.Items.Add(SVal);
  37.      3 : WriteLn('Add field 3 with name "', SVal, '" to combobox3');  // ComboBox3.Items.Add(SVal);
  38.     end;
  39.   end;
  40. end;
  41.  
  42. begin
  43.   SimClick;  
  44. end.
  45.  

test.csv :
Code: [Select]
first field, second field, thirth field, fourth field, fifth field
record 1 field 1, record 1 field 2, record 1 field 3, record 1 field 4, record 1 field 5
record 2 field 1, record 2 field 2, record 2 field 3, record 2 field 4, record 2 field 5
record 3 field 1, record 3 field 2, record 3 field 3, record 3 field 4, record 3 field 5
record 4 field 1, record 4 field 2, record 4 field 3, record 4 field 4, record 4 field 5

Output:
Code: [Select]
Add field 1 with name "first field" to combobox1
Add field 2 with name " second field" to combobox2
Add field 3 with name " thirth field" to combobox3

LeoGaucho

  • Newbie
  • Posts: 2
Re: Adicionar o nome da coluna do arquivo csv no ComboBox
« Reply #2 on: March 30, 2016, 05:10:40 pm »
 Thank you molly. The code you showed helped me in very. Thank you for the help!!!

 

TinyPortal © 2005-2018