To format the cells of a book, complete the following steps:
1. Add a reference to C1.Silverlight.Excel.dlland create a C1XLBook.
2. Add some content to the workbook, create a new style and apply the styles to the cells in the first column of the sheet.
3. Save the workbook. The code looks like the following. In this example it is placed within a button1_Click event so the Save As dialog box will open when the user clicks the button.
•C#
private void button1_Click_1(object sender, RoutedEventArgs e)
{
// create a new workbook to be saved
SaveBook(book =>
{
// Create a new style
XLStyle style1 = new XLStyle(book);
style1.ForeColor = Colors.Yellow;
style1.BackColor = Colors.Blue;
style1.Format = "$ .00";
// Add content and apply styles to cells in first column of sheet
XLSheet sheet = book.Sheets[0];
int i;
for (i = 0; i <= 9; i++)
{
sheet[i, 0].Value = i + 1;
sheet[i, 0].Style = style1;
}
});
}
// save the file
private void SaveBook(Action<C1XLBook> action)
{
var dlg = new SaveFileDialog();
dlg.Filter = "Excel Files (*.xlsx)|*.xlsx";
if (dlg.ShowDialog() == true)
{
try
{
var book = new C1XLBook();
if (action != null)
{
action(book);
}
using (var stream = dlg.OpenFile())
{
book.Save(stream);
}
}
catch (Exception x)
{
MessageBox.Show(x.Message);
}
}
}