locked
how to convert asp:chart in excel and word without using rdlc report RRS feed

  • Question

  • User1046245955 posted

    anyone got idea on how to convert an asp:chart to excel and word not using a rdlc report.

    here's the sample chart code:

    <asp:Chart ID="Chart12" runat="server" DataSourceID="SqlDataSource3" Width="513px" Height="264px">
    <Titles>
    <asp:Title Font="Arial, 10pt" Name="Title1"
    Text="TITLE CHART" Alignment="TopLeft">
    </asp:Title>
    </Titles>
    <Series>
    <asp:Series ChartType="Bubble" Legend="Legend1" Name="Series1" XValueMember="LEGEND" YValueMembers="Q7" IsValueShownAsLabel="True" LabelFormat="{0}%">
    </asp:Series>
    </Series>
    <ChartAreas>
    <asp:ChartArea
    Name="DefaultChartArea"
    BorderDashStyle="Dot"
    BorderWidth="2"
    BorderColor="LightGray"
    BackColor="White">
    </asp:ChartArea>
    </ChartAreas></asp:Chart>

    Thursday, June 7, 2018 11:34 AM

All replies

  • Thursday, June 7, 2018 12:08 PM
  • User-330142929 posted

    Hi Paminchever,

    According to your description, you don’t want to use rdlc report library to achieve what you want. Here is a workaround, firstly we could save chart image via chartcontrol.saveimage method, secondly write image to excel file, I have made a demo below:

    Aspx.

            <div>
                <asp:Chart ID="Chart1" runat="server" DataSourceID="SqlDataSource1">
                    <Series>
                        <asp:Series Name="Series1" XValueMember="Name" YValueMembers="Price"></asp:Series>
                    </Series>
                    <ChartAreas>
                        <asp:ChartArea Name="ChartArea1"></asp:ChartArea>
                    </ChartAreas>
                </asp:Chart>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [Products]"></asp:SqlDataSource>
            
            <asp:Button ID="Button1" runat="server" Text="Export" OnClick="Button1_Click" />
                </div>
    

    Code-Behind.

    protected void Button1_Click(object sender, EventArgs e)
            {
                string imgPath2 = "D://1.png";
                this.Chart1.SaveImage(imgPath2,System.Web.UI.DataVisualization.Charting.ChartImageFormat.Png);
                
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("test.xls"));
                Response.ContentType = "application/vnd.ms-excel";
    
    
                StringWriter stringWrite = new StringWriter();
                HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
    
                string headerTable = @"<Table><tr><td><img src='" + imgPath2 + @"' /></td></tr></Table>";
                Response.Write(headerTable);
                Response.Write(stringWrite.ToString());
                Response.End
    

    How it works.

    if you have any question, please feel free to let me know.

     

    Best Regards, 

    Abraham

    Monday, June 11, 2018 2:15 AM