我想导出gridview里面的值到excel里面,但是我不要导出后面的按钮或者linkbutton之类的,只要值就可以,要识别中英文分页也可以导出,就先这点要求吧,我写的导出来不识别中文改不来。大虾们帮帮我,给个详细代码,谢谢
解决方案 »
- C:\Program Files\Microsoft.NET下没有FramWork文件夹???
- 救急。登陆控制。
- 如何使用本地服务器上的图片??
- 心情好,送分了
- 怎样打印Repeater或DataGrid里面的数据
- 正则表达式是JS的独特功能吗﹖C# for Asp.Net 有没有类似的用法﹖
- 有人没有人碰到过这种问题?text field方面的问题。
- sql server中如何添加新的一列,而这一列中的值是前两列的乘积
- 求一个通用的web数据显示页面表
- 在web.config文件中数据库连接字符串中的数据库路径怎么写?绝对?相对?
- web.config 中怎么配置端口呀
- 世界多么美好,百度豆丁效果多么重要,我们如此思考。
protected void export_Click(object sender, EventArgs e) { if (this.MachineList.Rows.Count == 0) { Response.Write("<script>alert('没有查找到数据,无法导出!')</script>"); } else { this.MachineList.AllowPaging = false; // 将有分页的GridView中的数据全部导出到Excel Bind(); Export("application/ms-excel", "设备信息.xls"); // 换成 export("application/ms-word", "设备信息.doc"); 那么导出的就是Word格式的了. this.MachineList.AllowPaging = true; Bind(); } } 二、导出主函数 public void Export(string FileType, string FileName) { string style = @"<style>.text{mso-number-format:@}</script>";//导入到excel时,保存表里数字列中前面存在的 0 . PrepareGridViewForExport(MachineList);//将模版列显示出来 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.MachineList.AllowPaging = false; System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true); StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); this.MachineList.RenderControl(htw); Response.Write(style); Response.Write(sw.ToString()); //Response.Write(dt.ToString()); Response.End(); } /// <summary> /// 重写VerifyRenderingInServerForm(Control control)方法,以确保在程序运行时,指定的GridView控件总是位于"<form runat="server"></form>"标记内. /// 该方法用来确认在运行时为指定的ASP.NET服务器控件呈现<form runat="server">标记. /// 必须位于<form runat="server">中的控件可以在呈现之前调用此方法,以便在控件被置于标记外时显示错误信息;另外,发送回或依赖于注册的脚本块的 /// 控件应该在Control.Render()方法的重写中调用此方法. /// 如果回发或使用客户端脚本的服务器控件没有包含在<form runat="server"> 标记中,它们将无法正常工作,这时,可以通过重写VerifyRenderingInServerForm( /// Control control)方法使用程序正常运行. /// </summary> /// <param name="control"></param> public override void VerifyRenderingInServerForm(Control control) { // Confirms that an HtmlForm control is rendered for //the specified ASP.NET server control at run time. } 三、如果GridView存在模板列,其中包含子控件,例如CheckBox等,导出EXCEL后就会出现该区域的不规律。所以要对模板列单独处理(转载) public void PrepareGridViewForExport(Control gv)//模式化特殊元素 flashcong { LinkButton lb = new LinkButton(); Literal l = new Literal(); string name = String.Empty; for (int i = 0; i < gv.Controls.Count; i++) { if (gv.Controls[i].GetType() == typeof(LinkButton)) { l.Text = (gv.Controls[i] as LinkButton).Text; gv.Controls.Remove(gv.Controls[i]); gv.Controls.AddAt(i, l); } else if (gv.Controls[i].GetType() == typeof(DropDownList)) { l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text; gv.Controls.Remove(gv.Controls[i]); gv.Controls.AddAt(i, l); } else if (gv.Controls[i].GetType() == typeof(CheckBox)) { l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False"; gv.Controls.Remove(gv.Controls[i]); gv.Controls.AddAt(i, l); } else if (gv.Controls[i].GetType() == typeof(ImageButton)) { l.Text = "图片"; gv.Controls.Remove(gv.Controls[i]); gv.Controls.AddAt(i, l); } if (gv.Controls[i].HasControls()) { PrepareGridViewForExport(gv.Controls[i]); } } } 注:对于模板列的处理还有一特殊情况,我在GridView使用时,用了<a></a>在客户端打开对话框的方法。在导出时,这一列不是模板列控件,但导出后Excel中会显示有下划线的链接,所以想去掉它。
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:LinkButton ID="lbExcel" runat="server" onclick="lbExcel_Click">导出Excel</asp:LinkButton>
<asp:GridView ID="gvwStudent" runat="server">
</asp:GridView>
</form>
</body>
</html>后台代码:using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ControlClassspace;
using System.Text;
using System.IO; public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gridViewBind();
}
}//绑定gridView的函数 public void gridViewBind()
{
string sqlText = @"SELECT * FROM Student";
ControlClass controlClass = new ControlClass();
controlClass.BindGridView(gvwStudent, sqlText);
} protected void lbExcel_Click(object sender, EventArgs e)
{
Export("application/vnd.xls", "xx.xls");
} //重写此方法,使没有包含在<from runat="server">中的客户端脚本 //gridview中必须要重写此方法,但datagrid中不需要
public override void VerifyRenderingInServerForm(Control control)
{ /*以确认在运行时为指定的ASP.NET 控件呈现HtmlForm 控件*/
}
/// <summary>
/// 导出gridview函数,提出来的好处是可以放在公用函数中
/// </summary>
/// <param name="FileType"></param>
/// <param name="FileName"></param>
private void Export(string FileType,string FileName)
{
Response.Charset = "gb2312";//字体
//解决中文乱码
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
//the title of Excel
Response.AddHeader("content-disposition", "attachment;filename="+HttpUtility.UrlEncode(FileName,Encoding.UTF8).ToString());
//To sign the File type
Response.ContentType = FileType;
//提交后不回显,加快系统速度
this.EnableViewState = false;
//实现一个用于将信息写入字符串的 TextWriter。该信息存储在基础 StringBuilder 中。
StringWriter tw = new StringWriter();
//将标记字符和文本写入到 ASP.NET 服务器控件输出流。此类提供 ASP.NET 服务器控件在向客户端呈现标记时所使用的格式设置功能。
HtmlTextWriter htw = new HtmlTextWriter(tw);
//将服务器控件的内容输出到所提供的 HtmlTextWriter 对象中;如果已启用跟踪功能,则存储有关控件的跟踪信息。
gvwStudent.RenderControl(htw);
Response.Write(tw.ToString());
Response.End();
gvwStudent.AllowPaging = true;//导出后先取消分页,以便能将所有数据导出。
gridViewBind();//再重新绑定一次数据
}
}
public void ExportToExcel(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
Response.ContentType = FileType;
GridView1.Columns[6].Visible = false;//隐藏“编辑”列
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
//导出到Excel
protected void BtnOutPut_Click(object sender, EventArgs e)
{
ExportToExcel("application/ms-excel", "信息.xls");
}
public override void VerifyRenderingInServerForm(Control control)
{ }
我是怎么写的