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: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

GridControl add row in excel

Thread ID:

Created:

Updated:

Platform:

Replies:

129250 Mar 7,2017 10:32 AM UTC Mar 9,2017 06:20 AM UTC WinForms 3
loading
Tags: GridControl
Slava
Asked On March 7, 2017 10:32 AM UTC

Hello!
How to export gridControl in excel with add rows in file?
If ImportData and GridToExcel rewrite data(((

Piruthiviraj Malaimelraj [Syncfusion]
Replied On March 8, 2017 08:44 AM UTC

Hi Slava, 
 
Thanks for your interest in Syncfusion products. 
 
We have analyzed your scenario and created the simple sample as per your requirement. In order to import/export the grid  from/to excel, ExcelToGrid and GridToExcel methods of GridExcelConverterControl  can be used. In the attached sample, we are importing an excel sheet into a grid and export the grid after editing into that same excel file. To save the changes to excel , you need to save the WorkBook by code. Please refer to the attached sample and let us know if you have any queries. 
 
Code example: 
private GridExcelConverterControl control = new GridExcelConverterControl(); 
 
//Importing the excel to grid 
private void button2_Click(object sender, EventArgs e) 
{ 
                ExcelEngine engine = new ExcelEngine(); 
                IApplication app = engine.Excel.Application; 
                IWorkbook workbook = null; 
                workbook = engine.Excel.Workbooks.Open("..\\..\\Book2.xlsx", ExcelOpenType.Automatic); 
                IWorksheet mySheet = workbook.Worksheets[0]; 
                control.ExcelToGrid(mySheet, this.gridControl1.Model); 
 
    this.gridControl1.Refresh(); 
    MessageBox.Show("Successfully Imported"); 
} 
 
//Export the grid to excel. 
private void button1_Click(object sender, EventArgs e) 
{ 
                ExcelEngine engine = new ExcelEngine(); 
                IApplication app = engine.Excel.Application; 
                app.DefaultVersion = ExcelVersion.Excel2010; 
                IWorkbook workBook = app.Workbooks.Create(); 
                workBook = engine.Excel.Workbooks.Open("..\\..\\Book2.xlsx"); 
                IWorksheet sheet = workBook.Worksheets[0]; 
                control.GridToExcel(this.gridControl1, sheet, ConverterOptions.Visible); 
                workBook.Save(); 
                //Process.Start(@"..\..\Book2.xlsx"); 
 
    MessageBox.Show("Successfully exported"); 
} 
 
UG Document: 
 
Sample link: 
 
Note: 
If you want to import the excel sheet to grid control when it is needed or on demand , VirtualImporting can be used. Please refer to the dashboard sample from below location, 
 
Dashboard Sample Location: 
<InstalledLocation\Syncfusion\EssentialStudio\VersionNumber\Windows\Grid.Windows\Samples\Importing\Virtual Importing> 
 
UG Document: 
 
Regards, 
Piruthiviraj 


Slava
Replied On March 8, 2017 12:06 PM UTC

Hello!
The fact is that this way it overwrites the lines in the file. But where to look so that he adds them. Suppose I exported once. Then the data changed and added new rows in the excel file table


All. Solved this question =) Thanks for the answers. Support excellent

            ExcelEngine engine = new ExcelEngine();

            IApplication app = engine.Excel.Application;

            app.DefaultVersion = ExcelVersion.Excel2010;

            IWorkbook workBook = app.Workbooks.Create();

            workBook = engine.Excel.Workbooks.Open(@"../data/bux/sample.xlsx");

            IWorksheet sheet = workBook.Worksheets[0];

            sheet.InsertRow(1, checkGrid.RowCount, ExcelInsertOptions.FormatAsBefore); // <----- There was not enough line

            control.GridToExcel(this.checkGrid, sheet, ConverterOptions.Default);

            workBook.Save();


Piruthiviraj Malaimelraj [Syncfusion]
Replied On March 9, 2017 06:20 AM UTC

Hi Slava, 

Thanks for the update. 

We are glad to hear that the reported scenario is resolved from the provided solution. Please let us know if you have any queries. 

Regards, 
Piruthiviraj 


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

or the page will be automatically redirected to 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

;