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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

EXPORT TO EXISTING EXCEL WITH NEW/UPDATE SHEET NAME

Thread ID:

Created:

Updated:

Platform:

Replies:

144851 May 25,2019 03:38 PM UTC May 27,2019 11:54 AM UTC WinForms 1
loading
Tags: SfDataGrid
Deepak
Asked On May 25, 2019 03:38 PM UTC

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


Jagadeesan Pichaimuthu [Syncfusion]
Replied On 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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon