How to create an Excel (XLSX) file on Linux or macOS in C#

The following example shows how to use the GemBox.Spreadsheet library to create an Excel workbook and a PDF file using C# on Linux (Ubuntu).

Generated Excel workbook from .NET Core application running on Linux (Ubuntu)
Screenshot of an Excel file created with .NET Core on Linux

Prerequisites

After installing the requirements listed above you can follow these steps that show how you can easily use a Visual Studio Code (VS Code) to create a simple Console project and the C# code that generates an XLSX file.

1. Open a project folder

  • Open VS Code.
  • Click "Open Folder" on the EXPLORER panel.
  • Select the folder you want your console project to be in, for instance, GemBoxExample.
Selecting the folder you want your console project to be in
Screenshot of the Visual Studio Code folder selection

2. Create a new console project

  • Open Integrated Terminal in VS Code by clicking View > Terminal or using CTRL + ` shortcut.
  • In the terminal window execute following command: dotnet new console.
  • This command will create a code file (Program.cs) and a project file (GemBoxExample.csproj) inside the opened folder.
Creating a C# console project
Screenshot of the Visual Studio Code new console command

3. Edit project files

  • Add a reference to GemBox.Spreadsheet in GemBoxExample.csproj file:
    <ItemGroup>
      <PackageReference Include="GemBox.Spreadsheet" Version="*" />
    </ItemGroup>
  • Edit the C# code in Program.cs file:
    using GemBox.Spreadsheet;
    
    class Program
    {
        static void Main()
        {
            // If using the Professional version, put your serial key below.
            SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
    
            ExcelFile workbook = new ExcelFile();
            ExcelWorksheet worksheet = workbook.Worksheets.Add("Sheet1");
            ExcelCell cell = worksheet.Cells["A1"];
    
            cell.Value = "Hello World!";
    
            workbook.Save("HelloWorld.xlsx");
        }
    }
    

4. Run the console project

  • In the terminal window, execute the following command: dotnet run
  • This command will run your Console application and create an Excel workbook (HelloWorld.xlsx) that you can open in any Excel application.
Running a C# console project
Screenshot of the Visual Studio Code run command

Using the full functionality of GemBox.Spreadsheet on a Linux host requires the adjustments explained in detail on the Supported Platforms help page.

See also


Next steps

GemBox.Spreadsheet is a .NET component that enables you to read, write, edit, convert, and print spreadsheet files from your .NET applications using one simple API.

Download Buy