Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V26.2
  • Get Started

    • 8 minutes to read

    Important

    You need a license for the DevExpress Office & PDF File API Subscription or DevExpress Universal Subscription to use this library in production code.

    Important

    The DevExpress object-oriented Spreadsheet Document API library is currently available as a Community Technology Preview (CTP). We do not recommend the use of pre-release libraries within mission-critical software applications. Please explore the capabilities of this new library and share your feedback so that we can make necessary changes before official release.

    To start a conversation, submit a ticket in the DevExpress Support Center.

    Follow this tutorial to create a console application that uses the Spreadsheet Document API to generate a sales report. The application adds product data and formulas to a worksheet, formats cells, calculates formulas, saves the workbook to an XLSX file, and exports the workbook to PDF.

    DevExpress Spreadsheet API - Get Started

    Create a .NET Application

    1. Start Visual Studio and create a new Console App project.

    2. Install the DevExpress.Docs.Spreadsheet NuGet package:

      dotnet add package DevExpress.Docs.Spreadsheet
      

    Refer to the following help topic for additional information: Install Packages from NuGet.org: Visual Studio, VS Code, and JetBrains Rider.

    Create a .NET Framework Application

    1. Start Visual Studio and create a new Console App (.NET Framework) project that targets .NET Framework 4.6.2 or later.

    2. Install the DevExpress.Docs.Spreadsheet NuGet package. If you used the DevExpress Unified Component Installer to install DevExpress products, you can add references to the following assemblies instead:

      • DevExpress.Data.v26.2.dll
      • DevExpress.Docs.Core.v26.2.dll
      • DevExpress.Docs.Spreadsheet.v26.2.dll
      • DevExpress.Drawing.v26.2.dll
      • DevExpress.Pdf.v26.2.Core.dll
      • DevExpress.Printing.v26.2.Core.dll
      • DevExpress.Spreadsheet.v26.2.Core.dll
      • DevExpress.Office.v26.2.Core.dll
      • DevExpress.Charts.v26.2.Core.dll
      • DevExpress.XtraCharts.v26.2.dll
      • DevExpress.DataVisualization.v26.2.Core.dll
      • DevExpress.XtraTreeMap.v26.2.dll

    Create a Workbook

    The Workbook class is the root object of a spreadsheet document. A new workbook contains a worksheet. Use the Workbook.Worksheets property to access the worksheet.

    In the Program.cs (Program.vb) file, replace the file content with the following code:

    using System;
    using System.IO;
    using DevExpress.Docs.Spreadsheet;
    
    // Create a workbook and access the first worksheet.
    using Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    worksheet.Name = "Sales";
    
    // Paste the code from the following sections here.
    

    Add Cell Values

    Use the Worksheet.Cells property to access cells by cell reference. To specify cell content, set the Cell.Value property to a numeric, text, or Boolean value.

    The following code snippet adds column headers to cells A1:D1 and product data to cells A2:C5:

    // Add column headers.
    worksheet.Cells["A1"].Value = "Product";
    worksheet.Cells["B1"].Value = "Units";
    worksheet.Cells["C1"].Value = "Unit Price";
    worksheet.Cells["D1"].Value = "Amount";
    
    // Add product data.
    string[] products = { "Laptop", "Monitor", "Keyboard", "Mouse" };
    int[] units = { 12, 25, 40, 55 };
    double[] prices = { 1250, 310, 45, 25 };
    for (int i = 0; i < products.Length; i++) {
        worksheet.Cells[i + 1, 0].Value = products[i];
        worksheet.Cells[i + 1, 1].Value = units[i];
        worksheet.Cells[i + 1, 2].Value = prices[i];
    }
    

    Add Formulas

    To add a formula to a cell, set the Cell.Formula property. To add a formula to all cells in a range, pass the range reference to the Worksheet indexer and set the CellRange.Formula property.

    The following code snippet adds formulas to cells D2:D5 that multiply units by unit prices, and adds a total row:

    // Multiply units by the unit price for each product.
    worksheet["D2:D5"].Formula = "=B2*C2";
    
    // Add the total row.
    worksheet.Cells["A6"].Value = "Total";
    worksheet.Cells["B6"].Formula = "=SUM(B2:B5)";
    worksheet.Cells["D6"].Formula = "=SUM(D2:D5)";
    

    Format Cells

    To modify a formatting setting, call a With method (for example, FontFormat.WithBold).

    Call one of the CellRange methods that start with Apply (for example, CellRange.ApplyFont or CellRange.ApplyNumberFormat) to modify a single formatting setting in all cells in a range.

    The following code snippet applies bold white text formatting and a blue fill to the header row, applies a currency number format to prices and amounts, and applies bold text formatting to the total row:

    // Format the header row.
    CellRange header = worksheet["A1:D1"];
    header.ApplyFont(new FontFormat("Calibri", 11).WithBold(true).WithColor(CellColor.FromRgb(255, 255, 255)));
    header.ApplyFill(new SolidFillFormat(CellColor.FromRgb(68, 114, 196)));
    
    // Apply a currency number format to prices and amounts.
    worksheet["C2:D6"].ApplyNumberFormat(new NumberFormat("$#,##0.00"));
    
    // Format the total row.
    worksheet["A6:D6"].ApplyFont(new FontFormat("Calibri", 11).WithBold(true));
    

    Specify Column Widths

    Use the Column.Width property to specify column width in characters.

    The following code snippet widens columns A, C, and D to fit product names and currency values:

    worksheet.Columns["A"].Width = 14;
    worksheet.Columns["C"].Width = 12;
    worksheet.Columns["D"].Width = 12;
    

    Calculate Formulas

    The Spreadsheet Document API does not calculate formulas when you assign them. Call the Workbook.Calculate() method to calculate formulas before reading their results.

    The following code snippet calculates formulas and displays the total amount in the console:

    workbook.Calculate();
    Console.WriteLine("Total amount: " + worksheet.Cells["D6"].Value.NumericValue);
    

    Save the Workbook and Export It to PDF

    Call the Workbook.Save method to save the workbook in XLSX format. Call the Workbook.ExportToPdf method to export the workbook to PDF.

    The following code snippet saves the workbook to an XLSX file and exports the workbook to a PDF file:

    Directory.CreateDirectory("output");
    
    using FileStream xlsxStream = File.Create(Path.Combine("output", "Document.xlsx"));
    workbook.Save(xlsxStream, DocumentFormat.Xlsx);
    
    using FileStream pdfStream = File.Create(Path.Combine("output", "Document.pdf"));
    workbook.ExportToPdf(pdfStream);
    

    Results

    Run the application.

    The following image shows the generated spreadsheet document:

    DevExpress Spreadsheet API - Get Started

    The following code snippet contains the complete application code:

    using System;
    using System.IO;
    using DevExpress.Docs.Spreadsheet;
    
    // Create a workbook and access the first worksheet.
    using Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    worksheet.Name = "Sales";
    
    // Add column headers.
    worksheet.Cells["A1"].Value = "Product";
    worksheet.Cells["B1"].Value = "Units";
    worksheet.Cells["C1"].Value = "Unit Price";
    worksheet.Cells["D1"].Value = "Amount";
    
    // Add product data.
    string[] products = { "Laptop", "Monitor", "Keyboard", "Mouse" };
    int[] units = { 12, 25, 40, 55 };
    double[] prices = { 1250, 310, 45, 25 };
    for (int i = 0; i < products.Length; i++) {
        worksheet.Cells[i + 1, 0].Value = products[i];
        worksheet.Cells[i + 1, 1].Value = units[i];
        worksheet.Cells[i + 1, 2].Value = prices[i];
    }
    
    // Multiply units by the unit price for each product.
    worksheet["D2:D5"].Formula = "=B2*C2";
    
    // Add the total row.
    worksheet.Cells["A6"].Value = "Total";
    worksheet.Cells["B6"].Formula = "=SUM(B2:B5)";
    worksheet.Cells["D6"].Formula = "=SUM(D2:D5)";
    
    // Format the header row.
    CellRange header = worksheet["A1:D1"];
    header.ApplyFont(new FontFormat("Calibri", 11).WithBold(true).WithColor(CellColor.FromRgb(255, 255, 255)));
    header.ApplyFill(new SolidFillFormat(CellColor.FromRgb(68, 114, 196)));
    
    // Apply a currency number format to prices and amounts.
    worksheet["C2:D6"].ApplyNumberFormat(new NumberFormat("$#,##0.00"));
    
    // Format the total row.
    worksheet["A6:D6"].ApplyFont(new FontFormat("Calibri", 11).WithBold(true));
    
    // Specify column widths.
    worksheet.Columns["A"].Width = 14;
    worksheet.Columns["C"].Width = 12;
    worksheet.Columns["D"].Width = 12;
    
    // Calculate formulas.
    workbook.Calculate();
    Console.WriteLine("Total amount: " + worksheet.Cells["D6"].Value.NumericValue);
    
    // Save the workbook and export it to PDF.
    Directory.CreateDirectory("output");
    
    using FileStream xlsxStream = File.Create(Path.Combine("output", "Document.xlsx"));
    workbook.Save(xlsxStream, DocumentFormat.Xlsx);
    
    using FileStream pdfStream = File.Create(Path.Combine("output", "Document.pdf"));
    workbook.ExportToPdf(pdfStream);
    

    Next Steps