VS2005如何将GridView导出为Excel

 In this code snippet, I will show how you can export data from a GridView control to a Excel spreadsheet.

The Code

Listing 1: Default.aspx

<form id="form1" runat="server">
  <div>
  <asp:GridView ID="GridView1" runat="server">
  </asp:GridView>
  </div>
  <br />
  <asp:Button ID="BtnExport" runat="server" OnClick="BtnExport_Click"
  Text="Export to Excel" />
</form>

Listing 2: Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack)
  {
    BindData();
  }
}
 
private void BindData()
{
  string query = "SELECT * FROM Categories";
  SqlConnection myConnection = new SqlConnection(ConnectionString);
  SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
  DataSet ds = new DataSet();
  ad.Fill(ds, "Categories");
  GridView1.DataSource = ds;
  GridView1.DataBind();
}
 
private string ConnectionString
{
  get { return @"Server=localhost;Database=NorthWind;Trusted_Connection=true"; }
 
}
protected void BtnExport_Click(object sender, EventArgs e)
{
  Response.Clear();
  Response.AddHeader("content-disposition""attachment;filename=FileName.xls");
  Response.Charset = "";
 
  // If you want the option to open the Excel file without saving then
  // comment out the line below
  // Response.Cache.SetCacheability(HttpCacheability.NoCache);
  Response.ContentType = "application/vnd.xls";
  System.IO.StringWriter stringWrite = new System.IO.StringWriter();
  System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
  GridView1.RenderControl(htmlWrite);
  Response.Write(stringWrite.ToString());
  Response.End();
}
 

In the above listing, the GridView control will be populated with the data from the Categories table of the Northwind database. You will have to give the appropriate Server name or IP instead of localhost as server name in the above connection string.

If you use this code and try to export the GridView control, you will see an error message saying that the GridView control must be placed inside the form tags with the runat = server attribute.

笔者注:如出现这个要将GridView放在runat=server 里时,而你确实已经做了.但还是有这个问题.你就可以加入以下代码.

This is pretty confusing, since your GridView is already inside the form tags and also contains the runat = server attribute. You can easily resolve this error by adding the following lines.

Listing 3: Overiding VerifyRenderingInServerForm Method就是这里了:

public override void VerifyRenderingInServerForm(Control control)
{
  /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
     server control at run time. */

}

Yup, that’s it. Now, when you click the button, the GridView control will be exported correctly. It will prompt you either to open the file as it is or to save it elsewhere.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 当使用Java导出Excel文件时,有时会出现文件损坏的问题。这可能是由于文件格式不正确或者文件内容被破坏所致。常见的问题包括Excel打开后是黑底Excel、提示打开是损坏的实际是xlsx,但是后缀显示xls或者实际是xls但是后缀是xlsx等。这些问题一般会出现在使用Office打开Excel时,而使用WPS打开一般不会出现。这是因为WPS兼容了Office的历史版本功能,所以对于任何版本的Excel,WPS都能打开。而Office由于存在Office 2003及以前版本和Office 2007及以后版本的问题,所以会出现以上问题。解决这些问题的方法是确保导出Excel文件的格式正确,并且后缀与实际格式一致。另外,还可以尝试使用WPS来打开导出Excel文件,以确保文件能够正确打开。\[1\] #### 引用[.reference_title] - *1* [java导出excel常见问题(一)--- 导出excel打开异常](https://blog.csdn.net/qq_41154902/article/details/128103022)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [导出GridviewExcel成功但文件已损坏?](https://blog.csdn.net/weixin_34321959/article/details/114842226)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值