We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

EXPORT TO EXISTING EXCEL WITH NEW/UPDATE SHEET NAME

Dear Sir,

I have SFDATAGRID and Want to Export the data to Excel with file name "ABC.XLS"

Now the query is How Can Export the Data to any existing File (If File already exists in path where save the file) and update the Sheet. 

Example 1: I have ABC.XLS Where Sheet name "First" and "Second" already exists. Now  I want to export the SFDATAGRID Data into ABC.XLS with Sheet name "Second". In this ABC.XLS old "Second" sheet will deleted and updated excel with New "Second" sheet. How I can Get the Result ?

Example 2: I have ABC.XLS Where Sheet name "First"  is already exists. Now  I want to export the SFDATAGRID Data into ABC.XLS with Sheet name "Second". In this ABC.XLS New Sheet "Second" will be added with SFDATAGRID Data. How I can Get the Result

Please reply me at the earliest.

Thanks
Deepak


1 Reply

JP Jagadeesan Pichaimuthu Syncfusion Team May 27, 2019 11:54 AM UTC

Hi Deepak, 
  
Thanks for using Syncfusion products. 
  
If you want to export the grid to the existing excel sheet, you can use the ExcelEngine to open the workbook and set the name of the sheet to export. 
Refer to the following snippet and sample code, 
  
Code Snippet: 
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); 
    } 
} 
  
 
Let us know whether this helps also if you need any further assistance on this. 
  
Regards, 
Jagadeesan 


Loader.
Live Chat Icon For mobile
Up arrow icon