Recent

Author Topic: StringGrid.SaveToFile creating XML files rather than simple CSV files  (Read 28641 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Hi

I have a StringGrid with two columns of data. I'm using

StringGrid1.SaveToFile(MyFile.txt);

to output the content of the grid to a file. But rather than the MyFile.txt file being a simple deliminated file of two values seperated by a ',', it's an XML file. I just want a simple text file with the following layout : Column1Content,Column2Content

Am I doing something wrong with SaveToFile or is that it's only mode?

Ted

IPguy

  • Sr. Member
  • ****
  • Posts: 385
Re: StringGrid.SaveToFile creating XML files rather than simple CSV files
« Reply #1 on: August 13, 2011, 03:44:38 am »
FYI - I just found this article titled:

...save a StringGrid as a CSV file?

http://www.swissdelphicenter.ch/en/showcode.php?id=1743

IPguy

  • Sr. Member
  • ****
  • Posts: 385
Re: StringGrid.SaveToFile creating XML files rather than simple CSV files
« Reply #2 on: August 13, 2011, 03:54:09 am »
Or, you could dump each comma delimited line from the StringGrid into a Memo, then save the memo as a .csv file.

And I can think of a few more ways, but most require that you move away from the .savetofile method.

Did you set the DelimitedText property before calling .savetofile?
"Get or set the list via a big string. This string will have the list strings separated by the Delimiter value (default is a comma). Strings containing embedded blanks must be enclosed in the QuoteChar (default is "). "

Here is another interesting page:
http://www.delphipages.com/forum/archive/index.php/t-153170.html

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: StringGrid.SaveToFile creating XML files rather than simple CSV files
« Reply #3 on: August 14, 2011, 08:32:19 pm »
Hi

I'd read that first link already having Googled the issue before posting, but I thought it seemed a bit tricky overall, given my aims. I often find out there's an easier way to code something, having spent ages trying to do it another way. So I hoped I might be missing the obvious.

I guess what I don't quite understand is that, if I can just Ctrl + C all the cells of my two column StringGrid that is displayed in my program and then paste that into Excel (which I can do currently), the values are obviously stored in memory like that - i.e. two columns of data with lots of rows. So why, when I want to export that same memory resident data to a file, do I end up with an XML file? The SaveToFile procedure is obviously converting my simple data from a form that I actually want it in and already have it in, into a form that I don't want it in. So is there not a way to just ask SaveToFile to "don't convert to xml?"

« Last Edit: August 14, 2011, 08:35:01 pm by tedsmith »

mas steindorff

  • Hero Member
  • *****
  • Posts: 553
Re: StringGrid.SaveToFile creating XML files rather than simple CSV files
« Reply #4 on: August 14, 2011, 10:20:48 pm »
You have set something  different from standard install or how you import your data into the tstrings.
  I create  CVS with tstrings all of the time.  I was wondering if the tstring  could  create an xml  directly  or if I needed to translate it's contents across. (Like you have)
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: StringGrid.SaveToFile creating XML files rather than simple CSV files
« Reply #5 on: August 14, 2011, 11:23:49 pm »
Hui

Well maybe we can exchange ideas! I have no idea how I have set mine to the non-default. I'm using Lazarus 0.9.30 on Ubuntu 10.04 using FPC 2.4.2. The relevant parts of my code are :

Code: Pascal  [Select][+][-]
  1. ...
  2. var
  3.   SizeOfFile : int64;
  4.   NameOfFileToHash, fileHashValue, PercentageProgress : string;
  5.   FI : TFileIterator;                      //File Iterator class
  6.   SG : TStringGrid;                                                                
  7. begin
  8.   FI := TFileIterator.Create;
  9.   SG := TStringGrid.Create(self);    
  10. ...//Stuff done here
  11.    // StringGrid Elements:
  12.       StringGrid1.rowcount:= FileCounter+1;
  13.       StringGrid1.Cells[0,FileCounter] := IntToStr(FileCounter);
  14.       Stringgrid1.Cells[1,FileCounter] := NameOfFileToHash;
  15.       Stringgrid1.Cells[2,FileCounter] := UpperCase(fileHashValue);          
  16.      Application.ProcessMessages;      
  17. ...
  18. end
  19.  

How do you do things differently to get CSV by default?

Thanks

Ted

mas steindorff

  • Hero Member
  • *****
  • Posts: 553
Re: StringGrid.SaveToFile creating XML files rather than simple CSV files
« Reply #6 on: August 16, 2011, 12:03:17 am »
I think I see the difference.  I was thinking the stringgrid used a TStringlist for storage and "savetofile" but it may be different.  my code looks like this without the try..finally and getdata stuff:

Code: [Select]
...
var tstr:TStringlist;
str:string
begin
tstr := Tstringlist.create();
tstr.add(HeaderString);  //  := 'Var1,Var2,Var3,Var4';
  ...
  loop
   str := format('%d,%d,%d,%d',[v1,v2,v3,v4]);
   tstr.add(str);  // simple
------ or to add another Tstringlist ---
  tstr.addstrings(TstrLineOfData);
----
  endofloop
 tstr.savetofile(CVS_filename);
 tstr.free;
end;
sometimes I use something like..

  str:= timestr + ',' + tstrLine.commatext;
  Tstr.add(str);

and I don't even change the tstr.delimiter or .delinitertext properties and I still get one line per .add() in the final file.

