チュートリアル3(グラフ設定)を使用してExcelEpplusをエクスポートする
Export Excel Epplus Using Tutorial 3
Excel Epplusチュートリアル1のエクスポート(基本的な紹介)
Excel Epplusチュートリアル2のエクスポート(スタイル設定)
チュートリアル3(グラフ設定)を使用してExcelEpplusをエクスポートする
Excel Epplusチュートリアル4のエクスポート(その他の設定)
Epplusのチャートの実装は非常にシンプルで、基本的に私たちのニーズを満たすために、多くのチャートタイプもサポートしています。グラフの作成は、3段階のプロセスです(ヒストグラムの例)。
1、チャートを作成します
ExcelChart chart = worksheet.Drawings.AddChart(
'chart'
, eChartType.ColumnClustered)
//eChartType can choose chart type
2、データを選択します
このステップは重要なステップです。 chart.Series.Add()メソッドに必要なパラメーターは次のとおりです。chart.Series.Add(Y軸データ領域、X軸データ領域)
ExcelChartSerie serie = chart.Series.Add(worksheet.Cells[2, 3, 5, 3], worksheet.Cells[2, 1, 5, 1])
/ / Set the x and y axes of the chart
serie.HeaderAddress = worksheet.Cells[1, 3]
/ / Set the legend of the chart
3、グラフのスタイルを設定します
chart.SetPosition(150, 10)
/ / Set the location
chart.SetSize(500, 300)
/ / Set the size
chart.Title.Text =
'Sales trend'
/ / Set the title of the chart
chart.Title.Font.Color = Color.FromArgb(89, 89, 89)
/ / Set the color of the title
chart.Title.Font.Size = 15
//The size of the title
chart.Title.Font.Bold =
true
//The bold of the title
chart.Style = eChartStyle.Style15
/ / Set the style of the chart
chart.Legend.Border.LineStyle = eLineStyle.Solid
chart.Legend.Border.Fill.Color = Color.FromArgb(217, 217, 217)
/ / Set the style of the legend
基本的にチャートを生成することはそのようなことですが、特定のチャートに応じて、異なるチャート属性はわずかに異なる場合があります。
例の完全なコードは次のとおりです。
FileInfo newFile = new FileInfo(@'d: est.xlsx') if (newFile.Exists) { newFile.Delete() newFile = new FileInfo(@'d: est.xlsx') } using (ExcelPackage package = new ExcelPackage(newFile)) { ExcelWorksheet worksheet = package.Workbook.Worksheets.Add('test') worksheet.Cells.Style.WrapText = true worksheet.View.ShowGridLines = false//Remove the grid lines of the sheet worksheet.Cells[1, 1].Value = 'name' worksheet.Cells[1, 2].Value = 'price' worksheet.Cells[1, 3].Value = 'sales' worksheet.Cells[2, 1].Value = 'rice' worksheet.Cells[2, 2].Value = 56 worksheet.Cells[2, 3].Value = 100 worksheet.Cells[3, 1].Value = 'Corn' 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 = 'Microsoft Yahei' 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 = 'Sales trend' 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() }