Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi

i try to draw piechart of using excel data using coding in c# i use Microsoft.Office.Interop.Excel
i get chart if i try for column A and B but if if i try For A and Column C it not take column c so my problem is that how we pass range for pie chart which take nay column from excel sheet,

I use below code.
C#
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
        object missing = Type.Missing;
        Microsoft.Office.Interop.Excel.Range xlRange;
        Microsoft.Office.Interop.Excel.Sheets xlSheets = null;
        Microsoft.Office.Interop.Excel.Worksheet xlSheet = null;
        Microsoft.Office.Interop.Excel.Workbook xlWorkbook = null;


        xlWorkbook = excelApp.Workbooks.Open(@"C:\delfolder\data12.xlsx", missing, missing, missing,
                                        missing, missing, missing, missing, missing, missing, missing,
                                        missing, missing, missing, missing);


        xlSheets = (Microsoft.Office.Interop.Excel.Sheets)xlWorkbook.Sheets;
        xlSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlSheets[1];


      


        // Now create the chart.
        Microsoft.Office.Interop.Excel.ChartObjects chartObjs = (Microsoft.Office.Interop.Excel.ChartObjects)xlSheet.ChartObjects(Type.Missing);
        Microsoft.Office.Interop.Excel.ChartObject chartObj = chartObjs.Add(1000,20,500,500);
        Microsoft.Office.Interop.Excel.Chart xlChart = chartObj.Chart;

        Microsoft.Office.Interop.Excel.Range rg = xlSheet.get_Range("A2:A13", "B2:B13");
       
       

       
    
        xlChart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlPie;
        xlChart.SetSourceData(rg3, Type.Missing);
     
        xlChart.ApplyDataLabels(Microsoft.Office.Interop.Excel.XlDataLabelsType.xlDataLabelsShowLabelAndPercent,Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing);

        xlWorkbook.SaveAs(@"C:\delfolder\data12.xlsx", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        xlWorkbook.Close(true, Type.Missing, Type.Missing);

        excelApp.Quit();
        releaseObject(xlSheet);
        releaseObject(xlWorkbook);
        releaseObject(excelApp);

so Microsoft.Office.Interop.Excel.Range rg = xlSheet.get_Range("A2:A13", "B2:B13");
in place of range "B2:B13" i want to take column "C2:C13" but my problem is that in i pass "C2:C13"
it give me a chart for "B2:B13"g which is help full for me please help me


thank you.

[Edit]Code block formatting added, Excel tag added[/Edit]
Posted
Updated 25-Jul-13 0:51am
v2
Comments
Sergey Alexandrovich Kryukov 25-Jul-13 10:52am    
Why using Excel at all? Why not, say, Microsoft Charts?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900