目的:
1.根據模板里面的excel數據信息,動態創建line chart
2.linechart 的樣式改為灰色
3.以流的形式寫到客戶端,不管客戶端是否裝excel,都可以導出到到客戶端
4.使用Aspose.Cells的基本功能
5.使用mvc測試代碼
導出到excel里面的效果圖
excel里面的數據源sheet2
2001 2002 2003 2004 2005 2006 2007 中原地產 10 20 30 40 50 70 80 上海中原 30 80 44 55 88 90 120 河北中原 24 45 55 66 88 90 70 南京中原 44 55 66 77 88 99 90 背景中原 11 34 36 37 33 32 21 中原地產2 10 20 30 40 50 70 80 上海中原3 30 80 44 55 88 90 120 上海中原4 24 45 55 66 88 90 70 上海中原5 44 55 66 77 88 99 90 上海中原6 11 34 36 37 33 32 21 上海中原7 10 20 30 40 50 70 80 上海中原8 30 80 44 55 88 90 120 上海中原9 24 45 55 66 88 90 70 上海中原10 44 55 66 77 88 99 90 上海中原11 11 34 36 37 33 32 21 中原地產12 10 20 30 40 50 70 80 上海中原13 30 80 44 55 88 90 120 上海中原14 24 45 55 66 88 90 70 上海中原15 44 55 66 77 88 99 90 上海中原16 11 34 36 37 33 32 21 上海中原17 10 20 30 40 50 70 80 上海中原18 30 80 44 55 88 90 120 上海中原19 24 45 55 66 88 90 70 上海中原21 44 55 66 77 88 99 90 上海中原22 11 34 36 37 33 32 21
入口方法:
public ActionResult excels() { WorkbookDesigner designer = new WorkbookDesigner(); string path = Server.MapPath("/Templete/11111.xls"); designer.Workbook.Open(path); Workbook workbook = designer.Workbook; //創建一個chart到頁面 CreateStaticReport1(workbook); designer.Process(); //將流文件寫到客戶端流的形式寫到客戶端,名稱是_report.xls designer.Save("_report.xls", SaveType.OpenInExcel, FileFormatType.Excel2003, System.Web.HttpContext.Current.Response); Response.Flush(); Response.Close(); designer = null; // Response.End(); return View("getexcel"); }
生成chart方法
private void CreateStaticReport1(Workbook workbook) { //創建一個折線圖 workbook.Worksheets[0].Charts.Add(ChartType.Line, 1, 1, 25, 10); Aspose.Cells.Chart chart = workbook.Worksheets[0].Charts[0]; //折線區域豎線設置為顯示顏色設置為灰色 chart.CategoryAxis.MajorGridLines.IsVisible = true; chart.CategoryAxis.MajorGridLines.Color = Color.Gray; //折線區域設置橫着的網格線顯示 chart.MajorGridLines.IsVisible = true; chart.MajorGridLines.Color = Color.Gray; //設置title樣式 chart.Title.Text = "Sales By Region For Years"; chart.Title.TextFont.Color = Color.Gray; chart.Title.TextFont.IsBold = true; chart.Title.TextFont.Size = 12; //Set Properties of nseries chart.NSeries.Add("Sheet2!B2:H26", false); //Set NSeries Category Datasource chart.NSeries.CategoryData = "Sheet2!B1:H1"; Cells cells = workbook.Worksheets[1].Cells; //loop over the Nseriese for (int i = 0; i < chart.NSeries.Count; i++) { //設置每條折線的名稱 chart.NSeries[i].Name = cells[i + 1, 0].Value.ToString(); //設置線的寬度 chart.NSeries[i].Line.Weight = WeightType.MediumLine; //設置每個值坐標點的樣式 chart.NSeries[i].MarkerStyle = ChartMarkerType.Circle; chart.NSeries[i].MarkerSize = 5; chart.NSeries[i].MarkerBackgroundColor = Color.White; chart.NSeries[i].MarkerForegroundColor = Color.Gray; //每個折線向顯示出值 chart.NSeries[i].DataLabels.IsValueShown = true; chart.NSeries[i].DataLabels.TextFont.Color = Color.Gray; } //設置x軸上數據的樣式為灰色 chart.CategoryAxis.TickLabels.Font.Color = Color.Gray; chart.CategoryAxis.TickLabelPosition = TickLabelPositionType.NextToAxis; //設置y軸的樣式 chart.ValueAxis.TickLabelPosition = TickLabelPositionType.Low; chart.ValueAxis.TickLabels.Font.Color = Color.Gray; // chart.ValueAxis.TickLabels.TextDirection = TextDirectionType.LeftToRight; //設置Legend位置以及樣式 chart.Legend.Position = LegendPositionType.Bottom; chart.Legend.TextFont.Color = Color.Gray; chart.Legend.Border.Color = Color.Gray; }