将数据导出到excel和pdf ASP.NET [英] Export data to excel and pdf ASP.NET

查看:144
本文介绍了将数据导出到excel和pdf ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的观看结果保存在网络中以excel和/或PDF。我必须显示对话框保存以制作路径并选择文件的扩展名来制作文件。我用asp.net,你能帮帮我怎么做吗?



我试过的:



 public ActionResult Export2Excel()
{
var products = new System.Data.DataTable(teste);
products.Columns.Add(col1,typeof(int));
products.Columns.Add(col2,typeof(string));

products.Rows.Add(1,product 1);
products.Rows.Add(2,product 2);
products.Rows.Add(3,product 3);
products.Rows.Add(4,product 4);
products.Rows.Add(5,product 5);


var grid = new GridView();
grid.DataSource = products;
grid.DataBind();

Response.ClearContent();
Response.Buffer = true;
Response.AddHeader(content-disposition,attachment; filename = Result.xls);
Response.ContentType =application / ms-excel;

Response.Charset =;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);

grid.RenderControl(htw);

Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();

返回查看(MyView);
}

解决方案

这是一个链接,它将向您展示如何在C#和VB中导出: 如何在ASP.Net中将GridView导出为Word / Excel / PDF / CSV ASP.NET论坛 [ ^ ]

I want to save my view result in web to excel and or PDF. I must show dialog save to make path and choose the file's extension to make file. I use asp.net, Could you help me how to do this?

What I have tried:

public ActionResult Export2Excel()
  {
      var products = new System.Data.DataTable("teste");
      products.Columns.Add("col1", typeof(int));
      products.Columns.Add("col2", typeof(string));

      products.Rows.Add(1, "product 1");
      products.Rows.Add(2, "product 2");
      products.Rows.Add(3, "product 3");
      products.Rows.Add(4, "product 4");
      products.Rows.Add(5, "product 5");


      var grid = new GridView();
      grid.DataSource = products;
      grid.DataBind();

      Response.ClearContent();
      Response.Buffer = true;
      Response.AddHeader("content-disposition", "attachment; filename=Result.xls");
      Response.ContentType = "application/ms-excel";

      Response.Charset = "";
      StringWriter sw = new StringWriter();
      HtmlTextWriter htw = new HtmlTextWriter(sw);

      grid.RenderControl(htw);

      Response.Output.Write(sw.ToString());
      Response.Flush();
      Response.End();

      return View("MyView");
  }

解决方案

Here is a link that will show you how to export in both C# and VB: How to Export GridView To Word/Excel/PDF/CSV in ASP.Net | The ASP.NET Forums[^]


这篇关于将数据导出到excel和pdf ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