Step 2 of 4: Adding Content to a C1XLBook
In this step, you will add code to set up your project, and you will edit the HelloButton_Click event.
- To define the C1XLBook, add the following code directly below the MainPage class:
C# |
Copy Code
|
C1XLBook _book;
|
- Add a new C1XLBook and some text to the TextBox control by adding the following code after the InitializeComponent() method:
C# |
Copy Code
|
_book = new C1XLBook();
_tbContent.Text = "Empty workbook";
|
- Add the following to define the RefreshView() method after the MainPage constructor:
C# |
Copy Code
|
void RefreshView()
{
}
|
- Locate the HelloButton_Click event next. Add the following code to create a new workbook, get the sheet that was created by default, create some styles for the data, and write some content and format the cells:
C# |
Copy Code
|
// step 1: create a new workbook
_book = new C1XLBook();
// step 2: get the sheet that was created by default, give it a name
XLSheet sheet = _book.Sheets[0];
sheet.Name = "Hello World";
// step 3: create styles for odd and even values
XLStyle styleOdd = new XLStyle(_book);
styleOdd.Font = new XLFont("Tahoma", 9, false, true);
styleOdd.ForeColor = Color.FromArgb(255, 0, 0, 255);
XLStyle styleEven = new XLStyle(_book);
styleEven.Font = new XLFont("Tahoma", 9, true, false);
styleEven.ForeColor = Color.FromArgb(255, 255, 0, 0);
// step 4: write content and format into some cells
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 5: allow user to save the file
_tbContent.Text = "'Hello World' workbook has been created, you can save it now.";
RefreshView();
|