Skip to main content

Get Started - Create and Export an Excel File

  • 6 minutes to read

Important

You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use these examples in production code.

This article describes how to get started with the non-visual Spreadsheet component for .NET Framework and .NET Core.

Create a .NET Framework Application

  1. Start Visual Studio and create a new Console Application (.NET Framework) project.

  2. Install the DevExpress.Document.Processor NuGet package or add references to the following libraries (if you have the DevExpress Unified Component Installer installed on your machine):

    • DevExpress.Charts.v21.2.Core.dll
    • DevExpress.Data.v21.2.dll
    • DevExpress.DataAccess.v21.2.dll
    • DevExpress.DataVisualization.v21.2.Core.dll
    • DevExpress.Docs.v21.2.dll
    • DevExpress.Office.v21.2.Core.dll
    • DevExpress.Pdf.v21.2.Core.dll
    • DevExpress.Printing.v21.2.Core.dll
    • DevExpress.Sparkline.v21.2.Core.dll
    • DevExpress.Spreadsheet.v21.2.Core.dll
    • DevExpress.TreeMap.v21.2.Core.dll
    • DevExpress.XtraCharts.v21.2.dll
    • DevExpress.XtraTreeMap.v21.2.dll
  3. Open the Program.cs (Module1.vb) file. Paste the code below in the Main method of the Program class (Main procedure of the Module1 module in Visual Basic).

    using DevExpress.Spreadsheet;
    using System.Drawing;
    
    namespace SpreadsheetConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Create a new workbook.
                using (Workbook workbook = new Workbook())
                {
                    // Access the first worksheet in the workbook.
                    Worksheet worksheet = workbook.Worksheets[0];
    
                    // Set the unit of measurement.
                    workbook.Unit = DevExpress.Office.DocumentUnit.Point;
    
                    workbook.BeginUpdate();
                    try
                    {
                        // Create a multiplication table.
                        worksheet.Cells["A1"].Value = "*";
                        for (int i = 1; i < 11; i++)
                        {
                            // Create the header column.
                            worksheet.Columns["A"][i].Value = i;
                            // Create the header row.
                            worksheet.Rows["1"][i].Value = i;
                        }
    
                        // Multiply values of header cells.
                        worksheet.Range["B2:K11"].Formula = "=B$1*$A2";
    
                        // Obtain the data range.
                        CellRange tableRange = worksheet.GetDataRange();
    
                        // Specify the row height and column width.
                        tableRange.RowHeight = 40;
                        tableRange.ColumnWidth = 40;
    
                        // Align the table content.
                        tableRange.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;
                        tableRange.Alignment.Vertical = SpreadsheetVerticalAlignment.Center;
    
                        // Fill the header cells.
                        CellRange headerCells = worksheet.Range.Union(worksheet.Range["A1:K1"], 
                            worksheet.Range["A2:A11"]);
                        headerCells.FillColor = Color.FromArgb(0xf7, 0x9b, 0x77);
                        headerCells.Font.Bold = true;
    
                        // Fill cells that contain multiplication results.
                        worksheet.Range["B2:K11"].FillColor = Color.FromArgb(0xfe, 0xf2, 0xe4);
                    }
                    finally
                    {
                        workbook.EndUpdate();
                    }
    
                    // Calculate the workbook.
                    workbook.Calculate();
    
                    // Save the document file under the specified name.
                    workbook.SaveDocument("TestDoc.xlsx", DocumentFormat.OpenXml);
    
                    // Export the document to PDF.
                    workbook.ExportToPdf("TestDoc.pdf");
                }
                // Open the PDF document using the default viewer.
                System.Diagnostics.Process.Start("TestDoc.pdf");
    
                // Open the XLSX document using the default application.
                System.Diagnostics.Process.Start("TestDoc.xlsx");
            }
        }
    }
    
  4. Run the project.

The image below shows the result.

Getting_started

Create a .NET Core Application

  1. Start Visual Studio and create a new Console Application (.NET Core) project.

  2. Install the DevExpress.Document.Processor NuGet package.

  3. Open the Program.cs (Module1.vb) file. Paste the code below in the Main method of the Program class (Main procedure of the Module1 module in Visual Basic).

    using DevExpress.Spreadsheet;
    using System.Diagnostics;
    using System.Drawing;
    
    namespace SpreadsheetConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Create a new workbook.
                using (Workbook workbook = new Workbook())
                {
                    // Access the first worksheet in the workbook.
                    Worksheet worksheet = workbook.Worksheets[0];
    
                    // Set the unit of measurement.
                    workbook.Unit = DevExpress.Office.DocumentUnit.Point;
    
                    workbook.BeginUpdate();
                    try
                    {
                        // Create a multiplication table.
                        worksheet.Cells["A1"].Value = "*";
                        for (int i = 1; i < 11; i++)
                        {
                            // Create the header column.
                            worksheet.Columns["A"][i].Value = i;
                            // Create the header row.
                            worksheet.Rows["1"][i].Value = i;
                        }
    
                        // Multiply values of header cells.
                        worksheet.Range["B2:K11"].Formula = "=B$1*$A2";
    
                        // Obtain the data range.
                        CellRange tableRange = worksheet.GetDataRange();
    
                        // Specify the row height and column width.
                        tableRange.RowHeight = 40;
                        tableRange.ColumnWidth = 40;
    
                        // Align the table content.
                        tableRange.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;
                        tableRange.Alignment.Vertical = SpreadsheetVerticalAlignment.Center;
    
                        // Fill the header cells.
                        CellRange headerCells = worksheet.Range.Union(worksheet.Range["A1:K1"], 
                            worksheet.Range["A2:A11"]);
                        headerCells.FillColor = Color.FromArgb(0xf7, 0x9b, 0x77);
                        headerCells.Font.Bold = true;
    
                        // Fill cells that contain multiplication results.
                        worksheet.Range["B2:K11"].FillColor = Color.FromArgb(0xfe, 0xf2, 0xe4);
                    }
                    finally
                    {
                        workbook.EndUpdate();
                    }
    
                    // Calculate the workbook.
                    workbook.Calculate();
    
                    // Save the document file under the specified name.
                    workbook.SaveDocument("TestDoc.xlsx", DocumentFormat.OpenXml);
    
                    // Export the document to PDF.
                    workbook.ExportToPdf("TestDoc.pdf");
                }
    
                // Open the PDF document using the default viewer.
                Process.Start(new ProcessStartInfo("TestDoc.pdf") { UseShellExecute = true });
    
                // Open the XLSX document using the default application.
                Process.Start(new ProcessStartInfo("TestDoc.xlsx") { UseShellExecute = true });
            }
        }
    }
    
  4. Run the project.

The image below shows the result.

Getting_started

See Also