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

Export to Excel Template

Thread ID:

Created:

Updated:

Platform:

Replies:

120260 Sep 15,2015 08:21 AM UTC Oct 2,2015 12:12 PM UTC Windows Forms 3
loading
Tags: GridGroupingControl
Dani Silva
Asked On September 15, 2015 08:21 AM UTC

Hello,
There is any way to export a datagrid to an Excel template?
Something like:
Dim converter As GroupingGridExcelConverterControl = New GroupingGridExcelConverterControl
converter.GroupingGridToExcel(Me.grdPreview, "Grid.xls", ConverterOptions.Visible)
But to an existing template Excel file, not to a new one.
Thanks.

Adhikesevan Kothandaraman [Syncfusion]
Replied On September 16, 2015 12:21 PM UTC

Hi Daniel,

Thanks for contacting Syncfusion Support.

The reported scenario can be achieved by using simple workaround. We must create a worksheet to convert the grid to excel and then move the used range of exported worksheet to the already existing worksheet. Please refer to the following code example,


Code Example:

GroupingGridExcelConverterControl converter = new GroupingGridExcelConverterControl();           

ExcelEngine excelEngine = new ExcelEngine();

OpenFileDialog openFileDialog = new OpenFileDialog();

openFileDialog.Filter = "Files(*.xls)|*.xlx";

openFileDialog.DefaultExt = ".xls";           


if (openFileDialog.ShowDialog() == DialogResult.OK)

{

    //Create the object for the WorkBook

    IWorkbook sourceWorkBook = excelEngine.Excel.Workbooks.Open(openFileDialog.FileName);

    //Create temp worksheet for converting the grid to excel.

    sourceWorkBook.Worksheets.Create("Dummy");

    //Export grid to the temp worksheet

    converter.GroupingGridToExcel(this.gridGroupingControl1, sourceWorkBook.Worksheets["Dummy"], Syncfusion.GridExcelConverter.ConverterOptions.Default);

   //Get the used range of temp worksheet

    IRange currenRange = sourceWorkBook.Worksheets["Dummy"].UsedRange;  

    //get the used range of Primary worksheet

    IRange excelRange = sourceWorkBook.Worksheets[0].UsedRange;

    //Move the used range of temp worksheet to the end of the used range of primary worksheet

    currenRange.MoveTo(sourceWorkBook.Worksheets[0][excelRange.Rows.Length+1,1]);

    //Remove the temp worksheet from the workbook.

    sourceWorkBook.Worksheets["Dummy"].Remove();

    //Save the changes in the workbook

    sourceWorkBook.Save();

    if (MessageBox.Show("Do you wish to open the xls file now?", "Export to Excel", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)

    {

        Process proc = new Process();

        proc.StartInfo.FileName = openFileDialog.FileName;

        proc.Start();

    }             

}


Sample:
http://www.syncfusion.com/downloads/support/forum/120260/ze/ExportToExcelTemplate1032270483


Regards,
Adhi



Dani Silva
Replied On October 1, 2015 10:51 AM UTC

Hi again,
Sorry for my late answer.
It's running, quickly and efficient.
Thank you very much :-)

Dani Silva

Anish George [Syncfusion]
Replied On October 2, 2015 12:12 PM UTC

 Hi Daniel,
 
Thanks for your update


Please let us know if need any assistance.
 
Regards,
Anish.

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.

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

;