怎样把GridView导出到Excel?

DreamingEric 2006-03-31 04:21:24
Response.Clear();
Response.AddHeader("content-disposition",
"attachment;filename=FileName.xls");
Response.Charset = "gb2312";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
代码应该没错了吧,但是执行后居然提示:“类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内。 ”

晕啊……在XX.aspx里明明是有form1 runnat="server"啊。
...全文
127 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
DreamingEric 2006-04-01
  • 打赏
  • 举报
回复
顶顶
DreamingEric 2006-03-31
  • 打赏
  • 举报
回复
楼上的是直接用http流输出吧,这样虽然快,但是无法自定义格式啊
xieyun9958 2006-03-31
  • 打赏
  • 举报
回复
private void Button3_Click(object sender, System.EventArgs e)
{
getdata();
}

public DataTable getdata()
{
sql = "select * from atemp where id>='"+ this.TextBox2.Text.Trim().ToString() +"' and id<='"+ this.TextBox3.Text.Trim().ToString() +"'";
da = new SqlDataAdapter(sql,myConnection);
DataTable dt = new DataTable("Sheet1");
da.Fill(dt);
this.dgExport.DataSource=dt;
this.dgExport.DataBind();
return dt;
}


private void DataExport()
{
string urlPath = HttpContext.Current.Request.ApplicationPath + "/Temp/";
string path = HttpContext.Current.Server.MapPath(urlPath);
string fileName = Guid.NewGuid() + ".Xls";
string excelPath = path + fileName;
DataTable dt = getdata();
DataToExcel.DataConversion dc = new DataToExcel.DataConversion();
dc.DataTableToExcel(dt,excelPath);
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.WriteFile(path + fileName);
string httpHeader="attachment;filename=data.xls";
response.AppendHeader("Content-Disposition", httpHeader);
HttpContext.Current.Response.ContentType ="application/vnd.ms-excel";
response.Flush();
System.IO.File.Delete(path + fileName);//删除临时文件
response.End();
}
DreamingEric 2006-03-31
  • 打赏
  • 举报
回复
GridView要比DataGrid好用得多吧。

问题所在就是这行 GridView1.RenderControl(htmlWrite);
humin1906 2006-03-31
  • 打赏
  • 举报
回复
function onDocumentLoad()
{
ExportToExcel();
}

function ExportToExcel()
{
try
{
var app = createOfficeObject("Excel.Application");
var workbook = app.workbooks.Add();
try
{
workbook.ActiveSheet.PageSetup.Orientation = 2; //xlLandscape
}
catch(e)
{
}

copyTableToClipboard();
workbook.activeSheet.paste();
var objRange=workbook.activeSheet.UsedRange;
objRange.EntireColumn.Autofit();
exit();
}
catch(e)
{
showError(e);
}
}

//建立office对象
function createOfficeObject(strProgID, bVisible)
{
var app = createObject(strProgID, "Microsoft Office");

if (bVisible == null || typeof(bVisible) == "undefined")
bVisible = true;

app.visible = bVisible;

return app;
}

function copyTableToClipboard()
{
var Dtb = document.all.grdUserReg;
if (Dtb) //表格ID
{
var selection = document.selection;
selection.empty();

var r = document.body.createControlRange();
r.add(Dtb);
r.select();

r.execCommand("Copy");
selection.empty();
}
}
</script>
</HEAD>
<body onload="ExportToExcel();">
<form id="Form1" method="post" runat="server">
<cc1:enhancedatagrid id="grdUserReg" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="UserOrgFullPath" HeaderText="所在部门"></asp:BoundColumn>
<asp:BoundColumn DataField="UserName" HeaderText="测评对象"></asp:BoundColumn>
<asp:BoundColumn DataField="TestTitle" HeaderText="评价表"></asp:BoundColumn>
<asp:BoundColumn DataField="TestFlag" HeaderText="考核类别">
<HeaderStyle Width="90px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="TestDate" HeaderText="考核日期" DataFormatString="{0:d}">
<HeaderStyle Width="90px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="TestStat" HeaderText="状态">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="AllScope" HeaderText="总分"></asp:BoundColumn>
<asp:BoundColumn DataField="OurNum" HeaderText="自评"></asp:BoundColumn>
<asp:BoundColumn DataField="EditNum" HeaderText="修正值"></asp:BoundColumn>
</Columns>
</cc1:enhancedatagrid></form>
</body>
</HTML>


这是DataGrid 倒EXCEL 的脚本,没试过gridview ,应该可以,
humin1906 2006-03-31
  • 打赏
  • 举报
回复
DataGrid 倒EXCEL 不能用吗?
DreamingEric 2006-03-31
  • 打赏
  • 举报
回复
在线等,急用啊,麻烦大家了

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