---
title: Export to CSV and Excel
description: "You may clone all examples from git at this repository: https://github.com/SigmaEstimates/AppScript-Examples.git In the Export to CSV and Excel example, you can learn how to create a Ribbon tab with two buttons, that allows the user to export a Sigma insight view…"
url: "https://developer.sigmaestimates.com/docs/export-csv-excel/"
updated: "2019-05-07"
category: Sample Code
---

# Export to CSV and Excel

You may clone all examples from git at this repository: https://github.com/SigmaEstimates/AppScript-Examples.git
In the Export to CSV and Excel example, you can learn how to create a Ribbon tab with two buttons, that allows the user to export a Sigma insight view to either CSV or Excel. This way you can create customized exports for Sigma users.
//First, name the unit (this name is used to potentially include it in other units)
unit ExportExcelAndCSV;

//The interface section is used to define types and classes
interface

//Uses are inclusion of other libraries/units
uses
    Sigma.Document,       // Access to the Sigma application, the documents (projects) and their data
    System.UI.Dialogs,    // A library containing various Dialog methods, including ShowMessage()
    System.UI.Ribbon,     // A library providing access to the Application Ribbon (for adding/manipulating Ribbons and buttons)
    COM;                  // A library providing access to Windows COM servers (in this case to communicate with Excel)

//Type definitions, including classes
type

//We create a new class names TApp, that is inherited from the base class
TApp = class
  
  //Private properties, methods, variables - not accessiable outside the class
  private
    CSVLine: TStringList;
    FRibbonTab: TRibbonTab;
    FRow := 5;

    procedure ExportCSV(Sender: TRibbonItem);
    procedure ExportCSVComponent(Item: TSigmaItem; CSV: TStringList);

    procedure ExportExcel(Sender: TRibbonItem);
    procedure ExportExcelComponent(Sheet: COMVariant; Item: TSigmaItem);

  //Public properties, methods, variables - visible and accessible outside the class
  public

    procedure AddRIbbon();
    procedure RemoveRIbbon();
end;

//After the interface section follows the implementation section, that must implement all classes
implementation

