procedure ExportVirtualStringTreeToExcel(VirtualStringTree : TVirtualStringTree);
var
ExcelApp,
Workbook,
Worksheet,
Range,
Data : OleVariant;
iCol,
iRow,
ColCount,
RowCount : Integer;
FNodeData : PGridTreeData;
FNode : PVirtualNode;
begin
//Verbindung zu Excel herstellen
ExcelApp := CreateOleObject('
Excel.Application');
if not VarIsNull(ExcelApp)
then
begin
//Neues Workbook öffnen
Workbook := ExcelApp.Workbooks.Add;
//Worksheet auswählen
Worksheet := Workbook.ActiveSheet;
if not VarIsNull(Workbook)
then
begin
RowCount := VirtualStringTree.TotalCount;
ColCount := VirtualStringTree.Header.Columns.Count;
if (RowCount > 0)
and (ColCount > 0)
then
begin
//Bereich auswählen
Range := Worksheet.Range[RefToCell(1, 1), RefToCell(ColCount, RowCount)];
if not VarIsNull(Range)
then
begin
Data := VarArrayCreate([0, RowCount - 1,
0, ColCount - 1], varVariant);
with VirtualStringTree
do
begin
FNode := GetFirst;
for iRow := 0
to RowCount - 1
do
begin
FNodeData := GetNodeData(FNode);
for iCol := 0
to ColCount - 1
do
begin
Data[iRow, iCol] := FNodeData^.Columns[iCol];
end;
FNode := GetNext(FNode);
end;
end;
Range.Value := Data;
Range.Columns.AutoFit;
//Excel anzeigen
Workbook.Activate;
ExcelApp.Visible := True;
end;
end;
end
else
raise Exception.Create('
neues Workbook konnte nicht angelegt werden');
end
else
raise Exception.Create('
Excel.Application konnte nicht geladen werden');
end;