Epplus c# to excel 的 入門學習(三) 匯出excel資料 以及圖表
阿新 • • 發佈:2021-08-30
Epplus 匯出圖表的 三個步驟
建立圖表、選擇資料、設定圖表樣式
3.1 建立圖表
3.2 選擇資料。 這是很關鍵的一步,設定Y軸資料區、X軸資料區
3.3 設定圖表樣式
匯出結果:
參考文獻:
匯出Excel之Epplus使用教程3(圖表設定) - Wico - 部落格園 (cnblogs.com)
匯出Excel之Epplus使用教程3(圖表設定)
Epplus的圖表實現是很簡單的,它支援的圖表型別也很多,基本上能滿足我們的需求。建立圖表分為三步(以柱狀圖舉例):
1、建立圖表
1 |
ExcelChart chart = worksheet.Drawings.AddChart( "chart" , eChartType.ColumnClustered); //eChartType中可以選擇圖表型別
|
2、選擇資料
這一步是很關鍵的一步,chart.Series.Add()方法所需引數為:chart.Series.Add(Y軸資料區,X軸資料區)
1 2 |
ExcelChartSerie serie = chart.Series.Add(worksheet.Cells[2, 3, 5, 3], worksheet.Cells[2, 1, 5, 1]); //設定圖表的x軸和y軸
serie.HeaderAddress = worksheet.Cells[1, 3]; //設定圖表的圖例
|
3、設定圖表樣式
1 2 3 4 5 6 7 8 9 |
chart.SetPosition(150, 10); //設定位置
chart.SetSize(500, 300); //設定大小
chart.Title.Text = "銷量走勢" ; //設定圖表的標題
chart.Title.Font.Color = Color.FromArgb(89, 89, 89); //設定標題的顏色
chart.Title.Font.Size = 15; //標題的大小
chart.Title.Font.Bold = true ; //標題的粗體
chart.Style = eChartStyle.Style15; //設定圖表的樣式
chart.Legend.Border.LineStyle = eLineStyle.Solid;
chart.Legend.Border.Fill.Color = Color.FromArgb(217, 217, 217); //設定圖例的樣式
|
基本上生成圖表就這麼些東西了,不過不同的圖表屬性可能略有差異,得根據具體圖表具體分析。
下面是例子的全部程式碼:
FileInfo newFile = new FileInfo(@"d:\test.xlsx"); if (newFile.Exists) { newFile.Delete(); newFile = new FileInfo(@"d:\test.xlsx"); } using (ExcelPackage package = new ExcelPackage(newFile)) { ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("test"); worksheet.Cells.Style.WrapText = true; worksheet.View.ShowGridLines = false;//去掉sheet的網格線 worksheet.Cells[1, 1].Value = "名稱"; worksheet.Cells[1, 2].Value = "價格"; worksheet.Cells[1, 3].Value = "銷量"; worksheet.Cells[2, 1].Value = "大米"; worksheet.Cells[2, 2].Value = 56; worksheet.Cells[2, 3].Value = 100; worksheet.Cells[3, 1].Value = "玉米"; worksheet.Cells[3, 2].Value = 45; worksheet.Cells[3, 3].Value = 150; worksheet.Cells[4, 1].Value = "小米"; worksheet.Cells[4, 2].Value = 38; worksheet.Cells[4, 3].Value = 130; worksheet.Cells[5, 1].Value = "糯米"; worksheet.Cells[5, 2].Value = 22; worksheet.Cells[5, 3].Value = 200; using (ExcelRange range = worksheet.Cells[1, 1, 5, 3]) { range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; range.Style.VerticalAlignment = ExcelVerticalAlignment.Center; } using (ExcelRange range = worksheet.Cells[1, 1, 1, 3]) { range.Style.Font.Bold = true; range.Style.Font.Color.SetColor(Color.White); range.Style.Font.Name = "微軟雅黑"; range.Style.Font.Size = 12; range.Style.Fill.PatternType = ExcelFillStyle.Solid; range.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(128, 128, 128)); } worksheet.Cells[1, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191)); worksheet.Cells[1, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191)); worksheet.Cells[1, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191)); worksheet.Cells[2, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191)); worksheet.Cells[2, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191)); worksheet.Cells[2, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191)); worksheet.Cells[3, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191)); worksheet.Cells[3, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191)); worksheet.Cells[3, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191)); worksheet.Cells[4, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191)); worksheet.Cells[4, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191)); worksheet.Cells[4, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191)); worksheet.Cells[5, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191)); worksheet.Cells[5, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191)); worksheet.Cells[5, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191)); ExcelChart chart = worksheet.Drawings.AddChart("chart", eChartType.ColumnClustered); ExcelChartSerie serie = chart.Series.Add(worksheet.Cells[2, 3, 5, 3], worksheet.Cells[2, 1, 5, 1]); serie.HeaderAddress = worksheet.Cells[1, 3]; chart.SetPosition(150, 10); chart.SetSize(500, 300); chart.Title.Text = "銷量走勢"; chart.Title.Font.Color = Color.FromArgb(89, 89, 89); chart.Title.Font.Size = 15; chart.Title.Font.Bold = true; chart.Style = eChartStyle.Style15; chart.Legend.Border.LineStyle = eLineStyle.Solid; chart.Legend.Border.Fill.Color = Color.FromArgb(217, 217, 217); package.Save(); }