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

GridControl add row in excel

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

3 Replies

PM Piruthiviraj Malaimelraj Syncfusion Team 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 



SL Slava 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();



PM Piruthiviraj Malaimelraj Syncfusion Team 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 


Loader.
Up arrow icon