Home Forums WinForms controls Xceed Grid for WinForms Xceed data grid for .net and exporting to xlsx format Reply To: Xceed data grid for .net and exporting to xlsx format

User (Old forums)
Member
Post count: 23064

Hi Ghislain Thanks for your help. I did change the code as per your comments. But I still get the error when opening the file.

I am attaching herewith the code snippet for your perusal. Could you please help me to fix this issue.

Highlighted ones are the code I tried modifying according to your suggestion:

 

       protected void BtnExport_Click(object sender, EventArgs e)

        {

            try

            {

                bool isRowSelected = true;

                var fileDialog = new SaveFileDialog

                {

                    InitialDirectory = “c:\\”,

                    RestoreDirectory = true,

                    Filter = “(Export files) *.xml|”,

                    AddExtension = true,

                    DefaultExt = “xml”,

                    ValidateNames = true

                };

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

                {

                    ExcelExporter excelExporter = new ExcelExporter();

                    excelExporter.IncludeColumnHeaders = true;

                    excelExporter.GridLineColor = Color.Black;

                    var fileNamenoExtn= Path.GetFileNameWithoutExtension(fileDialog.FileName);

                    var diskFileName = new DiskFile(fileNamenoExtn + “.xlsx”); 

                    int startRow = Int32.MaxValue, endRow = -1, startColumn = Int32.MaxValue, endColumn = -1;

                    int currentRowIndex;

 

                    if (selectedCells.Count > 1)

                    {

                        this.GetSelectedCellsBounds(ref startRow, ref startColumn, ref endRow, ref endColumn, selectedCells);

                        excelExporter.Export(PrepareSelectedGrid(startColumn, endColumn, startRow, endRow, false), diskFileName.ToString());

                    }

                    else if (gridTable.SelectedRows.Count > 0)

                    {

                        int counter = 0;

                        while (counter < gridTable.SelectedRows.Count)

                        {

                            if (gridTable.SelectedRows[counter] is Xceed.Grid.DataRow)

                            {

                                currentRowIndex = ((GridTableRow)gridTable.SelectedRows[counter]).DisplayableRowId;

                                if (currentRowIndex > endRow)

                                {

                                    endRow = currentRowIndex;

                                }

                                if (currentRowIndex < startRow)

                                {

                                    startRow = currentRowIndex;

                                }

                            }

                            else if (this.gridTable.SelectedRows[counter] is Xceed.Grid.GroupManagerRow)

                            {

                                Xceed.Grid.GroupManagerRow gr = ((Xceed.Grid.GroupManagerRow)this.gridTable.SelectedRows[counter]);

                                Xceed.Grid.Collections.ReadOnlyDataRowList groupList = ((Xceed.Grid.Group)(gr.ParentGroup)).GetSortedDataRows(true);

                                int groupRowCount = 0;

                                isRowSelected = false;

                                while (groupRowCount < groupList.Count)

                                {

                                    currentRowIndex = ((GridTableRow)groupList[groupRowCount]).DisplayableRowId;

                                    if (currentRowIndex > endRow)

                                    {

                                        endRow = currentRowIndex;

                                    }

                                    if (currentRowIndex < startRow)

                                    {

                                        startRow = currentRowIndex;

                                    }

                                    groupRowCount++;

                                }

                            }

 

                            counter++;

                        }

 

                        excelExporter.Export(PrepareSelectedGrid(0, gridTable.Columns.DisplayableColumnCount – 1, startRow, endRow, isRowSelected), diskFileName.ToString());

                        return;

                    }

                    else

                    {

                        excelExporter.Export(PrepareSelectedGrid(0, gridTable.Columns.DisplayableColumnCount – 1, 0, gridTable.DataRows.Count – 1, false), diskFileName.ToString()); // No selection hence export all grid

                    }

                }

                else

                {

                    // User has Cancelled

                    return;

                }

            }

            catch (IOException)

            {

                if (MessageBox.Show(“File is opened by another application. Please close the application and try again.”, “File Save Error”, MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == DialogResult.Retry)

                {

                    BtnExport_Click(sender, e);

                }

            }

            catch (System.Exception sysException)

            {

                CoreLibrary.ExceptionHandler.HandleException(ConfigHandler.SystemUserID, sysException);

            }

        }

 

Imported from legacy forums. Posted by John (had 1205 views)