无法从gridview导出excel

时间:2013-11-07 18:42:18

标签: c# asp.net

这是我的代码:

          protected void btnExcel_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            DataTable dtbl = (DataTable)ViewState["dtbl"];
            string path = "E:\\shubby" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + ".xls";
            FileInfo fi = new FileInfo(path);
            StringWriter sw = new StringWriter();
            HtmlTextWriter htmltw = new HtmlTextWriter(sw);
            DataGrid grdCrew = new DataGrid();
            grdCrew.DataSource = dtbl;
            grdCrew.DataBind();

            grdCrew.RenderControl(htmltw);
            string directory = path.Substring(0, path.LastIndexOf("\\"));// GetDirectory(Path);
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            System.IO.StreamWriter vw = new System.IO.StreamWriter(path, true);
            sw.ToString().Normalize();
            vw.Write(sw.ToString());
            vw.Flush();
            vw.Close();
            WriteAttachment(fi.Name, "application/vnd.ms-excel", sw.ToString());
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

    public static void WriteAttachment(string FileName, string FileType, string content)
    {
        HttpResponse Response = System.Web.HttpContext.Current.Response;
        Response.ClearHeaders();
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
        Response.ContentType = FileType;
        Response.Write(content);
        Response.End();
    }

我有业务逻辑和存储过程的单独文件,一切正常,它只是我遇到麻烦的excel导出,

我得到的错误是说"客户不持有所需的特权" ...

请帮助

0 个答案:

没有答案