0
我想导出数据集以使用以下代码从asp.net页面方法(webmethod)导出excel。导出数据集以使用asp.net从webmethod导出excel的问题
[WebMethod]
public static void ExporttoExcel()
{
DataSet ds;
productfactory pf=new productfactory();
ds = pf.getproducts();
HttpResponse response = HttpContext.Current.Response;
// first let's clean up the response.object
response.Clear();
response.Charset = "";
response.ContentEncoding = System.Text.Encoding.Default;
// set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"products.xls\"");
// create a string writer
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// instantiate a datagrid
DataGrid dg = new DataGrid();
dg.DataSource = ds.Tables[0];
dg.DataBind();
dg.RenderControl(htw);
string filepath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\products.xls";
response.Write(sw.ToString());
// response.End();
}
}
}
上面的代码似乎是工作以外的事情,下载文件对话框没有打开询问用户“保存” /“打开”对话框,因此我没有看到响应是否被写入OK到一个文件。相反,当我在Firebug中检查响应时,我得到了HTML表格结构中的数据。
请问有人可以帮我吗?
谢谢。