导出gridview到excel中的关键之处
用法: toexcel(gvstaff, textbox1.text);
public static void toexcel(system.web.ui.control ctl,string filename)
{
httpcontext.current.response.charset =”utf-8″;
httpcontext.current.response.contentencoding =system.text.encoding.default;
httpcontext.current.response.contenttype =”application/ms-excel”;
httpcontext.current.response.appendheader(“content-disposition”,”attachment;filename=”+””+filename+”.xls”);
ctl.page.enableviewstate =false;
system.io.stringwriter tw = new system.io.stringwriter();
htmltextwriter hw = new htmltextwriter(tw);
ctl.rendercontrol(hw);
httpcontext.current.response.write(tw.tostring());
httpcontext.current.response.end();
}
必须有下面这句!否则不会通过!
public override void verifyrenderinginserverform(control control)
{
// confirms that an htmlform control is rendered for
}