반응형
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Button1: TButton;
Button2: TButton;
Opendialog1: TOpendialog;
SaveDialog1: TSaveDialog;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure LoadToGrid(strgrid: TStringGrid; FileName: String);
var
I, ColumnCount: Integer;
List: TStrings;
begin
ColumnCount := 1;
try
Screen.Cursor := crHourGlass;
List := TStringList.Create;
List.LoadFromFile(FileName);
for i := 0 To Length(List.Strings[0]) do
if List.Strings[0][I] = ',' then Inc(ColumnCount);
if ColumnCount = 0 then
begin
Screen.Cursor := crDefault;
ShowMessage('The file wasn''t a CSV file');
List.Free;
Exit;
end;
with strgrid do
begin
RowCount := List.Count;
ColCount := ColumnCount;
for I := 0 To List.Count - 1 Do
Rows[I].CommaText := List.Strings[I];
end;
finally
Screen.Cursor := crDefault;
List.Free;
end;
end;
procedure SaveGridToFile(strgrid: TStringGrid; FileName: String);
var
GridStrings: TStrings;
I: Integer;
begin
try
GridStrings := TStringList.Create;
with strgrid do
begin
for I := 0 to RowCount - 1 do
GridStrings.Add(Rows[I].CommaText);
end;
GridStrings.SaveToFile(FileName);
finally
GridStrings.Free;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i, j: Integer;
begin
// Column의 title을 만든다
for i := 1 to StringGrid1.ColCount - 1 do
StringGrid1.Cells[i, 0] := Char(Ord('A')+i-1);
// Row의 title을 만든다
for i := 1 to StringGrid1.RowCount - 1 do
StringGrid1.Cells[0, i] := IntToStr(i);;
// 임의의 자료를 만들어서 각 cell에 입력합니다
for i := 1 to StringGrid1.ColCount - 1 do
for j := 1 to StringGrid1.RowCount - 1 do
StringGrid1.Cells[i, j] := Format('%.0n', [i * j * 10000.0]);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if not Opendialog1.Execute then
System.Exit;
SaveGridToFile(StringGrid1, Opendialog1.FileName);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i: Integer;
begin
for i := 0 to pred(StringGrid1.RowCount) do
StringGrid1.Rows[i].Clear;
if not Opendialog1.Execute then
System.Exit;
LoadToGrid(StringGrid1, Opendialog1.FileName);
end;
end.
반응형