In this step you will add a C1XLBook component to your form. Each book is composed of one or more sheets.
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.
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.
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.
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; |
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; } } |