private void btn_Click(object sender, RoutedEventArgs e)
{
if (grid == null) return;
try
{
chart.Save("temp.jpg");
string jpgimage = "temp.jpg";
ExcelExportingOptions option = new ExcelExportingOptions();
option.ExcelVersion = ExcelVersion.Excel97to2003;
var excelEngine = grid.ExportToExcel(grid.View, option);
var workBook = excelEngine.Excel.Workbooks[0];
workBook.Worksheets[0].Pictures.AddPicture(20,1,jpgimage);
SaveFileDialog sfd = new SaveFileDialog
{
FilterIndex = 2,
Filter = "Excel 97 to 2003 Files(*.xls)|*.xls|Excel 2007 to 2010 Files(*.xlsx)|*.xlsx"
};
if (sfd.ShowDialog() == true)
{
using (Stream stream = sfd.OpenFile())
{
if (sfd.FilterIndex == 1)
workBook.Version = ExcelVersion.Excel97to2003;
else
workBook.Version = ExcelVersion.Excel2010;
workBook.SaveAs(stream);
}
//Message box confirmation to view the created spreadsheet.
if (MessageBox.Show("Do you want to view the workbook?", "Workbook has been created",
MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
{
//Launching the Excel file using the default Application.[MS Excel Or Free ExcelViewer]
System.Diagnostics.Process.Start(sfd.FileName);
}
}
}
catch (Exception)
{
}
}
|