2013-04-29 148 views
2

我想旋轉Excel圖表中的X軸文本。
這裏是我當前的代碼:在Excel圖表中旋轉X軸c#

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

但是我得到的所有的人的編譯錯誤。

回答

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