ComponentOne Excel for .NET
In This Topic
    Quick Start
    In This Topic

    This quick start guides you through the steps of creating a simple List application. You begin by creating a Windows Forms App in Visual Studio, adding C1XLBook to the Form, adding content to it, formatting the data, and then saving it to an XLS file.

    Follow the given steps to create a simple Excel application.

    Set Up the Application

    1. Create a new Windows Forms App and set the project framework to .NET 6.0 using Configure your new project window.
    2. Add reference to the Excel assembly in your application by installing C1.Excel NuGet package from NuGet Package Manager.
    3. Switch to the code view and create a new instance of the C1XLBook class using the following code.
      C#
      Copy Code
      c1XLBook1 = new C1XLBook();
      

    Add Content to C1XLBook

    To add content to C1XLBook, you need to first add a sheet to it using the XLSheet class. Once you add the sheet, you can easily add content to its cells by using Value property of the XLCell class as demonstrated in the following code snippet. In this example, we populate the first ten rows in the first three columns of the sheet named First_Sheet with random numbers.

    C#
    Copy Code
    // Add content to the sheet.
    int i;
                
    C1.C1Excel.XLSheet sheet = c1XLBook1.Sheets[0];
    sheet.Name = "First_Sheet";
    for (i = 0; i <= 9; i++)
    {
        sheet[i, 0].Value = (i + 1) * 10;
        sheet[i, 1].Value = (i + 1) * 100;
        sheet[i, 2].Value = (i + 1) * 1000;
    }
    

    Style the Content

    To style the content of XLSheet, you can use the XLStyle class as demonstrated in the following steps:

    1. Create a custom style as an object of XLStyle class and define its properties such as background color, foreground color, and font style. In this example, we create two styles and set their respective properties such as font, foreground color, and background color.
      C#
      Copy Code
      // Add style 1.
      XLStyle style1 = new XLStyle(c1XLBook1);
      style1.Font = new Font("Tahoma", 9, FontStyle.Bold);
      style1.ForeColor = Color.RoyalBlue;
      // Add style 2.
      XLStyle style2 = new XLStyle(c1XLBook1);
      style2.Font = new Font("Tahoma", 9, FontStyle.Italic);
      style2.BackColor = Color.RoyalBlue;
      style2.ForeColor = Color.White;
      
    2. Apply the created styles to rows and columns of the sheet using Style property of the XLCell class as demonstrated in the following code.
      C#
      Copy Code
      for (i = 0; i <= 9; i++)
      {
          // Apply styles to the content.
          if ((i + 1) % 2 == 0)
          {
              sheet[i, 0].Style = style2;
              sheet[i, 1].Style = style1;
              sheet[i, 2].Style = style2;
          }
          else
          {
              sheet[i, 0].Style = style1;
              sheet[i, 1].Style = style2;
              sheet[i, 2].Style = style1;
          }
      }
      

    Save the File

    Now, you can save the updates to an XLS file using Save method of the C1XLBook class as demonstrated in the following code. In this example, we save the content to "mybook.xls" file in the bin directory of the project folder.

    C#
    Copy Code
    c1XLBook1.Save("mybook.xls");
    System.Diagnostics.Process.Start("mybook.xls");