gpt4 book ai didi

c# - 如何使用 C# 在 Excel 中为标签提供自定义角度

转载 作者:行者123 更新时间:2023-11-30 15:03:05 25 4
gpt4 key购买 nike

我想在 x 轴上为我的标签提供自定义角度,例如 -35 度。即我想在 Excel 中格式化 ==> 格式化轴 ==> 对齐 ==> 自定义角度

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);

最佳答案

代码

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;
}

快照

enter image description here

跟进

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

尝试使用 X 轴刻度标签

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;

快照

enter image description here

关于c# - 如何使用 C# 在 Excel 中为标签提供自定义角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11991566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com