private void OutPut_Click(object sender, System.EventArgs e)
{
bExcel=true;
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312";
Response.AppendHeader("Content-Disposition","attachment;filename=FileName.xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
StudentDataGrid.AllowPaging=false;
Bind();//datagrid重新绑定
StudentDataGrid.DataBind();
this.StudentDataGrid.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
{
bExcel=true;
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312";
Response.AppendHeader("Content-Disposition","attachment;filename=FileName.xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
StudentDataGrid.AllowPaging=false;
Bind();//datagrid重新绑定
StudentDataGrid.DataBind();
this.StudentDataGrid.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
比较笨的方法使用StreamWrite试下:
首先用for循环,将datagrid的内容逐行输出到一个字符串中,
然后使用:
string path = Request.ServerVariables["APPL_PHYSICAL_PATH"];
path=path.Replace("\\","\\\\");
FileStream sb = new FileStream( path + "upload\\mypath.txt",FileMode.OpenOrCreate);
string s = "。";//获得的datagrid中的数据
StreamWriter ws = new StreamWriter(sb);
ws.Write(s);
ws.Close();需要using System.IO;
在项目文件夹下建立upload文件夹
将upload文件夹的安全属性加入aspnet的用户,并使该用户有修改权限