- [C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform
- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
- [VB.NET] Gởi tin nhắn và file đính kèm qua ứng dụng gởi tin nhắn Whats App
- [C#] Viết ứng dụng Auto Fill list Textbox from clipboard Winform
- [TOOL] Chia sẻ phần mềm thay đổi thông tin cấu hình máy tính
- [C#] Hướng dẫn Export dữ liệu ra file Microsoft Word Template
- [C#] Chia sẻ source code tool kiểm tra domain website
[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!