开发者

Showing labels on Excel Charts using C#

i have this code working fine. but i want to see the numerical values on the top of each column in the graph. how can i achieve this? thank you each and everyone for your time.

    rowNo += 1;
    string currDir = Server.MapPath(".") + "\\Dashboard\\";
    ExcelApp = new Microsoft.Office.Interop.Excel.Application();

    SqlParameter[] dParam = new SqlParameter[0];
    System.Data.SqlClient.SqlConnection con = new SqlConnection(strDBConn);
    System.Data.SqlClient.SqlCommand cmd = new SqlCommand();
    if (con.State == ConnectionState.Closed)
        con.Open();

    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "Excel_QuoteCount";
    cmd.Connection = con;
    SqlDataReader reader = cmd.ExecuteReader();
    oWBook = ExcelApp.Workbooks.Add(Type.Missing);
    oSheet = (Worksheet)oWBook.Worksheets.get_Item(1);

    while (reader.Read())
    {
        oSheet.Cells[rowNo, 1] = reader["Date"].ToString();
        oSheet.Cells[rowNo, 2] = reader["QuoteCount"].ToString();
        oSheet.Cells[rowNo, 3] = reader["Date"].ToString();
        oSheet.Cells[rowNo, 4] = reader["QuoteTotal"].ToString();
         rowNo++;
    }
    oSheet.Columns.AutoFit();

    reader.Close();
    Range chartRange;
    Range TotalsRange;
    ChartObjects xlCharts = (ChartObjects)oSheet.ChartObjects(Type.Missing);
    ChartObject myChart = (ChartObject)xlCharts.Add(10, 80, 300, 250);
    Chart chartPage = myChart.Chart;

    //Quotes Count
    chartPage.Legend.Clear();
    chartRange = oSheet.get_Range("A13", "B20");

    chartPage.SetSourceData(chartRange, Type.Missing);
    chartPage.ChartType = XlChartType.xlColumnClustered;
    chartPage.Export(@currDir + "QuoteCount.jpg", "JPEG", Type.Missing);

    //Quotes Count
    chartRange = oSheet.get_Range("C13", "D20");
    chartRange.Font.Bold = true;
    TotalsRange = oSheet.get_Range("D13", "D20");
    TotalsRange.NumberFormat = "$0,000.00";

    chartPage.SetSourceData(chartRange, Type.Missing);
    chartPage.ChartType = XlChartType.xlColumnClustered;// xlColumnClustered;
    chartPage.Export(@currDir + "QuotesTotal.jpg", "JPEG", Type.Missing);

    FileInfo theFile = new FileInfo(currDir + "Quotes.xls");
    if (theFile.Exists)
    {
        theFile.Delete();
    }


    oWBook.SaveAs(currDir + "Quotes.xls", XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    oWBook.Close(true, Type.Missing,开发者_StackOverflow社区 Type.Missing);
    ExcelApp.Quit();


    releaseObject(oSheet);
    releaseObject(oWBook);
    releaseObject(ExcelApp);


you can use chartPage.ApplyDataLabels() to show the numerical values on the top of each column in the graph. It is working.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