Excel for .NET
クイックスタート

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 (.NET or .NET Framework) and set the project framework to .NET 6.0 for .NET application and .NET 4.5.2 for .NET Framework application 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. Drag and drop the C1XLBook control from Toolbox onto the Form.

Add Content to C1XLBook

To add content to C1XLWorkBook, you need to add a sheet to it by using 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 XLS file with random numbers.

C#
コードのコピー
// シートにコンテンツを追加します。
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;
}

Format the Content

To format 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.
    C#
    コードのコピー
    // style 1を追加します。
    XLStyle style1 = new XLStyle(c1XLBook1);
    style1.Font = new Font("Tahoma", 9, FontStyle.Bold);
    style1.ForeColor = Color.RoyalBlue;
    // 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 XLSheet using Style property of the XLCell class as demonstrated in the following code.
    C#
    コードのコピー
    for (i = 0; i <= 9; i++)
    {
        // コンテンツにスタイルを適用します。
        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 XLS File

Now, 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 to "mybook.xls" file in the bin directory of the project folder.

C#
コードのコピー
c1XLBook1.Save("mybook.xls");
System.Diagnostics.Process.Start("mybook.xls");