简体   繁体   中英

OleDbDataAdapter leaves out Data from Excel Cells

Update:

Using the open source ExcelDataReader ( https://www.nuget.org/packages/ExcelDataReader/ ) solved the problem.


I'm reading in Data from an .xlsx File like this

DataSet ds = new DataSet();
OleDbConnection cn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties= Excel 12.0;");

cn.Open();

OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM ["Sheet1$"]", cn);
da.Fill(ds, "Sheet1$");

cn.Close();

And Everything seems to work fine, BUT in two specific rows the adapter just cuts off the last line of one Cell(both affected rows have the same content in that specific Cell).

ie:

"1 : 0x01 : Text_Example1(sometext)
 2 : 0x02 : Text_Example2(sometext)
 3 : 0x04 : TexT_Example3(sometext)"

becomes

"1 : 0x01 : Text_Example1(sometext)
 2 : 0x02 : Text_Example2(sometext)
 3 :"

What is the Problem here and how can i fix it? To me it seems totally random, because in all the other rows this problem doesn't occur.

edit: I have already tried it with extended properties HDR = NO/YES and IMEX = 0/1 like suggested on many related Questions, but that didn't do the trick either.

If I may suggest an alternative approach, an .xlsx file is an OpenXML file -- a ZIP file containing XML and other metadata.

Microsoft has an OpenXML API that allows you to manipulate such files. That's the "native" way to access the data, and should behave correctly.

There's an open source project called ClosedXML that aims to make the OpenXML API more developer-friendly. I haven't used it myself, but have seen it successfully used in existing code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM