Thứ Hai, 21 tháng 6, 2021

[ASP.NET] Read , Write Excel file với thư viện của MS Office

Một số lập trình viên hay sử dụng bộ thư viện Interop.Microsoft.Office.Interop.Excel.dll của MS Office để xử lý file Excel (VD : import, export dữ liệu từ file Excel)

Với thư viện này bắt buộc trên máy chủ phải có cài đặt (install) MS Office. Nếu không khi chay sẽ báo lỗi như bên dưới 

Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))


Đối với thư viện Microsoft cũng khuyên cáo không sử dụng và không được Microsoft hỗ trợ để chạy cùng với các ứng dụng ngoài khác (bao gồm ASP, ASP.NET )


"Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment."

https://support.microsoft.com/en-us/topic/considerations-for-server-side-automation-of-office-48bcfe93-8a89-47f1-0bce-017433ad79e2

Nếu bạn buộc phải sử dụng thư viện này và không thể thay đổi thì cần sử dụng VPS riêng để có thể cài đặt MS Office. 

Hoặc có thể tham khảo các thử viện khác hỗ trợ thao tác trên file Excel

https://nugetmusthaves.com/Tag/excel

https://www.codeproject.com/Articles/1088970/Read-Write-Excel-file-with-OLEDB-in-Csharp-without

https://github.com/ExcelDataReader/ExcelDataReader



Không có nhận xét nào: