Razor pages - EXT.net v7 - Export to excel Issue

  1. #1

    Razor pages - EXT.net v7 - Export to excel Issue

    Dear,

    Please note that I encountered an issue when exporting an ext-gridpanel to an excel sheet.
    Noting that I am using the ClosedXml library.

    Problem Description:
    Status Code:200
    Status Text:BADRESPONSE: Invalid or unexpected token

    Click image for larger version. 

Name:	Annotation 2021-01-08 102812.jpg 
Views:	66 
Size:	23.1 KB 
ID:	25486


       @*-- EXT.net Button*@
       <ext-button text="Export To Excel"  marginAsString="0 0 10 10">
           <listeners>
               <click fn="saveData" />
           </listeners>
       </ext-button>
       
    
       // Javascript function
     
         function saveData() {
                var data = App.GridPanel1.getRowsValues(
                    {
                        prepare: function (data, record) {
                        }
                    }),
                    columns = App.GridPanel1.getView().getGridColumns(),
                    headers = {};
    
                Ext.each(columns, function (c) {
                    headers[c.dataIndex] = c.text.replace(/ /g, "_");
                    headers[c.dataIndex] = headers[c.dataIndex].replace(/á/g, "a");
                    headers[c.dataIndex] = headers[c.dataIndex].replace(/é/g, "e");
                    headers[c.dataIndex] = headers[c.dataIndex].replace(/Ã*/g, "i");
                    headers[c.dataIndex] = headers[c.dataIndex].replace(/ó/g, "o");
                    headers[c.dataIndex] = headers[c.dataIndex].replace(/ú/g, "u");
                    headers[c.dataIndex] = headers[c.dataIndex].replace(/ñ/g, "n");
                });
    
                Ext.each(data, function (record) {
                    for (var field in record) {
                        record[headers[field]] = record[field];
                        delete record[field];
                    }
                });
    
                App.Hidden1.setValue(Ext.encode(data));
    
                console.log(Hidden1.value.toString());
    
                App.direct.CallExport(Hidden1.value.toString());
    
                //App.Hidden1.setValue(Ext.encode(App.GridPanel1.getRowsValues({ selectedOnly: false })));
            };
    
    
    
          // Code behind function
    
         [Direct]
            public IActionResult OnPostExportToExcel(String JsonFile)
            {
    
                
                    string json = JsonFile;
                    XmlNode xml = JsonConvert.DeserializeXmlNode("{\"Row\":" + json + "}", "root");
    
                    DataSet ds = new DataSet();
                    DataTable dt = new DataTable();
                    System.Xml.XmlReader xmr = new XmlNodeReader(xml);
                    ds.ReadXml(xmr);
                    if (ds.Tables.Count != 0)
                        dt = ds.Tables[0];
    
                    //  DataTable dt2 = getData();
    
                    using (var workbook = new XLWorkbook())
                    {
                        //var worksheet = workbook.Worksheets.Add("Sheet1");
                        var worksheet = workbook.Worksheets.Add(dt);
                        worksheet.TabColor = XLColor.BlueViolet;
                        worksheet.RowHeight = 12;
                        worksheet.ColumnWidth = 20;
                        worksheet.Row(1).Height = 30;
                        worksheet.Row(1).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                        worksheet.Row(1).Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                        worksheet.Row(1).Style.Fill.PatternType = XLFillPatternValues.Solid;
                        worksheet.Row(1).Style.Fill.SetBackgroundColor(XLColor.Gray);
                        worksheet.Row(1).Style.Alignment.WrapText = true;
    
                       
                      
                        using (var stream = new MemoryStream())
                        {
                        
                            workbook.SaveAs(stream);
                            var content = stream.ToArray();
    
                            return File(
                                content,
                               "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                                "tasktypes.xlsx");
                        }
                    }
              
            }//ExportToExcel
    in other hand, if you have another suggestions or examples, can you provide it to me? since I tried more than one solution without success.

    Thank you.
  2. #2
    Hello @Geovision!

    A direct request is not supposed to return a file. It should instead, return a response for the client. This response can contain information to let the client know what to do next.

    Then the returned information could contain a path for another request to actually download the generated file.

    Even if you look back at Ext.NET 5, you can see how it handles download requests like in this example: GridPanel > Miscellaneous > Export Data Ajax.

    In a similar manner, if you want to prepare the data via a direct event, that would work alright and let you do extensive validation and data preparation before making the client request the, then generated, file.

    I believe your question falls in the lines of this StackOverflow question: C# - Return file in ASP.NET Core Web API - Stack Overflow.

    So the answers there are probably what you're missing in order to fulfill your needs.

    Hope this helps!
    Fabrício Murta
    Developer & Support Expert

Similar Threads

  1. [CLOSED] Export data to excel in EXT NET MVC
    By pvera in forum 5.x Legacy Premium Help
    Replies: 5
    Last Post: Mar 13, 2020, 2:39 AM
  2. [CLOSED] Issue with Export to Excel in GridPanel
    By opendat2000 in forum 4.x Legacy Premium Help
    Replies: 3
    Last Post: Jul 19, 2016, 6:48 PM
  3. [CLOSED] close Ext.Net Window after export to excel
    By PriceRightHTML5team in forum 2.x Legacy Premium Help
    Replies: 1
    Last Post: Sep 04, 2013, 12:06 PM
  4. MVC Razor - export data to excel or csv
    By Ishrath in forum 2.x Help
    Replies: 4
    Last Post: Nov 14, 2012, 11:39 AM
  5. Export to excel: Grid Panel with multiple pages.
    By breakyoheart in forum 2.x Help
    Replies: 0
    Last Post: Aug 02, 2012, 8:09 PM

Posting Permissions