2014-04-01 154 views
1

我可以生成excel文件,當我通過datatable進入下面的功能。如何使用c#interop從Excel工作表生成圖表?

public static void ExportDataTableToExcel(DataTable dt, string filepath) 
{ 

    object missing = Type.Missing; 

    object misValue = System.Reflection.Missing.Value; 

    //create excel 
    Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); 

    //add excel workbook 
    Microsoft.Office.Interop.Excel.Workbook wb = excel.Workbooks.Add(); 

    //add worksheet to workbook 
    Microsoft.Office.Interop.Excel.Worksheet ws = wb.Sheets[1] as Microsoft.Office.Interop.Excel.Worksheet; 

    //add 2nd worksheet to workbook 
    Microsoft.Office.Interop.Excel.Worksheet ws2 = wb.Sheets[2] as Microsoft.Office.Interop.Excel.Worksheet; 

    //Set the header-row bold 
    ws.Range["A1", "A1"].EntireRow.Font.Bold = true; 

    //Adjust all columns 
    ws.Columns.AutoFit(); 

    //spit top row 
    ws.Application.ActiveWindow.SplitRow = 1; 

    //insert image into worsheet 2 
    ws2.Shapes.AddPicture("C:\\Koala.JPG", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 50, 50, 300, 300); 

    //freeze top row 
    ws.Application.ActiveWindow.FreezePanes = true; 


    int rowCount = 1; 

    foreach (DataRow dr in dt.Rows) 
    { 

     rowCount += 1; 

     for (int i = 1; i < dt.Columns.Count + 1; i++) 
     { 

      // Add the header the first time through 

      if (rowCount == 2) 
      { 

       ws.Cells[1, i] = dt.Columns[i - 1].ColumnName; 
       ws.Cells[1, i].Interior.ColorIndex = 40; 

       // add cell border 
       ws.Cells[1, i].Borders.LineStyle = Excel.XlLineStyle.xlContinuous; 

      } 

      ws.Cells[rowCount, i] = dr[i - 1].ToString(); 

      // add cell border 
      ws.Cells[rowCount, i].Borders.LineStyle = Excel.XlLineStyle.xlContinuous; 

     } 

    } 

    Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)ws.UsedRange; 

    Console.Write(range.ToString()); 


    wb.SaveAs(@"C:\Test.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, 
             misValue, misValue, misValue, 
             Excel.XlSaveAsAccessMode.xlExclusive, misValue, 
             misValue, misValue, misValue, misValue); 

    wb.Close(missing, missing, missing); 

    excel.Quit(); 
} 

該函數運行良好。我需要將C#代碼中的圖形添加到此Excel文件中。我嘗試了幾種方法,但沒有找到正確的方法來實施。你可以幫我嗎?

回答

6

查看教程here(首批Google點擊之一)。

它非常清楚地描述瞭如何使用C#代碼在Excel中製作簡單的圖表。

的總體思路是這樣的:

// Add chart. 
var charts = worksheet.ChartObjects() as 
    Microsoft.Office.Interop.Excel.ChartObjects; 
var chartObject = charts.Add(60, 10, 300, 300) as 
    Microsoft.Office.Interop.Excel.ChartObject; 
var chart = chartObject.Chart; 

// Set chart range. 
var range = worksheet.get_Range(topLeft, bottomRight); 
chart.SetSourceData(range); 

// Set chart properties. 
chart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlLine; 
chart.ChartWizard(Source: range, 
    Title: graphTitle, 
    CategoryTitle: xAxis, 
    ValueTitle: yAxis); 
+0

由於它運作良好。我對c#和interop有點新鮮。 – user3467827

+0

@ user3467827:很高興提供幫助。 – Baldrick

+0

@Baldrick - 欣賞添加的語法,現在是一個無效的鏈接 –

相關問題