Get Started - Create and Export an Excel File

  • 6 minutes to read
IMPORTANT

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

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. Add references to the following libraries:

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

    using DevExpress.Spreadsheet;
    using System.Drawing;
    // ...
    
    static void Main(string[] args)
    {
        // Create a new workbook.
        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. Paste the code below in the Main method of the Program.cs file (Main procedure of the Module1.vb file for Visual Basic).

    using DevExpress.Spreadsheet;
    using System.Diagnostics;
    using System.Drawing;
    // ...
    
    static void Main(string[] args)
    {
        // Create a new workbook.
        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