Geovision
Jan 08, 2021, 7: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.
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.