30/09/2018, 21:58

Hỏi về Import Excel to DataSet C# WPF

Mình đang xây dựng app WPF,cần load dữ liệu từ file Excel,đoạn mã load file :

        string srcFile = @"D:\test.xlsx";
        string connstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + srcFile + ";Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1";";

        OleDbConnection conn = new OleDbConnection(connstr);

        string strSQL = "SELECT * FROM [Sheet1$]";
        OleDbCommand cmd = new OleDbCommand(strSQL, conn);

        DataSet dataset = new DataSet();
        OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
        adapter.Fill(dataset);

        grid.DataContext = dataset;

và tất nhiên là ở ổ D mình có 1 file test.xlsx có duy nhất Sheet1 có nội dung,tuy nhiên khi chạy debug thì ở dòng adapter.Fill(dataset); nó không fill vào thằng dataset được,và dataset vẫn là có Count = 0 như lúc mới khởi tạo ? có ai biết tại sao nó ko Fill được vào dataset k ?
tks all !

vũ xuân quân viết 23:58 ngày 30/09/2018

mình tìm thấy trên stackoverflow

stackoverflow.com
Omri

OleDbException while trying to read excel file in c# - External table is not in the expected format

c#, excel, connection-string, oledb
asked by Omri on 08:25AM - 19 Aug 14

Có đoạn này
But make sure Microsoft Access Database Engine 2010 Redistributable package is installed on your machine

Bạn cần phải cài thêm gói này vào máy mới dùng được

Bài liên quan
0