-
Export to excel and csv
Hi guy,
I tried export data to excel and csv by the following code.
In the C# code
Code:
[DirectMethod]
public void DownloadFile(string type)
{
string json = GridData.Value.ToString();
StoreSubmitDataEventArgs eSubmit = new StoreSubmitDataEventArgs(json, null);
System.Xml.XmlNode xml = eSubmit.Xml;
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.Clear();
switch (type)
{
case "Excel":
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment; filename=submittedData.xls");
System.Xml.Xsl.XslCompiledTransform xtExcel = new System.Xml.Xsl.XslCompiledTransform();
xtExcel.Load(HttpContext.Current.Server.MapPath("Excel.xsl"));
xtExcel.Transform(xml, null, response.OutputStream);
break;
case "CSV":
response.ContentType = "application/octet-stream";
response.AddHeader("Content-Disposition", "attachment; filename=submittedData.csv");
System.Xml.Xsl.XslCompiledTransform xtCsv = new System.Xml.Xsl.XslCompiledTransform();
xtCsv.Load(HttpContext.Current.Server.MapPath("Csv.xsl"));
xtCsv.Transform(xml, null, response.OutputStream);
break;
}
response.End();
}
and javascript code...
Code:
ToExcel: function () {
GalaxyReport.FORMS._1.saveData();
App.direct.DownloadFile('Excel',{
isUpload: true,
buffer: 300,
success: function (result) {
Ext.Msg.alert('Message', result);
setTimeout(function () {
}, 2000);
},
failure: function (error, response) {
Ext.Msg.show({ title: 'Message', icon: Ext.MessageBox.ERROR, msg: error, buttons: Ext.Msg.OK });
}
});
setTimeout(function () {
}, 2000);
},
ToCSV: function () {
GalaxyReport.FORMS._1.saveData();
App.direct.DownloadFile('CSV',{
isUpload: true,
buffer: 300,
success: function (result) {
Ext.Msg.alert('Message', result);
setTimeout(function () {
}, 2000);
},
failure: function (error, response) {
Ext.Msg.show({ title: 'Message', icon: Ext.MessageBox.ERROR, msg: error, buttons: Ext.Msg.OK });
}
});
setTimeout(function () {
}, 2000);
},
saveData: function () {
var grid = Ext.getCmp("gplVoucher");
var d = Ext.getCmp("GridData");
d.setValue(Ext.encode(grid.getRowsValues({ selectedOnly: false })));
}
Code:
btnToExcel.Listeners.Click.Handler = SCOPE + ".ToExcel();";
btnToCSV.Listeners.Click.Handler = SCOPE + ".ToCSV();";
When i run in visual studio it work fine, but when i publish to the host, it download with blank data for excel file and something code for csv file.
I using ext.net version 4.8.2.0
Someone help me please
Many thanks
-
Full source code
C# code:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Ext.Net;
using GalaxyReport.Classes;
using System.Web.Script.Serialization;
using GalaxyReport.RBSL_ReportTitle;
using System.Globalization;
using System.Data;
namespace GalaxyReport.Forms.mkt._1
{
[DirectMethodProxyID(IDMode = DirectMethodProxyIDMode.None)]
public class _1: Viewport
{
string orderID = string.Empty;
public const string SCOPE = "GalaxyReport.FORMS._1";
DataTable dtMain;
Ext.Net.FormPanel pnlOrder;
Ext.Net.FormPanel pnlVoucher;
Ext.Net.MultiCombo cboCin;
Ext.Net.TextField txtStoreName;
Ext.Net.DateField txtCoGDTu;
Ext.Net.DateField txtCoGDDen;
Ext.Net.DateField txtKhongGDTu;
Ext.Net.DateField txtKhongGDDen;
Ext.Net.Button btnExecute;
Ext.Net.Button btnToExcel;
Ext.Net.Button btnToCSV;
Ext.Net.Store strVoucher;
Ext.Net.GridPanel gplVoucher;
Ext.Net.Hidden GridData;
public _1()
{
InitComponent();
InitLogis();
}
private void InitLogis()
{
btnExecute.Listeners.Click.Handler = SCOPE + ".Execute();";
btnToExcel.Listeners.Click.Handler = SCOPE + ".ToExcel();";
btnToCSV.Listeners.Click.Handler = SCOPE + ".ToCSV();";
}
private void InitComponent()
{
GridData = new Ext.Net.Hidden { ID= "GridData" };
txtStoreName = new TextField { LabelWidth = 150, Anchor = "100%", FieldLabel = "Store Name", Name = "StoreName", ID = "txtStoreName", ReadOnly = true, SelectOnFocus = true };
txtCoGDTu = new DateField { LabelWidth = 150, Anchor = "100%", FieldLabel = "Có Giao Dịch Từ", Name = "CoGDTu", ID = "txtCoGDTu", SelectOnFocus = true, Format = "yyyy-MM-dd" };
txtCoGDDen = new DateField { LabelWidth = 150, Anchor = "100%", FieldLabel = "Có Giao Dịch Äến", Name = "CoGDDen", ID = "txtCoGDDen", SelectOnFocus = true, Format = "yyyy-MM-dd" };
txtKhongGDTu = new DateField { LabelWidth = 150, Anchor = "100%", FieldLabel = "Không Giao Dịch Từ", Name = "KhongGDTu", ID = "txtKhongGDTu", SelectOnFocus = true, Format = "yyyy-MM-dd" };
txtKhongGDDen = new DateField { LabelWidth = 150, Anchor = "100%", FieldLabel = "Không Giao Dịch Äến", Name = "KhongGDDen", ID = "txtKhongGDDen", SelectOnFocus = true, Format = "yyyy-MM-dd" };
cboCin = new MultiCombo
{
ID = "CinList",
LabelWidth = 150,
Anchor = "100%",
FieldLabel = "Cinema",
DisplayField = "Name",
ValueField = "ID",
SelectionMode = MultiSelectMode.Selection,
Store = {
new Store {
Model = {
new Model {
Fields = {
new ModelField { Name = "ID" },
new ModelField { Name = "Name" }
}
}
}
}
}
};
btnExecute = new Button { Icon = Icon.ArrowRefresh, Text = "Execute", ToolTip = "Execute" };
btnToExcel = new Button { Icon = Icon.PageExcel, Text = "To Excel", ToolTip = "To Excel" };
btnToCSV = new Button { Icon = Icon.PageAttach, Text = "To CSV", ToolTip = "To CSV" };
pnlOrder = new FormPanel
{
Region = Region.North,
Icon = Ext.Net.Icon.House,
Title = "Order",
Border = true,
CollapseMode = CollapseMode.Header,
Collapsible = true,
ID = "frmHeader",
TrackResetOnLoad = true,
Layout = "Hbox",
Header = false,
Items =
{
new Panel {
Layout = "Anchor",
Flex = 1,
BodyPaddingSummary = "10 10 0 10",
Border = false,
Items = {
txtStoreName, txtCoGDTu, txtCoGDDen
}
},
new Panel {
Layout = "Anchor",
Flex = 1,
BodyPaddingSummary = "10 10 0 10",
Border = false,
Items = {
cboCin, txtKhongGDTu, txtKhongGDDen, GridData
}
}
}
};
//------------------------------------------------------------------------------------------------------------------------------//
Model model = new Model
{
IDProperty = "MembershipID",
Fields = {
new ModelField { Name = "MembershipID" }
}
};
strVoucher = new Store
{
Model = { model },
Sorters = { new DataSorter { Direction = SortDirection.ASC, Property = "MembershipID" } },
AutoLoad = true,
RemotePaging = true,
WarningOnDirty = false
};
gplVoucher = new GridPanel
{
ID = "gplVoucher",
BodyCls = "line-body-border",
CtCls = "line-border",
ColumnModel =
{
Columns = {
new Column{ DataIndex = "MembershipID", Text = "MembershipID", Width = 150 }
}
},
TopBar = { new Toolbar { Items = { btnExecute, new ToolbarFill(), btnToExcel, btnToCSV } } },
Plugins = { new FilterHeader { } },
BottomBar = { new PagingToolbar { HideRefresh = true } },
SelectionModel = { new RowSelectionModel { Mode = SelectionMode.Single } }
};
gplVoucher.Store.Add(strVoucher);
pnlVoucher = new FormPanel { ID = "pnlVoucher", Region = Ext.Net.Region.Center, Border = false, Flex = 1, Items = { gplVoucher }, Padding = 5, Layout = "Fit" };
this.ID = "pageMain";
this.Layout = "BorderLayout";
this.Items.AddRange(new ItemsCollection<Ext.Net.AbstractComponent> { pnlOrder, pnlVoucher });
}
[DirectMethod(ShowMask = true, Msg = "Loading....")]
public void Execute(string values)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
dynamic data = serializer.Deserialize(values, typeof(object));
string CoGDTu, CoGDDen, KhongGDTu, KhongGDDen, StoreName = "", Ciname = "";
List<RBSL_ReportTitle.Params> timeRange = new List<Params>();
object[] CinList;
foreach (var item in data)
{
string k = item.Key;
switch (k)
{
case "CoGDTu":
DateTime t1 = DateTime.Parse(item.Value);
t1 = t1.ChangeTime(6, 0, 0, 0);
CoGDTu = t1.ToString("yyyy-MM-dd hh:ss:mm");
timeRange.Add(new Params { ParamName = "@fd1", ParamValue = CoGDTu });
break;
case "CoGDDen":
DateTime t2 = DateTime.Parse(item.Value);
t2 = t2.ChangeTime(6, 0, 0, 0);
CoGDDen = t2.ToString("yyyy-MM-dd hh:ss:mm");
timeRange.Add(new Params { ParamName = "@td1", ParamValue = CoGDDen });
break;
case "KhongGDTu":
DateTime t3 = DateTime.Parse(item.Value);
t3 = t3.ChangeTime(6, 0, 0, 0);
KhongGDTu = t3.ToString("yyyy-MM-dd hh:ss:mm");
timeRange.Add(new Params { ParamName = "@fd2", ParamValue = KhongGDTu });
break;
case "KhongGDDen":
DateTime t4 = DateTime.Parse(item.Value);
t4 = t4.ChangeTime(6, 0, 0, 0);
KhongGDDen = t4.ToString("yyyy-MM-dd hh:ss:mm");
timeRange.Add(new Params { ParamName = "@td2", ParamValue = KhongGDDen });
break;
case "StoreName":
StoreName = item.Value;
break;
case "CinList":
CinList = item.Value;
foreach (string cin in CinList)
{
Ciname += "'" +cin + "', " ;
}
Ciname = Ciname.Substring(0, Ciname.Length - 2);
break;
}
}
RBSL_ReportTitle.BSL_ReportTitleClient client = new BSL_ReportTitleClient();
DataSet ds = client.ExecStore("spGLXReport1", Ciname, timeRange.ToArray());
dtMain = ds.Tables[0];
strVoucher.DataSource = dtMain;
strVoucher.DataBind();
}
[DirectMethod]
public void DownloadFile(string type)
{
string json = GridData.Value.ToString();
StoreSubmitDataEventArgs eSubmit = new StoreSubmitDataEventArgs(json, null);
System.Xml.XmlNode xml = eSubmit.Xml;
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.Clear();
switch (type)
{
case "Excel":
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment; filename=submittedData.xls");
System.Xml.Xsl.XslCompiledTransform xtExcel = new System.Xml.Xsl.XslCompiledTransform();
xtExcel.Load(HttpContext.Current.Server.MapPath("Excel.xsl"));
xtExcel.Transform(xml, null, response.OutputStream);
break;
case "CSV":
response.ContentType = "application/octet-stream";
response.AddHeader("Content-Disposition", "attachment; filename=submittedData.csv");
System.Xml.Xsl.XslCompiledTransform xtCsv = new System.Xml.Xsl.XslCompiledTransform();
xtCsv.Load(HttpContext.Current.Server.MapPath("Csv.xsl"));
xtCsv.Transform(xml, null, response.OutputStream);
break;
}
response.End();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!Ext.Net.X.IsAjaxRequest)
{
this.ResourceManager.AddDirectMethodControl(this);
}
Store store = cboCin.GetStore();
store.DataSource = Cinnemas.GetAllCinCRM();
}
}
}
Javascript code
Code:
Ext.ns('GalaxyReport.FORMS');
GalaxyReport.FORMS._1 = {
Execute: function () {
var frm = Ext.getCmp("frmHeader");
var data = frm.getForm().getFieldValues(false, 'dataIndex');
data.undefined = undefined;
var values = Ext.encode(data);
App.direct.Execute(values, {
failure: function (error, response) {
Ext.Msg.show({ title: 'Báo lỗi', icon: Ext.MessageBox.ERROR, msg: error, buttons: Ext.Msg.OK });
}
});
},
ToExcel: function () {
GalaxyReport.FORMS._1.saveData();
App.direct.DownloadFile('Excel',{
isUpload: true,
buffer: 300,
success: function (result) {
Ext.Msg.alert('Message', result);
setTimeout(function () {
}, 2000);
},
failure: function (error, response) {
Ext.Msg.show({ title: 'Message', icon: Ext.MessageBox.ERROR, msg: error, buttons: Ext.Msg.OK });
}
});
setTimeout(function () {
}, 2000);
},
ToCSV: function () {
GalaxyReport.FORMS._1.saveData();
App.direct.DownloadFile('CSV',{
isUpload: true,
buffer: 300,
success: function (result) {
Ext.Msg.alert('Message', result);
setTimeout(function () {
}, 2000);
},
failure: function (error, response) {
Ext.Msg.show({ title: 'Message', icon: Ext.MessageBox.ERROR, msg: error, buttons: Ext.Msg.OK });
}
});
setTimeout(function () {
}, 2000);
},
saveData: function () {
var grid = Ext.getCmp("gplVoucher");
var d = Ext.getCmp("GridData");
d.setValue(Ext.encode(grid.getRowsValues({ selectedOnly: false })));
}
}
-
Hello @phongdd10!
I'm sorry we took so long to reply the inquiry you sent but, as you shown, the issue is when you publish the solution to an IIS server, so it's hard for us to tell what's wrong on that.
The most likely reason you don't get it to work on the production environment is a misconfiguration on the GalaxyReport tool, which is a third-party we are not able to talk about here.
You should try to build a very simple application using this library (a static "hello world" report, maybe), and then ensure it works both in the development project and when sent to production, so you troubleshoot the issue from its root, and then climb up in complexity until you isolate the cause of the issue.
It really, really looks like not an issue with Ext.NET at all -- as far as I can see by a brief lookup on your very extensive code.
To get faster help here, we'd strongly advice you on reviewing our guidelines, which has some tips on simplifying your test case scenarios. The simplifying process itself helps you find (by yourself) the reasons of issues in most cases:
- Tips for creating simplified code samples
- More Information Required
- Forum Guidelines
Hope this helps!
-
Hi fabricio.murta
I trying as your guide but it still there. Would you please watch video as link below
https://youtu.be/iG3dRLFmo5E
and i was downgrade version to 2.5.3
Thanks fabricio.murta
Dinh Phong
-
-
Hello @Phongdd10,
Again, without being able to run the code with a runnable test case, we can't be of much help. But here's my best shot:
Review your example to perform either like this ajax example: GridPanel > Miscellaneous > Data Export Ajax
Or this postback example: GridPanel > Miscellaneous > Data Export PostBack
At least here you can base your example off something that we know that works. If you want these examples in v2 (they should not have much difference anyway):
- Ext.NET 2: GridPanel > Miscellaneous > Data Export Ajax
- Ext.NET 2: GridPanel > Miscellaneous > Data Export PostBack
Remember we're in v4 forums, and the answers here are based on Ext.NET 4. Have you temporarily downgraded to 2.5.3 to test and got same result, or are you all time on Ext.NET 2.5.3? If so, we can move this thread to 2.x help for clarity.
Hope this helps!