Microsoft.office.interop.excel C#

Ở bài trước Tui có hướng dẫn cách thức xuất dữ liệu ra File Excel để báo cáo. Các bạn đã làm thành công, tuy nhiên một số bạn có Email nhờ hướng dẫn cách đọc dữ liệu trong file Excel rồi hiển thị lên WPF như thế nào. Nhân tiện có bài hướng dẫn Sinh viên trong Khoa thực hiện bài này nên Tui sẽ viết lại chi tiết cách lập trình

Microsoft.office.interop.excel C#

Hình trên minh họa có 1 tệp Excel danh sách nhân viên có các cột. Mã, Tên, Tuổi. Giao diện WPF nhấn vào “Dọc Excel” thì dữ liệu từ Excel sẽ được hiển thị lên giao diện WPF

File dữ liệu Excel ở đây. https. //duythanhcse. các tập tin. báo chí. com/2019/10/danhsachnhanvien. xlsx

Bây giờ tiến hành mã hóa WPF, tạo đại dự án bằng WPF. Ví dụ tên “K18411_N1 QuanLyNhanVien” (do tôi đang giúp Sinh Viên làm và note lại)

Sau đó ta nhấn chuột phải vào Reference/ chọn Add Reference

Microsoft.office.interop.excel C#

Lúc này màn hình Trình quản lý tham chiếu hiển thị như dưới đây

Microsoft.office.interop.excel C#

In screen on ta into Com mục. rồi tick chọn 2 thư viện

  • Microsoft Excel 16. 0 Thư viện đối tượng (tùy phiên bản của bạn)
  • Microsoft Office 16. 0 Thư viện đối tượng (tùy phiên bản của bạn)

Sau đó nhấn OK, nhìn vào Tài liệu tham khảo của dự án sẽ thấy 2 thư viện này

Microsoft.office.interop.excel C#

Tiếp tục tạo 2 thư mục Mô hình và IO cho dự án (bấm chuột phải vào dự án rồi chọn thư mục mới)

Microsoft.office.interop.excel C#

Trong Models tạo lớp Nhân Viên như dưới đây (bấm chuột phải vào Models/chọn Add Class)

namespace K18411_N1_QuanLyNhanVien.Models

{

[Serializable]

public class NhanVien

{

public string Ma { get; set; }

public string Ten { get; set; }

public int Tuoi { get; set; }

}

}

Trong IOs tạo một lớp tên ExcelFactory, nhiệm vụ của nó là đọc Excel và trả về danh sách nhân viên

Microsoft.office.interop.excel C#

In the layer ExcelFactory coding as after

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.Office.Interop.Excel;

using K18411_N1_QuanLyNhanVien.Models;

namespace K18411_N1_QuanLyNhanVien.IOs

{

public class ExcelFactory

{

public static List readFromExcelFile(string path)

{

List dsNV = new List();

try

{

Workbook MyBook = null;

Application MyApp = null;

Worksheet MySheet = null;

MyApp = new Application();

MyApp.Visible = false;

MyBook = MyApp.Workbooks.Open(path);

MySheet = (Worksheet)MyBook.Sheets[1];

int lastRow = MySheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell).Row;

int lastColumn = MySheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell).Column;

int step = 2;

for (int i = step; i <= lastRow; i++)

{

Range rowContent_cellLeft = MySheet.Cells[i, 1];

Range rowContent_cellRight = MySheet.Cells[i, 3];

System.Array rowContent = (System.Array)MySheet.get_Range(rowContent_cellLeft, rowContent_cellRight).Cells.Value;

if (rowContent.GetValue(1, 1) == null)

break;

string ma = rowContent.GetValue(1, 1) + "";

string ten = rowContent.GetValue(1, 2) + "";

int tuoi = int.Parse( rowContent.GetValue(1, 3) + "");

NhanVien nv = new NhanVien();

nv.Ma = ma;

nv.Ten = ten;

nv.Tuoi = tuoi;

dsNV.Add(nv);

}

MyBook.Close(true);

MyApp.Quit();

releaseObject(MySheet);

releaseObject(MyBook);

releaseObject(MyApp);

}

catch (Exception ex)

{

throw ex;

}

return dsNV;

}

private static void releaseObject(object obj)

{

try

{

System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);

obj = null;

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

obj = null;

}

finally

{ GC.Collect(); }

}

}

}

Bây giờ tạo một cửa sổ mới có tên là “frmQuanLyNhanVien” (bạn có thể sử dụng chính màn hình MainWindow để thiết kế cũng được),

Tui tạo màn hình mới. nhấn chuột phải vào dự án chọn Thêm/chọn Cửa sổ rồi đặt tên cửa sổ là frmQuanLyNhanVien

Microsoft.office.interop.excel C#

mã XAML




Mã hóa để gọi thư viện đọc Excel

using System.Collections.Generic;

using System.Windows;

using Microsoft.Win32;

using K18411_N1_QuanLyNhanVien.Models;

using K18411_N1_QuanLyNhanVien.IOs;

namespace K18411_N1_QuanLyNhanVien

{

/// 

/// Interaction logic for frmDanhSachNhanVien.xaml

/// 

public partial class frmDanhSachNhanVien : Window

{

public frmDanhSachNhanVien()

{

InitializeComponent();

}

List dsNV = null;

private void BtnDocExcel_Click(object sender, RoutedEventArgs e)

{

OpenFileDialog ofd = new OpenFileDialog();

if(ofd.ShowDialog()==true)

{

string file = ofd.FileName;

dsNV = ExcelFactory.readFromExcelFile(file);

dgNhanVien.ItemsSource = dsNV;

}

}

}

}

Truy cập ứng dụng. xaml chỉnh cho frmDanhSachNhanVien chạy trước – chỉnh StartupUri (Nếu lúc nào bạn giả sử dùng MainWindow thì không cần chỉnh lại App. xaml)