编译代码以将输出保存为Excel格式时,gvFile错误 [英] gvFile error while compling code to save output in Excel format
问题描述
我正在使用以下代码将datagrid的输出写入excel文件.
我收到错误消息
名称gvFiles不能在上下文中使用"
System.IO.StringWriter sw =新的System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw =新的System.Web.UI.HtmlTextWriter(sw);
//渲染网格视图控件.
gvFiles.RenderControl(htw);
//将渲染的内容写入文件.
字符串renderGridView = sw.ToString();
File.WriteAllText(@"C:\ temp \ ExportedFile.xlsx",renderingGridView);
我可以在datagrid中看到输出吗?
I am using the following code to write output of datagrid into an excel file.
I am getting error message
"The name gvFiles cannot be use din the context"
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
// Render grid view control.
gvFiles.RenderControl(htw);
// Write the rendered content to a file.
string renderedGridView = sw.ToString();
File.WriteAllText(@"C:\temp\ExportedFile.xlsx", renderedGridView);
I can see the output in the datagrid?
How do I save to Excel file in C#?
推荐答案
使用以下代码.它对我有用.
Use the following code.its working for me..
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=REPORTNAME.xls");
Response.Charset = "iso-8859-2";
Response.ContentType = "application/ms-excel";
using (StringWriter StringWriter = new System.IO.StringWriter())
{
using (HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter))
{
GridViewName.AllowPaging = false;
GridViewName.Attributes.AddAttributes(HtmlTextWriter);
GetUserTaskDetails(); //Get data for Grid view
GridViewName.RenderControl(HtmlTextWriter);
}
Response.Write(str);
Response.Write(StringWriter.ToString());
}
Response.End();
GridViewName.AllowPaging = true;
快乐的编码:-) JMD
Happy coding :-)JMD
这篇关于编译代码以将输出保存为Excel格式时,gvFile错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!