if(datagrid1.items.count==0)
{
response.write(“<script>alert(对不起,你没有查询到任何记录,不能导出数据)</script>”);
}
else
{
response.clear();
response.buffer= true;
response.charset=”gb2312″;
response.appendheader(“content-disposition”,”attachment;filename=filename.xls”);
response.contentencoding=system.text.encoding.getencoding(“gb2312”);
//设置输出流为简体中文
response.contenttype = “application/ms-excel”;
//设置输出文件类型为excel文件。
this.enableviewstate = false;
system.globalization.cultureinfo mycitrad = new system.globalization.cultureinfo(“zh-cn”,true);
system.io.stringwriter ostringwriter = new system.io.stringwriter(mycitrad);
system.web.ui.htmltextwriter ohtmltextwriter = new system.web.ui.htmltextwriter(ostringwriter);
datagrid1.rendercontrol(ohtmltextwriter);
response.write(ostringwriter.tostring());
response.end();
}