需安裝 Microsoft Office Web Components 11.0 (OWC11),專案加入參考 Microsoft.Office.Interop.Owc11,並 using Microsoft.Office.Interop.Owc11;。
// 準備資料
int[] Month = { 1, 2, 3 };
double[] Count = { 120, 240, 220 };
// 建立圖表空間與圖表物件
ChartSpace chartSpace = new ChartSpace();
ChChart chart = chartSpace.Charts.Add(0);
chart.Type = ChartChartTypeEnum.chChartTypeColumnClustered;
// 圖表屬性設定
chart.HasLegend = true;
chart.HasTitle = true;
chart.Title.Caption = "第一季總結";
chart.Axes[0].HasTitle = true; chart.Axes[0].Title.Caption = "月份";
chart.Axes[1].HasTitle = true; chart.Axes[1].Title.Caption = "銷量";
// 加入三個系列
for (int i = 0; i < 3; i++) {
chart.SeriesCollection.Add(0);
chart.SeriesCollection[i].Caption = Month[i] + "月份";
chart.SeriesCollection[i].SetData(ChartDimensionsEnum.chDimCategories,
(int)ChartSpecialDataSourcesEnum.chDataLiteral, Month[i]);
chart.SeriesCollection[i].SetData(ChartDimensionsEnum.chDimValues,
(int)ChartSpecialDataSourcesEnum.chDataLiteral, Count[i]);
}
// 匯出圖片並顯示
string path = Server.MapPath(".") + @"\test.jpg";
chartSpace.ExportPicture(path, "jpg", 500, 450);
Image1.ImageUrl = path;