//Implenting a class method (procedure or function) is done this way:
procedure TApp.AddRibbon();
begin
    //Create new Ribbon tab and group
    FRibbonTab := Ribbon.Tabs.Add;
    FRibbonTab.Caption := &#x27;Export Tools&#x27;;
    var FGroup := FRibbonTab.Groups.Insert(0);
    FGroup.Caption := &#x27;Export&#x27;;

    //Add button to execute App
    var Item := FGroup.Items.Add(TRibbonLargeButtonItem);
    Item.OnClick := ExportCSV;  //Assign a method to call when clicking the Ribbon button
    Item.Caption := &quot;CSV file export&quot;;
    Item.LargeGlyph.LoadFromFile(&#x27;export_csv.png&#x27;, true);  //Expected in the same folder as the AppScript file (or the same .sigmabundle file)

    Item := FGroup.Items.Add(TRibbonLargeButtonItem);
    Item.OnClick := ExportExcel;
    Item.Caption := &quot;Excel file export&quot;;
    Item.LargeGlyph.LoadFromFile(&#x27;export_excel.png&#x27;, true);
end;

procedure TApp.RemoveRibbon();
begin
   FRibbonTab.Free;  //Will remove the Ribbon Tab
end;

procedure TApp.ExportCSV(Sender: TRibbonItem);
begin
    //Create a new AppScript insightview
    var FInsightViewTree := TSigmaInsightViewTree.Create( Application.ActiveProject );

    //Get a handle to an existing Insight view in the active project
    var InsightView := Application.ActiveProject.InsightViews.FindByName(&quot;Location&quot;);

    //Ensure Insight view exists, load settings from existing and build with project root as source
    ASSERT(InsightView &lt;&gt; nil);
    FInsightViewTree.LoadSettings(InsightView.Setup);
    FInsightViewTree.Build(Application.ActiveProject.RootItem);

    //Create CSV file holder based on TStringList class
    var CSVFile := new TStringList;

    //Create instance of CSV Line, used to produce every CSV file line (record)
    CSVLine := new TStringList;
    CSVLine.Delimiter := &#x27;;&#x27;;
    
    //Build a header line
    CSVLine.Add(&quot;Level&quot;);
    CSVLine.Add(&quot;Text&quot;);
    CSVLine.Add(&quot;Phase&quot;);
    CSVLine.Add(&quot;SalesPrice&quot;);
    CSVFile.Add( CSVLine.Delimitedtext);

    //Now recursively add all lines from insight and save to file
    ExportCSVComponent(FInsightViewTree.RootItem, CSVFile);
    CSVFile.SaveToFile(&quot;output.csv&quot;);

    CSVLine.Free;

end;

procedure TApp.ExportCSVComponent(Item: TSigmaItem; CSV: TStringList);
begin
    //Prepare for new line, and add columns
    CSVLine.Clear;
    CSVLine.Add( IntToStr(Item.Level) );
    CSVLine.Add(Item.Values[tcText]);
    CSVLine.Add(Item.CustomFieldValues[&quot;Phase&quot;]);
    CSVLine.Add(Item.Values[tcSalesPrice]);
    CSV.Add( CSVLine.Delimitedtext);

    //Recursively add children
    for var i := 0 to Item.Items.Count-1 do
        if Item.Items.Count &gt; 0 then
            ExportCSVComponent(Item.Items[i], CSV);

end;

procedure TApp.ExportExcel(Sender: TRibbonItem);
begin
    //Create a new AppScript insightview
    var FInsightViewTree := TSigmaInsightViewTree.Create(Application.ActiveProject);

    //Get a handle to an existing Insight view in the active project
    var InsightView := Application.ActiveProject.InsightViews.FindByName(&quot;Location&quot;);

    //Ensure Insight view exists, load settings from existing and build with project root as source
    ASSERT(InsightView &lt;&gt; nil);
    FInsightViewTree.LoadSettings(InsightView.Setup);
    FInsightViewTree.Build(Application.ActiveProject.RootItem);

    //Create Excel COM object
    var MSExcel := CreateOleObject(&#x27;Excel.Application&#x27;);
	
    //To speed up things, we don&#x27;t want Excel to reflect changes during the proces
    MSExcel.ScreenUpdating := False;
	
    //Now add a workbook - if you give an excel file as argument to Add, it will be used as template
    var WorkBook := MSExcel.Workbooks.Add();
	
    //And finally select the first sheet (there is always a sheet in a new workbook)
    var Sheet := WorkBook.Sheets(1);

    //Now recursively add all lines from insight and show Excel
    FRow := 5;
    ExportExcelComponent(Sheet, FInsightViewTree.RootItem);

    MSExcel.ScreenUpdating := True;
    Sheet.Activate;
    MSExcel.Visible := true;
    MSExcel.WindowState := -4137; //xlMaximized

end;

procedure TApp.ExportExcelComponent(Sheet: COMVariant; Item: TSigmaItem);
begin
    Sheet.Cells(FRow, 1).Value := IntToStr(Item.Level);
    Sheet.Cells(FRow, 2).Value := Item.Values[tcText];
    if Item.Level&gt;0 then
       Sheet.Cells(FRow, 2).InsertIndent( Item.Level );
    Sheet.Cells(FRow, 3).Value := Item.CustomFieldValues[&quot;Phase&quot;];
    Sheet.Cells(FRow, 4).Value := Item.Values[tcSalesPrice];
    FRow := FRow +1;

    //Recursively add children
    for var i := 0 to Item.Items.Count-1 do
        if Item.Items.Count &gt; 0 then
            ExportExcelComponent(Sheet, Item.Items[i]);

end;

//The initialization part is executed when Sigma loads the AppScript
initialization

var MyApp := TApp.Create;
MyApp.AddRIbbon;

//The finalization part is executed when Sigma unloads the AppScript (when Sigma is closed/terminated)
finalization

MyApp.RemoveRIbbon;
MyApp.Free;


View this file on GitHub
