c# - 在 Excel 图表 c# 中旋转 X 轴

标签 c# excel charts

我正在尝试旋转 Excel 图表中的 X 轴文本。
这是我当前的代码:

    Excel.Application xlApp;
    Excel.Workbook xlWorkBook;
    Excel.Worksheet xlWorkSheet;
    object misValue = System.Reflection.Missing.Value;

    xlApp = new Excel.ApplicationClass();
    xlWorkBook = xlApp.Workbooks.Add(misValue);

    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
    int currentLine = 1;

    foreach (int currentKey in currentLogFile.Keys)
    {
          xlWorkSheet.Cells[currentLine, 1] = currentKey;
          xlWorkSheet.Cells[currentLine, 2] = currentLogFile[currentKey];
          currentLine++; 
    }

    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", "B10");
    chartPage.SetSourceData(chartRange, misValue);

    chartPage.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlLine;

    chartPage.ApplyLayout(6, Type.Missing);

    xlWorkBook.SaveAs(excelOutputFile, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, 
    Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

    xlWorkBook.Close(true, misValue, misValue);
    xlApp.Quit();

我看到的解决方案很少,例如:

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

Changing Axis Labels on Excel Chart created in C#

C# chart rotate labels

但是我在所有这些上都遇到了编译错误。

最佳答案

Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;

xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);

xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
int currentLine = 1;

foreach (int currentKey in currentLogFile.Keys)
{
      xlWorkSheet.Cells[currentLine, 1] = currentKey;
      xlWorkSheet.Cells[currentLine, 2] = currentLogFile[currentKey];
      currentLine++; 
}

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", "B10");
chartPage.SetSourceData(chartRange, misValue);

chartPage.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlLine;

//Rotate 35 degrees.
chartPage.Axes(Excel.XlAxisType.xlCategory).TickLabels.Orientation = 35;
chartPage.Axes(Excel.XlAxisType.xlValue).TickLabels.Orientation = 35;

chartPage.ApplyLayout(6, Type.Missing);

xlWorkBook.SaveAs(excelOutputFile, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, 
Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();

关于c# - 在 Excel 图表 c# 中旋转 X 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16275979/

相关文章:

excel - 向下拖动公式并更改列引用而不是行

r - 如何使用 Quantmod 在 ChartSeries 图上画一条线?

C# 折线图如何创建垂直线

c# - 正确的方法将数字分配给列表集合中的元素

vba - 如何知道工作表中最长行的长度

excel - 如果此公式中没有值,如何返回空白而不是 #N/A?

javascript - 使用 d3 的轴标签的多级/分组

c# - xUnit.net IsType 在返回类型的 MS 测试中等效

c# - WPF工具包图表: How to add value label to specific data point

c# - 控制台应用程序仅发送 POST 请求中的前 46 个字符