简体   繁体   中英

How to provide the custom angle to label in excel using C#

I want to provide the custome angle to my labels on the x-axis to like -35 degrees. ie i want to format the in Excel ==> Format Axis ==>Alignment ==>Custom angle

newWorksheet.Select(Type.Missing);
            Excel.Range chartRange;
            object misValue = System.Reflection.Missing.Value;
            Excel.ChartObjects xlCharts = (Excel.ChartObjects)newWorksheet.ChartObjects(Type.Missing);
            Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
            Excel.Chart chartPage = myChart.Chart;
            chartRange = newWorksheet.get_Range(cell1, cell2);
            chartPage.SetSourceData(chartRange, Excel.XlRowCol.xlColumns);
            chartPage.ChartType = Excel.XlChartType.xlColumnClustered;

            var labels = new List<string>();
            string date = string.Empty;
            foreach (KeyValuePair<string, List<TestDetails>> kvp in writetocsv)
            {
                date = kvp.Key.Substring(0, (kvp.Key.Length - kvp.Key.IndexOf('_')) + 1);
                labels.Add(date);
            }
            var series = (Excel.Series)chartPage.SeriesCollection(1);
            series.XValues = labels.ToArray();
            //series.HasDataLabels = true;
            Excel.Axis valueAxis = (Excel.Axis)chartPage.Axes(Excel.XlAxisType.xlValue, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary);


            chartPage.Location(Microsoft.Office.Interop.Excel.XlChartLocation.xlLocationAsNewSheet, "Chart_" + sheetName);

Try this ( TRIED AND TESTED )

Excel.Range chartRange;

Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
Excel.Chart chartPage = myChart.Chart;
chartRange = xlWorkSheet.get_Range("A1", "d5");
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = Excel.XlChartType.xlColumnClustered;

//~~> From here this is the part which you want
chartPage.ApplyLayout(6, Type.Missing);
chartPage.Axes(Excel.XlAxisType.xlValue).AxisTitle.Select();
chartPage.Axes(Excel.XlAxisType.xlValue).AxisTitle.Orientation = Excel.XlOrientation.xlHorizontal;

//~~> 35 Deg angle
for (int i = 1; i <= 35; i++)
{
    chartPage.Axes(Excel.XlAxisType.xlValue).AxisTitle.Orientation = -i;
}

SNAPSHOT

在此输入图像描述

FOLLOWUP

Hi Siddharth, Actullay i want to provide the Angle to "Term1", "Term2" – User123 10 mins ago

Try this for X Axis Tick Labels

Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 80, 300, 250);
Excel.Chart chartPage = myChart.Chart;
chartRange = xlWorkSheet.get_Range("A1", "d5");
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = Excel.XlChartType.xlColumnClustered;
chartPage.ApplyLayout(6, Type.Missing);

chartPage.Axes(Excel.XlAxisType.xlCategory).Select();
chartPage.Axes(Excel.XlAxisType.xlCategory).TickLabels.Orientation = 35;

SNAPSHOT

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM