Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6019417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:25:07+00:00 2026-05-23T03:25:07+00:00

Is it possible to export a VirtualStringTree to Excel or CSV? I am using

  • 0

Is it possible to export a VirtualStringTree to Excel or CSV?

I am using Delphi 2007 and trying to save my VirtualStringTree data records as Excel or CSV format.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:25:08+00:00Added an answer on May 23, 2026 at 3:25 am

    We have an ExcelWriter helper class, that can dump all kinds of things to Excel, e.g.

    • TADOQuery
    • TListView
    • TVirtualStringTree

    In this case the fast way (and easy to copy-paste here) is the overload that can dump a variant array into Excel:

    class function TExcelWriter.ExportToExcelVariantArray(const VArray: Variant; const Title, SubTitle: WideString): Boolean;
    var
        xl: OleVariant;
        workbook: OleVariant;
        worksheet: OleVariant;
        range: OleVariant;
        Rowcount, ColumnCount: Integer;
        HeaderRowIndex: Integer;
        s: WideString;
    begin
        Result := False;
    
        if not VarIsArray(VArray) then
            raise EExcelWriterException.Create('ExportToExcelVariantArray: Supplied variant is not an array');
    
        if VarArrayDimCount(VArray) <> 2 then
            raise EExcelWriterException.Create('ExportToExcelEVariantArray: Supplied variant array does not have 2 dimensions ('+IntToStr(VarArrayDimCount(VArray))+')');
    
        ColumnCount := VarArrayHighBound(VArray, 2) - VarArrayLowBound(VArray, 2); //2 for "leftmost dimension"
        rowCount := VarArrayHighBound(VArray, 1) - VarArrayLowBound(VArray, 1); //1 for "leftmost dimension"
    
        try
            xl := CreateOleObject('Excel.Application');
        except
            on E:Exception do
            begin
                if (E is EOleSysError) then
                begin
                    if EOleSysError(E).ErrorCode = CO_E_CLASSSTRING then
                        raise EExcelWriterException.Create('Excel is not installed.'+CRLF+
                                'Could not load "Excel.Application" object (Co_E_CLASSSTRING)')
                    else
                        raise;
                end
                else
                    raise;
            end;
        end;
        try
            xl.ScreenUpdating := False;
            xl.DisplayAlerts := False;  // Don't display dialogs such as "save changes to workbook".
    
    
            workbook := xl.Workbooks.Add;
            try
                Worksheet := Workbook.Worksheets[1];
                try
                    Worksheet.Activate;
                    Worksheet.Cells.WrapText := False;
    
                    HeaderRowIndex := 1; //Rows&Columns in Excel start at one.
                    range := TExcelWriter.GetRange(worksheet, HeaderRowIndex, 1, HeaderRowIndex+RowCount, ColumnCount);
                    range.Value := VArray;
    
                    //Bold the header row
                    Worksheet.Rows[HeaderRowIndex].Font.Bold := True;
                    Worksheet.Rows[HeaderRowIndex].Font.Underline := True;
                    Worksheet.Columns.AutoFit;
    
                    //Set printed header&footer
                    if Copy(Title, 1, 2) = '@@' then
                        s := Copy(Title, 3, MaxInt)
                    else
                        s := Title;
                    if SubTitle <> '' then
                    begin
                        if s <> '' then s := s+#13;
                        s := s + SubTitle;
                    end;
    
                    TExcelWriter.SetHeaderAndFooters(Worksheet,
                            s, '', '',
                            '&D &T', '', 'Page &P of &N');
                finally
                    Worksheet := Unassigned;
                end;
            finally
                Workbook := Unassigned;
            end;
    
            //When all done
            xl.ScreenUpdating := True;
            xl.Visible := True;
            xl.UserControl := True; // Very important, prevents Excel from going
                      // away when we nil out our reference to it below.
        finally
            xl := Unassigned;
        end;
    
        Result := True;
    end;
    

    We have a TVirtualListView descendant of TVirtualStringTree that made it easy to transition to VirtualTrees (it has TVirtualListItem, etc). It then has a helper method ContentToVariantArray, which is similar to ContentToHtml and ContentToRtf:

    function TVirtualListView.ContentToVariantArray: Variant;
    var
        Columns: TColumnsArray;
        VArray: Variant;
        Node: PVirtualNode;
        ColumnCount: Integer;
        RowCount: Integer;
        nRow: Integer;
        i: Integer;
    begin
        Columns := Self.Columns.GetVisibleColumns;
    
        ColumnCount := Length(Columns);
        RowCount := Self.Items.Count+1; //+1 for the heaader
    
        VArray := VarArrayCreate([0, RowCount-1, 0, ColumnCount-1], varOleStr); //Docs say cannot use varString, must be varOleStr (which is a BSTR i.e. WideString)
    
        nRow := 0;
    
        for i := 0 to ColumnCount-1 do
        begin
            VArray[nRow, i] := Self.Columns.Items[Columns[i].Index].Text;
        end;
    
        Node := Self.GetFirst;
        while Assigned(Node) do
        begin
            Inc(nRow);
            for i := 0 to ColumnCount-1 do
            begin
                VArray[nRow, i] := Self.Text[Node, Columns[i].Index];
            end;
    
            Node := Self.GetNextSibling(Node);
        end;
    
        Result := VArray;
    end;
    

    The main downside here is that we’re automating Excel in order to use it. This means your customer/server will need Excel installed.

    The above code shows the user Excel (having to save a file to the hard drive just to look at it is wasteful), rather than creating an export file. But wouldn’t be hard to call .Save or whatever the API is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It is possible export sqlite3 table to csv or xls format? I'm using python
Is it possible to Export a MySQL to a CSV using the command line
Is it possible to limit export formats only PDF and Excel ?
Is it possible to export a site definition (NOT a site template) using stsadm?
It is possible to export/print Visual Studio test results in some document format, maybe
Am exporting data to csv. after 25000 records , memory exhausted. Memory limit increasing
Is it possible, ideally using the spring JMX annotations, to export a map of
Possible Duplicate: Using SSL in an iPhone App - Export Compliance I am in
Possible Duplicate: How do you send email from a Java app using Gmail? How
Is it possible to export a flash movie with a transparent background as a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.