0

这是我的代码:

          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 导出,

我得到的错误是“客户不持有所需的特权”的异常......

请帮忙

4

0 回答 0