GridView导出Excel需要注意的地方
2008-11-10 12:35
316 查看
将GridView中的数据导出为Excel,然后由用户选择是保存还是打开打印,虽然代码比较简单。
无非就是用IO流去写成为Excel,但是有很多细节稍不注意就为成为困扰。
还是写下来,以后碰到了来查查。
先贴个源码:
public override void VerifyRenderingInServerForm(Control control)
{ }
protected void Print_ServerClick(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
string filename = "attachment;filename=" + System.Web.HttpUtility.UrlEncode( this.DropDownList1.SelectedItem.Text + "年" + this.DropDownList2.SelectedItem.Text + "月技术交易奖酬金",System.Text.Encoding.UTF8)+".xls";
Response.AppendHeader("Content-Disposition", filename);
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.GVStart.AllowPaging = false;
BindGridView();
this.GVStart.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
this.GVStart.AllowPaging = true;
this.GVStart.PageIndex = 0;
BindGridView();
}
1:首先在打印页面的HTML<%@ Page />标签中加入 EnableEventValidation = "false",否则可能会出现
错误:只能在执行 Render() 的过程中调用 RegisterForEventValidation
2:重写方法 public override void VerifyRenderingInServerForm(Control control) ,否则会出现,GridView控件
必须出现在 RunAt="server" 标签中,原因嘛,就不在此啰嗦了。
3:文件名中文乱码,很简单用 System.Web.HttpUtility.UrlEncode转换一下为UTF8就好了。
4:至于分页的GridView。在输出前把分页禁用,绑定所有数据后输出,完成后在重新启用分页就好了。
至于还有其它的就照着上面的代码写没有其它了...
无非就是用IO流去写成为Excel,但是有很多细节稍不注意就为成为困扰。
还是写下来,以后碰到了来查查。
先贴个源码:
public override void VerifyRenderingInServerForm(Control control)
{ }
protected void Print_ServerClick(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
string filename = "attachment;filename=" + System.Web.HttpUtility.UrlEncode( this.DropDownList1.SelectedItem.Text + "年" + this.DropDownList2.SelectedItem.Text + "月技术交易奖酬金",System.Text.Encoding.UTF8)+".xls";
Response.AppendHeader("Content-Disposition", filename);
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.GVStart.AllowPaging = false;
BindGridView();
this.GVStart.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
this.GVStart.AllowPaging = true;
this.GVStart.PageIndex = 0;
BindGridView();
}
1:首先在打印页面的HTML<%@ Page />标签中加入 EnableEventValidation = "false",否则可能会出现
错误:只能在执行 Render() 的过程中调用 RegisterForEventValidation
2:重写方法 public override void VerifyRenderingInServerForm(Control control) ,否则会出现,GridView控件
必须出现在 RunAt="server" 标签中,原因嘛,就不在此啰嗦了。
3:文件名中文乱码,很简单用 System.Web.HttpUtility.UrlEncode转换一下为UTF8就好了。
4:至于分页的GridView。在输出前把分页禁用,绑定所有数据后输出,完成后在重新启用分页就好了。
至于还有其它的就照着上面的代码写没有其它了...
相关文章推荐
- exp\expdp导出需要注意的地方
- php导出excel 大数据 时候需要注意的问题
- gridview 导出Excel时注意的问题
- exp\expdp导出需要注意的地方
- C# 将GridView导出为Excel注意事项
- 把数据导出Excel 出现异常:HRESULT:0x800A03EC 需要注意数据中的"="号
- Pear 的Excel类写数据需要注意的地方
- [译]ASP.Net 2.0: Export GridView to Excel (转) 如果GridView中有其它控件,比如Checkboxes,Dropdownlists,我们需要将它转换为其相关的值,以下递归就用于导出Excel前的准备工作,将各类控件转换为其相关值.
- 大数据量导出到Excel时需要注意的问题
- GridView导出Excel方法源码以及注意事项
- VS2005中 GridView导入Excel的导入需要注意的几点
- 初次用SqlServer查看本地的Excel文件时需要注意的地方
- gridview导出到excel的几个注意事项
- VS2005中 GridView导入Excel的导入需要注意的几点
- 大数据量导出到Excel时需要注意的问题
- VS2005中 GridView导入Excel的导入需要注意的几点
- 从GridView导出数据到Excel时的注意事项
- 3DMAX导出插件开发过程中需要注意的一些地方
- 使用aspose.cell导出excel需要注意什么?
- GridView完美快速导出到Excel(超强)