GridView中的数据导出到Excel方法(经测试完整)
2007-09-20 10:38
537 查看
GridView中的数据导出到Excel方法(经测试完整)
protected void Button2_Click(object sender, EventArgs e)
{
if (this.GridView1.Rows.Count == 0)
{
Response.Write("<script>alert('没有查找到数据,无法导出!')");
}
else
{
this.GridView1.AllowPaging = false; // 将有分页的GridView中的数据全部导出到Excel
gvBond();
export("application/ms-excel", "工作人员.xls");
// 换成 export("application/ms-word", "工作人员.doc"); 那么导出的就是Word格式的了.
this.GridView1.AllowPaging = true;
gvBond();
}
}
public void export(string FileType, string FileName)
{
string style = @"<style>.text{mso-number-format:/@}</script>";//导入到excel时,保存表里数字列中前面存在的 0 .
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
this.GridView1.AllowPaging = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
this.GridView1.RenderControl(htw);
Response.Write(style);
Response.Write(sw.ToString());
//Response.Write(dt.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
//在后台中重载VerifyRenderingInServerForm()方法,否则报错为“类型"GridView"的控件"GridView1"必须放在具有 runat=server 的窗体标记内“
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[3].Attributes.Add("class", "text");//在数字列前存在的 0 的列中加入 class 样式 以便保存 0
}
}
注:如果GricView中有分页的话,导出到Excel时就会报错.可通过修改页文件可以修正这个问题:EnableEventValidation = "false".
[align=left]<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>[/align]
protected void Button2_Click(object sender, EventArgs e)
{
if (this.GridView1.Rows.Count == 0)
{
Response.Write("<script>alert('没有查找到数据,无法导出!')");
}
else
{
this.GridView1.AllowPaging = false; // 将有分页的GridView中的数据全部导出到Excel
gvBond();
export("application/ms-excel", "工作人员.xls");
// 换成 export("application/ms-word", "工作人员.doc"); 那么导出的就是Word格式的了.
this.GridView1.AllowPaging = true;
gvBond();
}
}
public void export(string FileType, string FileName)
{
string style = @"<style>.text{mso-number-format:/@}</script>";//导入到excel时,保存表里数字列中前面存在的 0 .
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
this.GridView1.AllowPaging = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
this.GridView1.RenderControl(htw);
Response.Write(style);
Response.Write(sw.ToString());
//Response.Write(dt.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
//在后台中重载VerifyRenderingInServerForm()方法,否则报错为“类型"GridView"的控件"GridView1"必须放在具有 runat=server 的窗体标记内“
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[3].Attributes.Add("class", "text");//在数字列前存在的 0 的列中加入 class 样式 以便保存 0
}
}
注:如果GricView中有分页的话,导出到Excel时就会报错.可通过修改页文件可以修正这个问题:EnableEventValidation = "false".
[align=left]<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>[/align]
相关文章推荐
- Gridview数据导出excel时身份证号码为科学计数法的解决方法
- GridView中的数据导出到Excel方法(包含处理模板列)
- 将repeater、datalist、gridview中的数据导出到Excel中的简单方法
- Gridview数据导出excel时身份证号码为科学计数法的解决方法
- 将repeater、datalist、gridview中的数据导出到Excel中的简单方法
- GridView中的数据导出到Excel方法(包含处理模板列)
- 从gridview 中查询数据导出Excel 简单方法
- GridView中的数据导出到Excel方法(包含处理模板列)
- 导出gridview数据到Excel
- 动态创建gridview,导出Excel数据
- 怎么样把GridView里的数据导出到Excel里
- 把数据导出到Excel的几种方法说明
- Java中Excel表格数据的导入和导出步骤和方法
- Java利用xml将大批量数据导出到excel的一个方法
- gridview --导出execl 方法及身份证号灯数据类型被转换为数字导致数据错误。及导出数据方法。
- Asp.net 2.0 GridView数据导出Excel文件(示例代码下载)
- GridView如何将分页数据全部导出为EXCEL?
- 使用RenderControl方法把GridView控件导出到EXCEL
- SQL Server数据表在编程中实现导出EXCEL的几种方法
- GridView导出数据到Excel(形如身份证等数据的处理)