欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net GridView导出到Excel代码

程序员文章站 2024-03-09 09:17:23
复制代码 代码如下:stringbuilder sb = new stringbuilder(); stringwriter sw = new stringwriter(s...
复制代码 代码如下:

stringbuilder sb = new stringbuilder();
stringwriter sw = new stringwriter(sb);
htmltextwriter htw = new htmltextwriter(sw);
page page = new page();
htmlform form = new htmlform();
gvlinkman.enableviewstate = false;
page.enableeventvalidation = false;
page.designerinitialize();
page.controls.add(form);
form.controls.add(gvlinkman);
page.rendercontrol(htw);
response.clear();
response.buffer = true;
response.contenttype = "application/vnd.ms-excel";
response.addheader("content-disposition", "attachment;filename=data.xls");
response.charset = "utf-8";
response.contentencoding = encoding.default;
response.write(sb.tostring());
response.end();