you may be able to transfer a line at a time from the grid to a tstringlist or someone may answer your original question
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: StringGrid.SaveToFile creating XML files rather than simple CSV files
« Reply #7 on: August 20, 2011, 11:48:43 pm »
Well this is defeating me.

I've spent another two hours this evening trying to work it out. There must be an easier way than the many variation I've read about (using AssignFile etc).

Does nobody here export the data of StringGrids, specifically, to files? If so, how are you doing it?

Scoops

  • Full Member
  • ***
  • Posts: 103
Re: StringGrid.SaveToFile creating XML files rather than simple CSV files
« Reply #8 on: August 21, 2011, 01:17:25 am »
This is a quick way that i save my stringgrids, probably not the best way,
or maybe what you want, but it works for me ...

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var col : integer;
var row : integer;
string1 : string;
file1 : TStringlist;
begin

file1 := TStringList.Create;

file1.Sorted := True;
file1.Duplicates := dupIgnore;
file1.CaseSensitive := True;

for row := 0 to stringgrid1.RowCount - 1 do
begin
string1 := '';
  for col := 0 to stringgrid1.ColCount - 1 do
  begin
  string1 := string1 + stringgrid1.Cells [col,row];
  if col < stringgrid1.ColCount - 1 then string1 := string1 + ',';
  end;
file1.Add(string1);
  end;
file1.SaveToFile(ExtractFilePath(Application.ExeName) + 'test.csv');
file1.Free;
end;

Dick, from the internet

  • Full Member
  • ***
  • Posts: 198
Re: StringGrid.SaveToFile creating XML files rather than simple CSV files
« Reply #9 on: August 21, 2011, 02:31:56 am »
Not as elegant as posted by Scoops, but does the job:

Code: [Select]
   var i : integer;
     str : string;
begin
 memo1.Lines.Clear;
  for i := 1 to StringGrid1.RowCount - 1 do
   begin
     str := StringGrid1.Cells[1,i] + ',' + StringGrid1.Cells[2,i];
     memo1.Lines.Add(str);
   end;
  memo1.Lines.SaveToFile('output.csv');
end;
« Last Edit: August 21, 2011, 02:41:47 am by geno »

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: StringGrid.SaveToFile creating XML files rather than simple CSV files
« Reply #10 on: September 03, 2011, 11:05:39 pm »
Deepest thanks to JesusRMX from the IRC forum whoi kindly pointed out that the solution was simple this :

Code: [Select]
StringGrid1.rows[1].savetofile('/home/ted/test.txt');

Apparently, though a StringGrid is a StringGrid, the 'SG.rows' element is actually a StringList within a StringGrid (http://wiki.lazarus.freepascal.org/Grids_Reference_Page#property_Rows.5Bindex:_Integer.5D:_TStrings_read_GetRows_write_SetRows.3B)

Or, you can do this:
Code: [Select]
StringGrid1.Cols[X].savetofile('/home/ted/test.txt');  // where X = your column index, e.g 1, 2 or 3

Now I just need to work out how to get it to fill the whole file with the content of the whole grid, as opposed to just the last row, or last column! It's very frustrating. 

Because this :

Code: [Select]
StringGrid1.Cols[0].savetofile('/home/ted/test.txt');
StringGrid1.Cols[1].savetofile('/home/ted/test.txt');
StringGrid1.Cols[2].savetofile('/home/ted/test.txt');   

Results in the last column (No 2) been in the file, but columns 1 and 0 get wiped out.
« Last Edit: September 04, 2011, 12:39:53 am by tedsmith »

jesusr

  • Sr. Member
  • ****
  • Posts: 496
Re: StringGrid.SaveToFile creating XML files rather than simple CSV files
« Reply #11 on: September 05, 2011, 04:28:11 am »
grid.SaveToCSVFile and grid.LoadFromCSVFile methods were added to StringGrid in Lazarus r32179, they are documented here: http://wiki.lazarus.freepascal.org/Grids_Reference_Page#procedure_SaveToCSVFile.28AFileName:_string.3B_ADelimiter:Char.3D.27.2C.27.3B_WithHeader:boolean.3Dtrue.29.3B

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: StringGrid.SaveToFile creating XML files rather than simple CSV files
« Reply #12 on: September 05, 2011, 06:31:41 am »
savetofile() always starts from beginning of file, you can't use it to continue writing.

Modifying above code a little:
Code: [Select]
var i : integer;
    sl : TStringList;
begin
  sl:=TStringList.Create;
  for i := 1 to StringGrid1.RowCount - 1 do
    sl.Add(StringGrid1.Rows[i].Text);
  sl.SaveToFile('output.csv');
  sl.Free;
end;
Did i miss something? This is completely untested code, but you said Rows[] are TStringList's so this should work.

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: StringGrid.SaveToFile creating XML files rather than simple CSV files
« Reply #13 on: September 05, 2011, 01:34:24 pm »
Thanks Jesus...

User137 - thanks for that. I will give it a try tonight and report back. Yes - as I understand it from the links above, the rows and column elements of a StringGrid are actually 'just' the simpler StringLists. So I will give your code snippet a go. I always got muddled up with for loops when dealing with streams, so thanks for the code.

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: StringGrid.SaveToFile creating XML files rather than simple CSV files
« Reply #14 on: September 05, 2011, 09:19:31 pm »
Thanks man! That has worked. I just need to tweak it a bit to get the final output I want, but yes, that outputs each row of my StringGrid into a file.

Very much appreciated.

 

TinyPortal © 2005-2018