c#chart excel风格

时间:2017-04-04 07:47:14

标签: c# charts excel-interop

我无法在C#中更改excel图表的样式。

我有这个: default design

我希望得到这个: design I try to have

我阅读了很多关于这个主题的主题,尝试了很多这些主题,但似乎没有什么工作...... 我设法得到了我想要的饼图,但它不适用于我的“柱子”图表:

我试过了:

object paramChartFormat = 1; //tried to change it to 201, 209, ... not working :(

然后这个(在excel中找到带有宏的名称,因为我看到它是在stackoverflow上获取我们想要的另一个问题的设计名称的方法)但是设计仍然是蓝色列的“标准”:

Microsoft.Office.Interop.Excel.XlChartType.xlColumnStacked; 

这是我的代码:

        object paramChartFormat = 1;
        object paramCategoryLabels = 1;
        object paramSeriesLabels = 1;
        //bool paramHasLegend = true;
        object paramTitle = "Collection";
        object paramCategoryTitle = "";
        object paramValueTitle = "";
        Range dataRange = null;
        ChartObjects chartObjects = null;
        //ChartObject newChartObject = null;
        object paramMissing = Type.Missing;
        string tuple = "C" + Row.ToString();
        dataRange = sheet.get_Range("A1", tuple);
        chartObjects = (ChartObjects)(sheet.ChartObjects(paramMissing));
        /*newChartObject = chartObjects.Add(0, 100, 300, 300);
        newChartObject.Chart.ChartWizard(dataRange, XlChartType.xl3DColumn, paramChartFormat, XlRowCol.xlRows, paramCategoryLabels, paramSeriesLabels, paramHasLegend, paramTitle, paramCategoryTitle, paramValueTitle, paramMissing);
        */
        //Number of copies
        Microsoft.Office.Interop.Excel.ChartObject chartObjectnbex = sheet.ChartObjects().Add((float)sheet.get_Range("B1").Left, (float)sheet.get_Range("B"+(Row+2).ToString()).Top, 300, 300);
        chartObjectnbex.Chart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlColumnStacked;
        Microsoft.Office.Interop.Excel.Series seriesnbex = chartObjectnbex.Chart.SeriesCollection().Add(sheet.Range["B2:B" + Row.ToString()]);
        seriesnbex.XValues = sheet.Range["A2:A" + Row.ToString()];
        seriesnbex.Name = "Nombre d'Exemplaires";
        //Nbre documents différents

//此代码(饼图正在运行):

 Microsoft.Office.Interop.Excel.ChartObject myChart = (Microsoft.Office.Interop.Excel.ChartObject)chartObjects.Add((float)sheet.get_Range("B1").Left, (float)sheet.get_Range("B" + (Row + 77).ToString()).Top, 300, 300);
            Microsoft.Office.Interop.Excel.Chart chartPage = myChart.Chart;

            Microsoft.Office.Interop.Excel.SeriesCollection seriesCollection = chartPage.SeriesCollection();
            Microsoft.Office.Interop.Excel.Series series1 = seriesCollection.NewSeries();
            series1.XValues = sheet.Range["A2", "A"+Row.ToString()];
            series1.Values = sheet.Range["B2", "B" + Row.ToString()];
            chartPage.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlDoughnut;
            chartPage.HasTitle = true;
            chartPage.ChartTitle.Text = "Nombre d'exemplaires";
            Microsoft.Office.Interop.Excel.Axis axis = chartPage.Axes(Microsoft.Office.Interop.Excel.XlAxisType.xlValue, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary) as Microsoft.Office.Interop.Excel.Axis;

            series1.ApplyDataLabels(Microsoft.Office.Interop.Excel.XlDataLabelsType.xlDataLabelsShowPercent, true, true, false, false, false, false, true);

您是否知道我在改变设计方面做错了什么?

非常感谢,

R上。

1 个答案:

答案 0 :(得分:0)

我发现我做错了什么。我只有一个系列,所以它把我所有的数字作为一个整体来考虑。我写了这段代码并且vo,它正在运作:

             //Nbre documents différents
            Microsoft.Office.Interop.Excel.ChartObject chartObjectnbdd = sheet.ChartObjects().Add((float)sheet.get_Range("E1").Left, (float)sheet.get_Range("B" + (Row + 2).ToString()).Top, 300, 300);
            chartObjectnbex.Chart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlBarClustered;
            chartObjectnbdd.Chart.HasTitle = true;
            chartObjectnbdd.Chart.ChartTitle.Text = "Nombre de documents différents";
            Microsoft.Office.Interop.Excel.SeriesCollection seriescollnbdd = chartObjectnbdd.Chart.SeriesCollection();
            Microsoft.Office.Interop.Excel.Series seriesnbdd = null;
            i = 2;
            while (i <= Row)
            {
                seriesnbdd = seriescollnbdd.NewSeries();
                seriesnbdd.Name = excel.Cells[i, 1].Value.ToString();
                seriesnbdd.Values = sheet.Range["C" + i.ToString() + ":C" + i.ToString()];
                i++;
            }

输出(现在唯一的东西是“1”我无法想象如何擦除但是没关系):

Chart with colors

谢谢大家!