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

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #17230 |

    Hi

    We are looking for exporting the data grid into xlsx file, but it stores only as XML format (with a .XML file name). Is there a way to forcibly rename that file to xlsx (since XLS supports open XML standard). Any help would be greatly appreciated.
    Thanks

    John

     

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

    User (Old forums)
    Member
    Post count: 23064

    Hi,

    you have to include / Imports Xceed.FileSystem and then instanciate a DiskFile object (references by an AbstractFile, of course) that uses the exported filename for its constructor. 

    Once this is done, you just have to use the AbstractFile Name property to rename the file using Path.GetFileNameWithoutExtension(originalFileName)+ “.(the extension you want)”  ( requires the use of System.IO for Path.GetFileNameWithoutExtension() )

    The filename would be renamed to the new name instantly.

    Imported from legacy forums. Posted by Ghislain (had 282 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks for your reply, this Xceed.FileSystem is part of Xceed zip component and should I have to install that as well inorder to take advantage of this or just copy the DLL in my grid designer and it should work? Please let me know/

    Thanks

    John

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

    User (Old forums)
    Member
    Post count: 23064

    Hi,

    Ctrl-W, S to Open Solution Explorer…  (in VB.NET, you also need to click on Show All Files to be shown the “References”.)  Right-Click “Add references…”, select the .NET tab, scroll down to Xceed.FileSystem.Dll, Click this and Accept / OK. 

    Also,

    Imports Xceed.FileSystem  (VB.NET)

    or

    using Xceed.FileSystem;  (C#)

    at the top of your file.

    Imported from legacy forums. Posted by Ghislain (had 336 views)

    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)

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.