Export to Excel Template

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.

3 Replies

AK Adhikesevan Kothandaraman Syncfusion Team 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




DS Dani Silva 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


AG Anish George Syncfusion Team October 2, 2015 12:12 PM UTC

 Hi Daniel,
 
Thanks for your update


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

Loader.
Up arrow icon