To create a new workbook and add values to the first ten cells, complete the following steps:
1. Add a reference to C1.Silverlight.Excel.dlland create a C1XLBook.
2. Add values to the first ten cells.
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 =>
{
// write content for the first ten cells
XLSheet sheet = book.Sheets[0];
for (int i = 0; i <= 9; i++)
{
sheet[i, 0].Value = i + 1;
}
});
}
// 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);
}
}
}