음..
StringGrid의 내용을 CSV파일로 저장하거나
CSV파일을 StringGrid로 읽어들이는것과 관련해서 종종 Q&A에 올라온다.
그래서 함 만들어 보았다.
@ CSV파일이란?
먼저 CSV파일이란?
"comma-separated values file" 즉 Data가 Comma (,) 로 구분된 파일을 말한다.
만약 value에 Comma가 들아가야 하는 경우엔 value전체를 쌍따옴표(double quotation mark)로 감싸놓는다.
@CommaText 파싱은?
Comma로 구분된 문자열을 손쉽게 파싱하는 방법중 하나는 TStringList를 이용하면 된다.
TStringList *lst=new TStringList;
lst->CommaText = "하나,둘,셋,넷,다~섯";
for(int i=0;iCount;i++)
{
ShowMessage(lst->Strings[i]);
}
delete lst;
위 코드를 실행하면
5번의 메세지가 뜰것이다.
@CommaText 를 만들려면?
여러개의 Value를 CommaText로 만드는 방법에는 여러가지 있을수 있지만..
이또한 StringList를 이용하면 솝쉽게 가능한다.
TStringList *lst=new TStringList;
lst->Add("하나");
lst->Add("둘");
lst->Add("셋");
lst->Add("네 엣");
lst->Add("다아... 섯");
ShowMessage(lst->CommaText); //이렇게 가져오면 된다.
delete lst;
TStringList의 CommaText프로퍼티는
read할때는 lst에 문자열을 묶어서 CommaText로 만들어주고
write할때는 문자열을 파싱해서 짤라준다.
@CSV파일을 StringGrid로 읽으려면..
자 그럼 이제
CSV파일을 StringGrid로 읽어오는 기능을 만들어 보자
위에 TStringList의 CommaText를 잘 이용하면 쉽게 구현이 가능하다.
//---------------------------------------------------------------------------
// CSVFile을 읽어서 StringGrid에 넣는 함수
//---------------------------------------------------------------------------
void __fastcall LoadFromCSVFile(TStringGrid *sGrid,String sCSVFile)
{
if(!FileExists(sCSVFile))return;
TStringList *sLst=new TStringList;
TStringList *sCols=new TStringList;
try
{
sLst->LoadFromFile(sCSVFile);
sGrid->RowCount=sLst->Count;
for(int i=0 ; i < sLst->Count ; i++ )
{
sCols->CommaText = sLst->Strings[i];
if( sCols->Count > sGrid->ColCount )
{
sGrid->ColCount = sCols->Count;
}
sGrid->Rows[i]->Assign(sCols);
}
}
__finally
{
delete sLst;
delete sCols;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
LoadFromCSVFile(StringGrid1,"D:\\test.csv");
}
//---------------------------------------------------------------------------
델파이 버젼
//---------------------------------------------------------------------------
// CSVFile을 읽어서 StringGrid에 넣는 함수
//---------------------------------------------------------------------------
procedure LoadFromCSVFile(sGrid: TStringGrid;sCsvFile:string);
var
i: Integer;
sLst: TStringList;
sCols: TStringList;
begin
if not FileExists(sCSVFile)then
begin
Exit;
end;
sLst:=TStringList.Create;
sCols:=TStringList.Create;
try
begin
sLst.LoadFromFile(sCSVFile);
sGrid.RowCount:=sLst.Count;
for i:=0 to sLst.Count -1 do
begin
sCols.CommaText:=sLst.Strings[i];
if sCols.Count>sGrid.ColCount then
begin
sGrid.ColCount:=sCols.Count;
end;
sGrid.Rows[i].Assign(sCols);
end
end
finally
sLst.Free;
sCols.Free;
end;
end;
//---------------------------------------------------------------------------
procedure TForm6.Button1Click(Sender: TObject);
begin
LoadFromCSVFile(StringGrid1,'D:\test.csv');
end;
@ StringGrid의 내용을 CSV파일로 저장하기
이번에는 반대로 Grid의 내용을 CSV파일로 저장하는 기능을 만들어 보자
//---------------------------------------------------------------------------
// StringGrid의 내용을 CSVFile로 저장하는 함수
//---------------------------------------------------------------------------
void __fastcall SaveToCSVFile(TStringGrid *sGrid,String sCSVFile)
{
TStringList *sLst=new TStringList;
try
{
for(int i=0;i< sGrid->RowCount ; i++)
{
sLst->Add( sGrid->Rows[i]->CommaText );
}
sLst->SaveToFile(sCSVFile);
}
__finally
{
delete sLst;
}
}
//---------------------------------------------------------------------------
// 사용은 이렇게..
void __fastcall TForm1::Button2Click(TObject *Sender)
{
SaveToCSVFile(StringGrid1,"D:\\test.csv");
}
//---------------------------------------------------------------------------
델파이 버젼
//---------------------------------------------------------------------------
// StringGrid의 내용을 CSV파일로 저장하는 함수
//---------------------------------------------------------------------------
procedure SaveToCSVFile(sGrid: TStringGrid;sCsvFile:string);
var
i: Integer;
sLst: TStringList;
begin
sLst:=TStringList.Create;
try
begin
for i:=0 to sGrid.RowCount -1 do
begin
sLst.Add(sGrid.Rows[i].CommaText);
end;
sLst.SaveToFile(sCsvFile);
end
finally
sLst.Free;
end;
end;
//---------------------------------------------------------------------------
// 사용은..
procedure TForm6.Button2Click(Sender: TObject);
begin
SaveToCSVFile(StringGrid1,'D:\test.csv');
end;
그럼...