Altova EBA Add-in for Excel, Version 2025 Enterprise Edition

C# Example

Home Prev Top Next

Each of the C# code listings illustrated below represents the Program.cs file in a standard .NET Framework console application. Before attempting to run the program code, make sure that you have added the required assembly references to the Visual Studio project, as described in Access API.

 

Export XBRL from Excel

The code listing below shows how to create a new Excel workbook using a specific XBRL entry point, populate a few properties and data cells, and save data to an XBRL file on the disk. The code will help you save both the Excel workbook and the XBRL file to the C:\XBRL_Examples directory.

 

using System;
using Excel = Microsoft.Office.Interop.Excel;
 
namespace EbaAddinClient
{
   class Program
   {
       static void Main(string[] args)
       {
           var app = new Excel.Application();
 
           try
           {
               // Suppress Excel alerts and create a new workbook
               Console.WriteLine("Creating a new workbook...");
               app.DisplayAlerts = false;
               var wb = (Excel._Workbook)(app.Workbooks.Add());
 
               // Get the Automation API object
               Console.WriteLine("Getting the COM automation object...");
               dynamic addIn = app.COMAddIns.Item("Altova.EBAAddIn");
               dynamic automationObject = addIn.Object;
 
               // Create a new report using taxonomy entry point
               Console.WriteLine("Creating the new report...");
               automationObject.InsertNewReport("http://www.eba.europa.eu/eu/fr/xbrl/crr/fws/fp/gl-2019-05/2020-02-29/mod/fp_con.xsd");
 
               // Set the report properties
               Console.WriteLine("Setting the report properties...");
               var rp = automationObject.GetReportProperties(wb);
               rp.ReportingEntityScheme = "http://standards.iso.org/iso/17442";
               rp.ReportingEntityIdentifier = "123456";
 
               // Find table by its code and ensure it is included in this report
               var tab = automationObject.GetTableTree(wb);
               var tableNode = tab.FindTableByRCCode("P 00.01");
               tableNode.IncludeInFiling = true;
 
               // Populate cells
               Console.WriteLine("Populating cells...");
               tableNode.Forms.Item(0).DataRange.Item(1).Value = "National GAAP";
               tableNode.Forms.Item(0).DataRange.Item(2).Value = "Consolidated";
 
               // Export data to XBRL
               Console.WriteLine("Exporting the XBRL instance...");
               automationObject.ExportXBRL(wb, @"C:\XBRL_Examples\Example.xbrl");
 
               // Save and close the .xlsx workbook
               Console.WriteLine("Saving the .xlsx file...");
               wb.SaveAs(@"C:\XBRL_Examples\Example.xlsx");
               wb.Close();
 
               Console.WriteLine("Task completed.");                
           }
           catch (Exception e)
           {
               Console.WriteLine(e.Message);              
           }
           finally
           {
               app.DisplayAlerts = true;
               app.Quit();
           }
       }
   }
}

 

Import XBRL to Excel

The code listing below shows how to convert an XBRL file to an Excel file. To run this example successfully, an XBRL instance file must exist at C:\XBRL_Examples\Example.xbrl. Otherwise, change the path accordingly. Use the Export command to create an XBRL file by running the previous code listing or manually from Excel. For more information, see Export Data to XBRL.

 

using System;
using Excel = Microsoft.Office.Interop.Excel;
 
namespace ConsoleApp1
{
   class Program
   {
       static void Main(string[] args)
       {
           var app = new Excel.Application();
           try
           {
               // Suppress Excel alerts and create a new workbook
               Console.WriteLine("Creating a new workbook...");
               app.DisplayAlerts = false;
               var wb = (Excel._Workbook)(app.Workbooks.Add());
 
               // Get the Automation API object
               Console.WriteLine("Getting the COM automation object...");
               dynamic addIn = app.COMAddIns.Item("Altova.EBAAddIn");
               dynamic automationObject = addIn.Object;
 
               // Import EBA report eba_example.xbrl
               Console.WriteLine("Importing XBRL...");
               automationObject.ImportXBRL(@"C:\XBRL_Examples\Example.xbrl");
 
               // Save as xlsx
               Console.WriteLine("Saving the .xlsx file...");
               wb.SaveAs(@"C:\XBRL_Examples\Example.xlsx");
               wb.Close();
 
               Console.WriteLine("Task complete.");
           }
           catch (Exception e)
           {
               Console.WriteLine(e.Message);
           }
           finally
           {
               app.DisplayAlerts = true;
               app.Quit();
           }
       }
   }
}

 

Map XBRL table to Excel report table

