- 论坛徽章:
- 0
|
有一种方法可以回避这个问题:我们可以实现在点击“打开”按钮时不单独打开本地Excel,而是在线打开,即直接在IE里用Excel打开,但这样做比较慢。代码如下:
public static void ExportExcel(System.Web.UI.Page page,DataView dv,string[] headers,string[] colnames)
{
HttpResponse resp;
resp = page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
resp.AppendHeader("Content-Disposition", "online;filename=report.xls");//*****
resp.ContentType = "application/ms-excel";
string colHeaders= "", ls_item="";
int i = 0;
foreach(string header in headers)
{
colHeaders += header + " ";
}
colHeaders += "";
resp.Write(colHeaders);
i = 0;
foreach(DataRowView row in dv)
{
ls_item += Convert.ToString(i + 1) + " ";
foreach(string colname in colnames)
ls_item += row[colname].ToString() + " ";
ls_item += "";
resp.Write(ls_item);
ls_item="";
i++;
}
resp.End();
}
关键的地方在于参数“online”,若为“attachment”,则会出现打开两次的问题。
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/3787/showart_65511.html |
|