本快速入门指南将使你熟悉Excelfor.NET的一些特性。在快速入门中你将学到怎么样给程序添加一个C1XLBook,给工作簿添加格式化好的数据,以及保存和打开XLS文件。
步骤一:创建程序
在这步中你将添加一个C1XLBook 组建到你的窗体中。每个Excel文件由一个或多个表组成。
- 创建一个新的.NET2.0程序
- 工具盒中,双击C1XLBook 图标就会将C1XLBook 组件添加到你的程序中。C1XLBook 组件会显示在窗体下的组件托盘里。
- 双击窗体添加Form1_Load事件并且切换到代码视图。
- 窗体的上面添加Imports (Visual Basic)或者using (C#)声明到代码中,这样你就可以使用在C1.C1Excel命名空间中的所有命名。
Visual Basic
Visual Basic |
Imports C1.C1Excel |
C#
现在你有了一个C1XLBook,你可以开始给它添加内容了。
步骤二:给C1XLBook添加内容
当你还在VisualStudio程序的代码视图中,将下面的代码添加到步骤一创建的Form_Load事件中,这些代码就会给Excel 文件添加内容。
Visual Basic
Visual Basic |
' 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 |
C#
C# |
// Add content to the sheet. |
int i; <ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="775edbf6-f526-4d8f-90c3-cd73c466e154"><ac:plain-text-body><![CDATA[C1.C1Excel.XLSheet sheet = c1XLBook1.Sheets[0]; for (i = 0; i <= 9; i++) ]]></ac:plain-text-body></ac:structured-macro> { <ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a01bd396-db1c-48a6-a22a-bb336a0a24fd"><ac:plain-text-body><![CDATA[ sheet[i, 0].Value = (i + 1) * 10; sheet[i, 1].Value = (i + 1) * 100; sheet[i, 2].Value = (i + 1) * 1000; ]]></ac:plain-text-body></ac:structured-macro> } |
当你运行程序,XLS文件前十行中的前三列将会填充数字。
步骤三:格式化内容
下面你将使用样式格式化内容。此步骤中的代码应该添加在Form_Load事件中步骤二的代码之后。
- 添加下面的代码来创建两个样式:style1和style2.
Visual Basic
Visual Basic |
'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 |
C#
Title Text |
// 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; |
- 然后添加下面的代码来给内容提供样式。
Visual Basic
Visual Basic |
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
C#
C# |
for (i = 0; i <= 9; i++) { <ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="eb7538f5-991e-4eb5-ab31-5df3880cf6e7"><ac:plain-text-body><![CDATA[ // 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; } ]]></ac:plain-text-body></ac:structured-macro> } |
步骤四:保存和打开XLS文件
最后,添加下面的代码来保存和加载工作簿。这些代码需要添加在Form_Load事件中步骤三的代码之后。
Visual Basic
Visual Basic |
C1XLBook1.Save("c:\mybook.xls") System.Diagnostics.Process.Start("C:\mybook.xls") |
C#
C# |
c1XLBook1.Save(@"c:\mybook.xls"); System.Diagnostics.Process.Start(@"c:\mybook.xls"); |
运行程序并且观察:

格式化好的内容添加到了Excel文件中。
恭喜!你已经完成了Excelfor.NET快速入门。