Member 13755629 Ответов: 0

Невозможно экспортировать gridview и элемент управления диаграммой на отдельных листах excel в одну книгу


Всем привет

У меня есть gridview и 3 диаграммы на моей странице asp, которые я хочу экспортировать в excel и могу это сделать. Однако я не могу экспортировать gridview на листе 1 и диаграммы на листе 2 в excel.

Пожалуйста, помогите мне...

Ниже приведен мой код:

Что я уже пробовал:

protected void ExportToExcel(DataTable dt)
    {
        if (dt.Rows.Count > 0)
        {    
            string imgName1 = Session["tmpChartName1"].ToString();
            string imgName2 = Session["tmpChartName2"].ToString();
            string imgName3 = Session["tmpChartName3"].ToString();
            string imgPath1 = Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/" + imgName1);
            string imgPath2 = Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/" + imgName2);
            string imgPath3 = Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/" + imgName3);

            Response.Clear();
            Response.Buffer = true;
            Response.ClearContent();
            Response.ClearHeaders();
            Response.Charset = "";
            string filename="";

            filename = "GridData_" + DateTime.Now.ToString("yyyy-MM-dd") + ".xlsx"
            
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);

            GridView gv = new GridView();
            gv.DataSource = dt;
            gv.DataBind();

            gv.GridLines = GridLines.Both;
            gv.HeaderStyle.Font.Bold = true;
            gv.RenderControl(hw);
            string headerTable1 = @"";
            string headerTable2 = @"";
            string headerTable3 = @"";
            Response.Write(headerTable1);
            Response.Write(headerTable2);
            Response.Write(headerTable3);
            Response.Write(sw.ToString());
            Response.End();           
        }
        else
        {
            ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "ClientScript", "alert('Data not available!')", true);
        }
    }

0 Ответов