Here some simple XML export (could be improved but my time is limted).
It simply exports all columns and cells as plain text. Note that escaping illegal character isn't implemented here. If i have some more time i'll post a DOM based one too.
CODE
procedure TForm1.ExportToXml(const GridView: TGridView; const fn: string);
var
r, c : Integer;
sl : TStringList;
s : string;
function EscapeValue(const value: string): string;
begin
//Not implemented yet.
Result := value;
end;
begin
sl := TStringList.Create;
sl.Add('<?xml version="1.0"?>');
sl.Add(Format('<GridView version = "%s ">', ['2.4.1'])); //Should be stVersion!
sl.Add(' <Columns>');
for c := 0 to Pred(GridView.Columns.Count) do
begin
sl.Add(' <Column>');
sl.Add(' ' + EscapeValue(GridView.Columns[c].Header.Caption));
sl.Add(' </Column>');
end;
sl.Add(' </Columns>');
sl.Add(' <Rows>');
for r := 0 to Pred(GridView.RowCount) do
begin
sl.Add(' <Row>');
for c := 0 to Pred(GridView.Columns.Count) do
begin
sl.Add(' <Cell>');
sl.Add(' ' + EscapeValue(GridView.Cell[c, r].AsString));
sl.Add(' </Cell>');
end;
sl.Add(' </Row>');
end;
sl.Add(' </Rows>');
sl.Add('</GridView>');
sl.SaveToFile(fn);
sl.Free;
end;
var
r, c : Integer;
sl : TStringList;
s : string;
function EscapeValue(const value: string): string;
begin
//Not implemented yet.
Result := value;
end;
begin
sl := TStringList.Create;
sl.Add('<?xml version="1.0"?>');
sl.Add(Format('<GridView version = "%s ">', ['2.4.1'])); //Should be stVersion!
sl.Add(' <Columns>');
for c := 0 to Pred(GridView.Columns.Count) do
begin
sl.Add(' <Column>');
sl.Add(' ' + EscapeValue(GridView.Columns[c].Header.Caption));
sl.Add(' </Column>');
end;
sl.Add(' </Columns>');
sl.Add(' <Rows>');
for r := 0 to Pred(GridView.RowCount) do
begin
sl.Add(' <Row>');
for c := 0 to Pred(GridView.Columns.Count) do
begin
sl.Add(' <Cell>');
sl.Add(' ' + EscapeValue(GridView.Cell[c, r].AsString));
sl.Add(' </Cell>');
end;
sl.Add(' </Row>');
end;
sl.Add(' </Rows>');
sl.Add('</GridView>');
sl.SaveToFile(fn);
sl.Free;
end;
Sample output:
CODE
<?xml version="1.0" ?>
<GridView version="2.4.1">
<Columns>
<Column>Col0</Column>
<Column>Col1</Column>
</Columns>
<Rows>
<Row>
<Cell>0</Cell>
<Cell>0</Cell>
</Row>
<Row>
<Cell>2</Cell>
<Cell>6</Cell>
</Row>
</Rows>
</GridView>
<GridView version="2.4.1">
<Columns>
<Column>Col0</Column>
<Column>Col1</Column>
</Columns>
<Rows>
<Row>
<Cell>0</Cell>
<Cell>0</Cell>
</Row>
<Row>
<Cell>2</Cell>
<Cell>6</Cell>
</Row>
</Rows>
</GridView>
Have fun coding!