StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
this.Grid_SalesValue.RenderControl(htw); //Grid_SalesValue为GridView控件
string strHtml = sw.ToString().Trim();
strHtml = strHtml.Substring(1, strHtml.Length - 1);
string ExcelFileName = this.userinfo.Uid.Trim() + "SalV.xls";
string FilePhysicialPathName = Request.PhysicalApplicationPath + "//DownloadExcel//";
//生成的Excel文件名
string objectExcelFileName = Path.Combine(FilePhysicialPathName, ExcelFileName);
if (File.Exists(objectExcelFileName))
{
File.Delete(objectExcelFileName);
}
FileStream fs = new FileStream(objectExcelFileName, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs, Encoding.GetEncoding("GB2312"));
bw.Write(strHtml);
bw.Close();
fs.Close();
sw.Dispose();
htw.Dispose();
//将保存好的excel文件进行锁定
// LockedExcel();
//下载表格
Response.Write("<script> window.open('http://cgqh306a/Rolling_Forecast/downloadexcel/" + this.userinfo.Uid.Trim() + "SalV.xls" + "')</script>");
本文介绍如何使用ASP.NET中的GridView控件将数据显示为HTML并进一步导出为Excel文件的过程。具体步骤包括:利用StringWriter和HtmlTextWriter将GridView渲染为HTML字符串,然后通过BinaryWriter写入文件系统生成Excel文件,并通过Response对象触发文件下载。

744

被折叠的 条评论
为什么被折叠?



