ComponentOne Excel for .NET
Quick Start / Step 1 of 4: Setting up the Project
In This Topic
    Step 1 of 4: Setting up the Project
    In This Topic

    In this step you will add a C1XLBook component to your form. Each book is composed of one or more sheets.

    1. Create a new .NET 2.0 project.
    2. In the Toolbox, double-click the C1XLBook icon to add the C1XLBook component to your project. The C1XLBook component will appear in the component tray below the form.
    3. Double-click the form to add the Form1_Load event and switch to code view.
    4. Add the Imports (Visual Basic) or using (C#) statement to the code at the top of the form so you can use all names within the C1.C1Excel namespace.

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Imports C1.C1Excel

      To write code in C#

      C#
      Copy Code
      using C1.C1Excel;

    Now that you have a C1XLBook, you can begin adding content to it.

    Add Content to C1XLBook

    While you are still in code view in the Visual Studio project, add the following code within the Form_Load event created in Step 1 of 4. This code will add content to the Excel workbook.

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' Add content to the sheet.
    Dim i As Integer
    Dim sheet as XLSheet = C1XLBook1.Sheets(0)
    For i = 0 To 9
        sheet(i, 0).Value = (i + 1) * 10
        sheet(i, 1).Value = (i + 1) * 100
        sheet(i, 2).Value = (i + 1) * 1000
    Next i

    To write code in C#

    C#
    Copy Code
    // Add content to the sheet.
    int i;
    C1.C1Excel.XLSheet sheet = c1XLBook1.Sheets[0];
    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;
    }

    The first ten rows in the first three columns of the XLS file will be populated with numbers when you run the project.

    Format the Content

    Next we will format the content using styles. The code in this step should be added after the code from Step 2 of 4 within the Form_Load event.

    1. Add the following code to create two new styles: style1 and style2.

      To write code in Visual Basic

      Visual Basic
      Copy Code
      'Add style 1.
      Dim style1 As New XLStyle(C1XLBook1)
      style1.Font = New Font("Tahoma", 9, FontStyle.Bold)
      style1.ForeColor = Color.RoyalBlue
      ' Add style 2.
      Dim style2 As New XLStyle(C1XLBook1)
      style2.Font = New Font("Tahoma", 9, FontStyle.Italic)
      style2.BackColor = Color.RoyalBlue
      style2.ForeColor = Color.White

      To write code in C#

      Title Text
      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. Then add the following code to apply the new styles to the content.

      To write code in Visual Basic

      Visual Basic
      Copy Code
      For i = 0 To 9
          ' Apply styles to the content.
          If (i + 1) Mod 2 = 0 Then
              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
          End If
      Next i
      

      To write code in C#

      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;
         }
      }