在Excel图表c#中旋转X轴

本文关键字:旋转 Excel 图表 | 更新日期: 2023-09-27 18:11:14

我想旋转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();

我看到了一些解决方案,比如:

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

在c#中创建的Excel图表中更改坐标轴标签

c# chart rotate label

然而,我得到编译错误的所有。

在Excel图表c#中旋转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;
//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();