To create a new XLS file using Excel for .NET, three steps are required:
For example, the following code creates a new Excel file with a single sheet containing numbers from 1 to 100. Note that if you add the C1XLBook component to a form, you can skip the code in step 1.
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
' step 1: create a new workbook Dim C1XLBook1 As New C1XLBook() ' step 2: write content into some cells Dim sheet As XLSheet = C1XLBook1.Sheets(0) Dim i As Integer For i = 0 To 99 sheet(i, 0).Value = i + 1 Next i ' step 3: save the file C1XLBook1.Save("c:\temp\hello.xls") |
To write code in C#
C# |
Copy Code
|
---|---|
// step 1: create a new workbook C1XLBook c1XLBook1 = new C1XLBook(); // step 2: write content into some cells XLSheet sheet = c1XLBook1.Sheets[0]; for (int i = 0; i < 100; i++) { sheet[i, 0].Value = i + 1; } // step 3: save the file c1XLBook1.Save(@"c:\temp\hello.xls"); |
Step 2 is the most interesting one. The code starts by retrieving an XLSheet object that represents the single worksheet in the new Excel workbook. This sheet is created automatically when you add or create a new C1XLBook. Then the code uses the sheet indexer to reference cells in the sheet and assign them values from 1 to 100.
Note that the indexer in the XLSheet object automatically creates cells, if necessary. This makes it easy to fill worksheets that you create. If you want to find out the sheet dimensions, use the sheet's Rows.Count and Columns.Count properties.
Of course, you are not limited to assigning values to cells. You can also use styles to format the cells. Just create one or more XLStyle objects and assign them to cells much like you did values. This revised version of the code above creates a sheet where even numbers are shown in bold red characters and odd numbers in italic blue. Note that if you add the C1XLBook component to the form, you can skip the code in step 1.
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
' step 1: create a new workbook Dim C1XLBook1 As New C1XLBook() ' step 2: create styles for odd and even values Dim styleOdd As New XLStyle(C1XLBook1) styleOdd.Font = New Font("Tahoma", 9, FontStyle.Italic) styleOdd.ForeColor = Color.Blue Dim styleEven As New XLStyle(C1XLBook1) styleEven.Font = New Font("Tahoma", 9, FontStyle.Bold) styleEven.ForeColor = Color.Red ' step 3: write content and styles into some cells Dim sheet As XLSheet = C1XLBook1.Sheets(0) Dim i As Integer For i = 0 To 99 Dim cell As XLCell = sheet(i, 0) cell.Value = i + 1 If (i + 1) Mod 2 = 0 Then cell.Style = styleEven Else cell.Style = styleOdd EndIf Next i ' step 4: save the file C1XLBook1.Save("c:\temp\hello.xls") |
To write code in C#
C# |
Copy Code
|
---|---|
// step 1: create a new workbook C1XLBook c1XLBook1 = new C1XLBook(); // step 2: create styles for odd and even values XLStyle styleOdd = new XLStyle(c1XLBook1); styleOdd.Font = new Font("Tahoma", 9, FontStyle.Italic); styleOdd.ForeColor = Color.Blue; XLStyle styleEven = new XLStyle(c1XLBook1); styleEven.Font = new Font("Tahoma", 9, FontStyle.Bold); styleEven.ForeColor = Color.Red; // step 3: write content and styles into some cells XLSheet sheet = c1XLBook1.Sheets[0]; for (int i = 0; i < 100; i++) { XLCell cell = sheet[i, 0]; cell.Value = i + 1; cell.Style = ((i+1) % 2 == 0)? styleEven: styleOdd; } // step 4: save the file c1XLBook1.Save(@"c:\temp\hello.xls"); |
The code is similar. The main difference is the new step 2, which creates styles for odd and even cells. The new styles are assigned to cells in step 3, along with the cell values.
This is what the file created by the code above looks like when opened in Microsoft Excel: