用datagrid,不要用GridView(Vs2005也有DataGrid),然后这样的代码 Response.Clear() Response.Buffer = True Response.Charset = "GB2312" Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.DateTime.Today.ToShortDateString() + ".xls") Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312") Response.ContentType = "application/ms-excel" Me.EnableViewState = False Dim myCItrad As New System.Globalization.CultureInfo("ZH-CN", True) Dim oStringWriter As New System.IO.StringWriter(myCItrad) Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter) GridView1.RenderControl(oHtmlTextWriter) Response.Write(oStringWriter.ToString()) Response.End()
public static void ToExcel(System.Web.UI.Control ctl,string FileName) { HttpContext.Current.Response.Charset ="UTF-8"; HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default; HttpContext.Current.Response.ContentType ="application/ms-excel"; HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName + ".xls"); ctl.Page.EnableViewState =false; System.IO.StringWriter tw = new System.IO.StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); ctl.RenderControl(hw); HttpContext.Current.Response.Write(tw.ToString()); HttpContext.Current.Response.End(); } public override void VerifyRenderingInServerForm(Control control) { // Confirms that an HtmlForm control is rendered for } protected void Button_Click(object sender, EventArgs e) { ToExcel(myGridView,"huadan"); }我这几天刚刚做了一个,上面的代码在我这里可以用,注意必须加上中间这个函数。
Response.Clear()
Response.Buffer = True
Response.Charset = "GB2312"
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.DateTime.Today.ToShortDateString() + ".xls")
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312")
Response.ContentType = "application/ms-excel"
Me.EnableViewState = False
Dim myCItrad As New System.Globalization.CultureInfo("ZH-CN", True)
Dim oStringWriter As New System.IO.StringWriter(myCItrad)
Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
GridView1.RenderControl(oHtmlTextWriter)
Response.Write(oStringWriter.ToString())
Response.End()
{
HttpContext.Current.Response.Charset ="UTF-8";
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType ="application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName + ".xls");
ctl.Page.EnableViewState =false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
protected void Button_Click(object sender, EventArgs e)
{
ToExcel(myGridView,"huadan");
}我这几天刚刚做了一个,上面的代码在我这里可以用,注意必须加上中间这个函数。
程序是想出来的,研究出来的,不是问出来的,否则你问我我问他问到最前面那个人找谁问去?总是出错就从错的地方找你错的原因,为什么别人不错就你错呢??
而且不一定要挂死在一棵树上,上面那样的代码虽然在大部分情况下是有用的,不过如果内容过少,比如只有1行....导出是正确的,不过用Excel打开会乱码,这是因为这种方式出来的是html标签而不是标准的Excel,所以Excel需要在打开的时候自动判断编码,如果内容过少,判断编码就很可能出错(当然也可能不错...),对于中文版的Excel2000,如果用utf8,几乎两行以内一定乱码...换种思路不要挂死一种方法,方法是想出来的.你自己想吧,思考后没辙再问别人..... 我负责任地告诉你方法多的是,就看你想像力.需要提示的话我邮箱[email protected] 跟别人要源码不是好的学习方法
还有请问存在哪个目录下了??