2

我正在尝试在 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();

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

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

更改用 C# 创建的 Excel 图表上的轴标签

C# 图表旋转标签

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

4

1 回答 1

0
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();
于 2017-08-18T01:13:45.227 回答