private void gridExportButton_Click(object sender, EventArgs e)
{
ExcelEngine engine = new ExcelEngine();
IApplication app = engine.Excel.Application;
app.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workBook = app.Workbooks.Create();
//Open the existing excel workbook.
workBook = engine.Excel.Workbooks.Open("..\\..\\Sample.xls");
workBook.Version = ExcelVersion.Excel2013;
IWorksheet sheet = workBook.Worksheets[0];
//Set the sheet name
if (sheet.Name != "First")
sheet.Name = "First";
else
{
sheet = workBook.Worksheets[1];
sheet.Name = "Second";
sheet.Activate();
}
//Export the grid to the given worksheet.
ExcelExportingOptions GridExcelExportingOptions = new ExcelExportingOptions();
sfDataGrid1.ExportToExcel(sfDataGrid1.View, GridExcelExportingOptions, sheet);
workBook.Save();
//Message box confirmation to view the created spreadsheet.
if (MessageBox.Show("Do you want to view the workbook?", "Workbook has been created", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
//Launching the Excel file using the default Application.[MS Excel Or Free ExcelViewer]
System.Diagnostics.Process.Start(workBook.FullFileName);
}
} |