none
Insert Excel Data Bar Format in ASP.Net web page RRS feed

  • Question

  • I have a spreadsheet that utilizes data bars. Is this Object available to use in a ASP.net web page? I would be getting the data from an Azure Table. Thanks in advance for design guidance on this.

    Wednesday, December 13, 2017 2:03 AM

Answers

  • Hi DDL19,

    generally , it is not recommended to use Office automation on server side.

    so it is better to avoid Excel Object Model to display data bars in Asp.net Web page.

    you can try to refer the link below to know about the issues may occur.

    Considerations for server-side Automation of Office

    but there is a way to achieve the same requirement using Asp.Net.

    refer the example below.

    HTML code:

    <h3>Sales Report Month wise with Databars Like Excel using ASP.NET and C#.</h3>
        <div style="padding:20px;">
            <br />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="4">
                <Columns>
                    <asp:BoundField HeaderText="Month" DataField="MonthName" />
                    <asp:BoundField HeaderText="Sales Amount" DataField="SalesAmount" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <div class="container">
                                <div class="graph">
                                    <div class="databar" style='width:<%#Eval("Percentage")%>%;'>
                                        &nbsp;
                                    </div>
                                </div>
                                <div class="datavalue">
                                    <%#Eval("SalesAmount") %>
                                </div>
                            </div>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>

    Code Behind:

     protected void Page_Load(object sender, EventArgs e)
                {
                    if (!IsPostBack)
                    {
                        PopulateSaleData();
                    }
                }         
            
    private void PopulateSaleData()
                {
                    List<SalesData> allData = new List<SalesData>();
                    using (MyDatabaseEntities dc = new MyDatabaseEntities())
                    {
                        allData = dc.SalesDatas.ToList();
                    }
                    int max = Convert.ToInt32(allData.Max(a=>a.SalesAmount));
                    foreach (var a in allData)
                    {
                        decimal p = (100 * a.SalesAmount) / max;
                        a.Percentage = Convert.ToInt32(p);
                    }
     
                    GridView1.DataSource = allData;
                    GridView1.DataBind();
                }     

    Output:

    for full code and detailed steps, you can refer link below.

    Excel conditional formatting options using asp.net c#.

    Disclaimer: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 13, 2017 7:10 AM
    Moderator
  • That is exactly what I was looking for. Thanks!
    • Marked as answer by DDL19 Thursday, December 14, 2017 12:20 PM
    Thursday, December 14, 2017 12:20 PM

All replies

  • Hi DDL19,

    generally , it is not recommended to use Office automation on server side.

    so it is better to avoid Excel Object Model to display data bars in Asp.net Web page.

    you can try to refer the link below to know about the issues may occur.

    Considerations for server-side Automation of Office

    but there is a way to achieve the same requirement using Asp.Net.

    refer the example below.

    HTML code:

    <h3>Sales Report Month wise with Databars Like Excel using ASP.NET and C#.</h3>
        <div style="padding:20px;">
            <br />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="4">
                <Columns>
                    <asp:BoundField HeaderText="Month" DataField="MonthName" />
                    <asp:BoundField HeaderText="Sales Amount" DataField="SalesAmount" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <div class="container">
                                <div class="graph">
                                    <div class="databar" style='width:<%#Eval("Percentage")%>%;'>
                                        &nbsp;
                                    </div>
                                </div>
                                <div class="datavalue">
                                    <%#Eval("SalesAmount") %>
                                </div>
                            </div>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>

    Code Behind:

     protected void Page_Load(object sender, EventArgs e)
                {
                    if (!IsPostBack)
                    {
                        PopulateSaleData();
                    }
                }         
            
    private void PopulateSaleData()
                {
                    List<SalesData> allData = new List<SalesData>();
                    using (MyDatabaseEntities dc = new MyDatabaseEntities())
                    {
                        allData = dc.SalesDatas.ToList();
                    }
                    int max = Convert.ToInt32(allData.Max(a=>a.SalesAmount));
                    foreach (var a in allData)
                    {
                        decimal p = (100 * a.SalesAmount) / max;
                        a.Percentage = Convert.ToInt32(p);
                    }
     
                    GridView1.DataSource = allData;
                    GridView1.DataBind();
                }     

    Output:

    for full code and detailed steps, you can refer link below.

    Excel conditional formatting options using asp.net c#.

    Disclaimer: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 13, 2017 7:10 AM
    Moderator
  • That is exactly what I was looking for. Thanks!
    • Marked as answer by DDL19 Thursday, December 14, 2017 12:20 PM
    Thursday, December 14, 2017 12:20 PM