- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
- [C#] Flash Window in Taskbar Winform
- Download và Giải nén tập tin File sử dụng Powershell
- [C#] Hướng dẫn cách lấy thông tin đăng nhập tài khoản và mật khẩu web browser trên windows
- [VB.NET] CRUD Thêm xóa sửa tìm kiếm Realtime FireBase
- [C#] Hiển thị thông báo Toast Message trong lập trình Winform
- [C#] Cấu hình định dạng ngày tháng, thời gian trên Windows cho ứng dụng Winform
- [C#] Rút gọn đường dẫn link url với TinyURL API
- [C#] Hướng dẫn cách bo tròn winform with Radius
- [C#] Chia sẽ class BackGroundOverlay Show Modal cho Winform
- [C#] Hướng dẫn Flip Image Winform
- [C#] Invoke là gì? cách sử dụng phương thức Invoke()
- [C#] Hướng dẫn chia sẽ file, folder từ ứng dụng sang Zalo Chat
[C#] Hướng dẫn vẽ biểu đồ cột xuất file excel sử dụng thư viện Epplus
Xin chào các bạn, bài viết hôm nay mình sẻ tiếp tục hướng dẫn các bạn cách vẽ biểu đồ trên file Excel sử dụng thư viện Epplus C# Winform.
[C#] Export data to Excel file with chart Winform
Biểu đồ là một trong những thành phần không thể thiếu trong các báo cáo.
Khi nhìn các số liệu được biểu diễn trên biểu đồ chúng ta thấy trực quan và dễ hiểu hơn nhiều so với nhìn vào các con số trong bảng báo cáo.
Ví dụ: chúng ta có dữ liệu đất đai như hình bên dưới
Và bây giờ, mình muốn xuất dữ liệu ra Excel, với dữ liệu này thì vẽ biểu đồ hình cột cho mình.
Dưới đây là kết quả sau khi mình vẽ biểu đồ:
Source code Chart Excel C#:
using OfficeOpenXml;
using OfficeOpenXml.Drawing;
using OfficeOpenXml.Drawing.Chart;
using OfficeOpenXml.Style;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ExportChartToExcel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnExport_Click(object sender, EventArgs e)
{
FileInfo newFile = new FileInfo(@"chart.xlsx");
if (newFile.Exists)
{
newFile.Delete();
newFile = new FileInfo(@"chart.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 = "Loại đất";
worksheet.Cells[1, 2].Value = "Đồng bằng sông Hồng";
worksheet.Cells[1, 3].Value = "Trung du và miền núi Bắc Bộ";
worksheet.Cells[1, 4].Value = "Cả nước";
worksheet.Cells[2, 1].Value = "Đất nông nghiệp";
worksheet.Cells[2, 2].Value = 742;
worksheet.Cells[2, 3].Value = 1479;
worksheet.Cells[2, 4].Value = 9599;
worksheet.Cells[3, 1].Value = "Đất lâm nghiệp";
worksheet.Cells[3, 2].Value = 130;
worksheet.Cells[3, 3].Value = 5551;
worksheet.Cells[3, 4].Value = 14758;
worksheet.Cells[4, 1].Value = "Đất chuyên dùng và đất ở";
worksheet.Cells[4, 2].Value = 378;
worksheet.Cells[4, 3].Value = 426;
worksheet.Cells[4, 4].Value = 2263;
worksheet.Cells[5, 1].Value = "Đất khác";
worksheet.Cells[5, 2].Value = 246;
worksheet.Cells[5, 3].Value = 2688;
worksheet.Cells[5, 4].Value = 6485;
worksheet.Cells[6, 1].Value = "Tổng";
worksheet.Cells[6, 2].Value = 1496;
worksheet.Cells[6, 3].Value = 10144;
worksheet.Cells[6, 4].Value = 33105;
using (ExcelRange range = worksheet.Cells[1, 1, 1, 4])
{
range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
range.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
}
using (ExcelRange range = worksheet.Cells[1, 1, 1, 4])
{
range.Style.Font.Bold = true;
range.Style.Font.Color.SetColor(Color.White);
range.Style.Font.Name = "Tahoma";
range.Style.Font.Size = 11;
range.Style.Fill.PatternType = ExcelFillStyle.Solid;
range.Style.Fill.BackgroundColor.SetColor(ColorTranslator.FromHtml("#3A8A98"));
}
worksheet.Column(1).Width = 30;
worksheet.Column(2).Width = 40;
worksheet.Column(3).Width = 30;
worksheet.Column(4).Width = 30;
worksheet.Row(5).Height = 25;
worksheet.Row(2).Height = 25;
worksheet.Row(3).Height = 25;
worksheet.Row(4).Height = 25;
worksheet.Row(6).Height = 25;
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[1, 4].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[2, 4].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[3, 4].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[4, 4].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));
worksheet.Cells[5, 4].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
worksheet.Cells[6, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
worksheet.Cells[6, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
worksheet.Cells[6, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
worksheet.Cells[6, 4].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]);
ExcelChartSerie serie1 = chart.Series.Add(worksheet.Cells[2, 2, 5, 2], worksheet.Cells[2, 1, 5, 1]);
ExcelChartSerie serie2 = chart.Series.Add(worksheet.Cells[2, 4, 5, 4], worksheet.Cells[2, 1, 5, 1]);
serie.HeaderAddress = worksheet.Cells[1, 3];
serie1.HeaderAddress = worksheet.Cells[1, 2];
serie2.HeaderAddress = worksheet.Cells[1, 4];
serie.Fill.Color = ColorTranslator.FromHtml("#4D7DB6");
serie1.Fill.Color = ColorTranslator.FromHtml("#BA4D4C");
serie2.Fill.Color = ColorTranslator.FromHtml("#96B555");
chart.SetPosition(250, 10);
chart.SetSize(700, 300);
chart.Title.Text = "BIỂU ĐỒ ĐẤT ĐAI \r\n (Số liệu Laptrinhvb.net 2020)";
chart.Title.Font.Color = Color.FromArgb(89, 89, 89);
chart.Title.Font.Size = 15;
chart.Title.Font.Bold = true;
chart.Style = eChartStyle.Style8;
chart.RoundedCorners = true;
chart.Legend.Border.LineStyle = eLineStyle.Solid;
//chart.Legend.Border.Fill.Color = Color.
package.Save();
Process.Start("chart.xlsx");
}
}
}
}
Thanks for watching!