你的位置:首页 > ASP.net教程

[ASP.net教程]csharp: Export DataTable to Excel using OpenXml 2.5 in asp.net


 //https://www.microsoft.com/en-us/download/details.aspx?id=5124 Open 

  

 /// <summary>  ///   /// </summary>  public partial class WebForm1 : System.Web.UI.Page  {    DataTable getData()    {      DataTable dt = new DataTable();      dt.Columns.Add("id", typeof(int));      dt.Columns.Add("name", typeof(string));      dt.Rows.Add(1, "geovindu");      dt.Rows.Add(2, "geov");      dt.Rows.Add(3, "塗斯博");      dt.Rows.Add(4, "趙雅芝");      dt.Rows.Add(5, " なわち日本語");      dt.Rows.Add(6, "처리한다");      dt.Rows.Add(7, "涂聚文");      dt.Rows.Add(8, "塗聚文");      return dt;    }    /// <summary>    ///     /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    protected void Page_Load(object sender, EventArgs e)    {      if (!IsPostBack)      {        BindGrid();      }    }    /// <summary>    ///     /// </summary>    private void BindGrid()    {      this.GridView1.DataSource = getData();      GridView1.DataBind();    }    /// <summary>    ///     /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    protected void Button1_Click(object sender, EventArgs e)    {            string rootPath = HttpContext.Current.Server.MapPath("~").ToString();      string localCopy = "塗聚文" + DateTime.Now.ToString("yyyyMMddHHmmssfff")+ ".xlsx"; //      string file = new ExcelHelper().ExportToExcel(getData(), "geovindu",localCopy);      File.Copy(file, rootPath + localCopy);      Response.Redirect(HttpUtility.UrlEncode(localCopy,System.Text.Encoding.UTF8));    }  }