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.
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; } |
To format the content of XLSheet, you can use the XLStyle class as demonstrated in the following steps:
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; |
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; } } |
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"); |