新手求教:.net中如何把数据用EXCEL形式保存在指定IP的指定文件夹下
发布网友
发布时间:2022-11-19 10:27
我来回答
共1个回答
热心网友
时间:2024-05-05 05:40
/// <summary>
/// 将GridView中的数据内容导出成EXCEL文件
/// </summary>
/// <param name="gv">GridView</param>
public void ExportToExcel(GridView gv)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
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);
gv.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
}
然后调用这个方法,Gridview做参数