Función para crear excel en c# a partir de la lista que sea retornada en un datacall

Lucio Flores
2014-08-06T14:40:14Z

Función para crear excel en c#

| /*Función patrocinada por Gerry*/
|
| public ActionResult ExportToExcelReporteCobros()
| {
| var lst_report = DataCalls.GetReporteCobros();
| if (lst_report == null)
| {
| return Content("error");
| }
| var filename = "";
| var json = JsonConvert.SerializeObject(lst_report);
| StoreSubmitDataEventArgs eSubmit = new StoreSubmitDataEventArgs(json, null);
| DataSet ds = new DataSet();
| ds.ReadXml(new XmlTextReader(new StringReader(eSubmit.Xml.OuterXml)));
|
| string _outer = eSubmit.Xml.OuterXml;
|
| if (lst_report != null || _outer == "<records />")
| {
| /*Folder para crear archivos temporales*/
| filename = Server.MapPath("/Areas/Configuracion/Files/" + Guid.NewGuid().ToString().Replace("-", "") + ".xls");
| try
| {
| System.IO.File.Delete(filename);
| }
| catch { }
| StringBuilder sb = new StringBuilder();
| using (StringWriter sw = new StringWriter(sb))
| {
| using (System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw))
| {
| try
| {
| System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();
| dg.DataSource = ds.Tables[0];
| dg.DataBind();
| dg.RenderControl(htw);
| }
| catch { }
| }
| };
|
| System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
| Byte[] bytes = encoding.GetBytes(sb.ToString());
|
| FileStream sf = new FileStream(filename, FileMode.CreateNew);
| sf.Write(bytes, 0, bytes.Length);
| sf.Flush();
| sf.Dispose();
|
| }
| else
| {
| filename = "error";
| }
| return Content(filename);
|
| }
|