The code listing below shows how to map an XBRL table to a report table in Excel.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Excel = Microsoft.Office.Interop.Excel;

 

namespace EBAAddInAPITest

{

   class Program

   {

       internal struct Data

       {

           internal Data(string rc, string cc, string value)

           {

               RowCode = rc;

               ColumnCode = cc;

               Value = value;

           }

 

           internal string RowCode;

           internal string ColumnCode;

           internal string Value;

       }

 

       static Data[] testData = new Data[]

       {

           new Data("0010", "0060", "163.59000" ),

           new Data("0010", "0070", "2032.33000" ),

           new Data("0020", "0010", "1016.16000"),

           new Data("0022", "0010", "1000.00000"),

           new Data("0040", "0010", "16.16000"),

           new Data("0050", "0010", "1016.16000"),

           new Data("0020", "0030", "16.16000"),

           new Data("0020", "0050", "1016.16000"),

           new Data("0020", "0060", "81.29000"),

           new Data("0040", "0030", "16.16000"),

           new Data("0050", "0030", "1016.16000"),

           new Data("0050", "0050", "1016.16000"),

           new Data("1050", "0060", "81.29000")

       };

 

       static void Main(string[] args)

       {

           var app = new Excel.Application();

 

           try

           {

               // Suppress Excel alerts and create a new workbook

               Console.WriteLine("Creating a new workbook...");

               app.DisplayAlerts = false;

               app.Visible = false;

               var wb = (Excel._Workbook)(app.Workbooks.Add());

 

               // Get the Automation API object

               Console.WriteLine("Getting the COM automation object...");

               // dynamic addIn = app.COMAddIns.Item("Altova.EBAAddIn");

               dynamic addIn = app.COMAddIns.Item("EBAAddInForExcel");

               dynamic automationObject = addIn.Object;

 

               // Create a new report using taxonomy entry point

               Console.WriteLine("Creating the new report...");

               automationObject.InsertNewReport("http://www.eba.europa.eu/eu/fr/xbrl/crr/fws/if/its-002-2021/2021-05-08/mod/if_class2_con.xsd");

 

               // Set the report properties

               Console.WriteLine("Setting the report properties...");

               var rp = automationObject.GetReportProperties(wb);

               rp.ReportingEntityScheme = "http://standards.iso.org/iso/17442";

               rp.ReportingEntityIdentifier = "123456";

 

               // Find table by its code and ensure it is included in this report

               var tab = automationObject.GetTableTree(wb);

               var tableNode = tab.FindTableByRCCode("C 21.00");

               tableNode.IncludeInFiling = true;

 

               // Read column/row headers

               var columnIndices = new Dictionary<string, int>();

               var rowIndices = new Dictionary<string, int>();

               var form = tableNode.Forms.Item(0);

               var dataRange = form.DataRange;

               for (int i = 1; i <= dataRange.Columns.Count; ++i)

                   columnIndices.Add(dataRange.Item(0, i).Value.ToString(), i);

               for (int i = 1; i <= dataRange.Rows.Count; ++i)

                   rowIndices.Add(dataRange.Item(i, 0).Value.ToString(), i);

 

               // Populate cells

               Console.WriteLine("Populating cells...");

               foreach (var data in testData)

               {

                   int row, column;

                   if (!rowIndices.TryGetValue(data.RowCode, out row))

                   {

                       Console.WriteLine(String.Format("Warning: RowCode {0} not found in form {1}!", data.RowCode, form.Text));

                       continue;

                   }

                   if (!columnIndices.TryGetValue(data.ColumnCode, out column))

                   {

                       Console.WriteLine(String.Format("Warning: ColumnCode {0} not found in form {1}!", data.ColumnCode, form.Text));

                       continue;

                   }

 

                   dataRange.Item(row, column).Value = data.Value;

               }

 

               // Export data to XBRL

               Console.WriteLine("Exporting the XBRL instance...");

               automationObject.ExportXBRL(wb, @"C:\XBRL_Examples\Example.xbrl");

               

               var valReport = automationObject.GetValidationReport(wb);

               if (valReport != null)

                   Console.WriteLine(valReport.CreateTextReport());

 

               // Save and close the .xlsx workbook

               Console.WriteLine("Saving the .xlsx file...");

               wb.SaveAs(@"C:\XBRL_Examples\Example.xlsx");

               wb.Close();

 

               Console.WriteLine("Task completed.");

           }

           catch (Exception e)

           {

               Console.WriteLine(e.Message);

           }

           finally

           {

               app.DisplayAlerts = true;

               app.Quit();

           }

       }

   }

}

 

© 2018-2024 Altova GmbH