PDA

View Full Version : Razor pages - EXT.net v7 - Export to excel Issue



Geovision
Jan 08, 2021, 8:34 AM
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

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.get RowsValues({ 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(XLC olor.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.

fabricio.murta
Jan 08, 2021, 6:23 PM
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 (https://examples5.ext.net/#/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 (https://stackoverflow.com/questions/42460198/return-file-in-asp-net-core-web-api).

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

Hope this helps!