Ado net - import excel data - missing data

At first glance, I have the same problem as many people, and I found many questions and answers about my problem, but none of them helped me.

I am importing from an MS excel file (.xls file) to NET using ADO NET. The file contains mixed types in one column: numbers and text, as well as a known problem - the text format is not recognized and data is lost.

I use the following connection string with recommended parameters:

string strConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\";", pathname);

This is my code:

OleDbConnection sqlConn = new OleDbConnection(this.strConnectionString);

sqlConn.Open();

OleDbDataAdapter sqlAdapter = new OleDbDataAdapter();

string sql = "SELECT * FROM [" + sheetName + "]";

OleDbCommand selectCMD = new OleDbCommand(sql, sqlConn);

sqlAdapter.SelectCommand = selectCMD;

DataTable dt = new DataTable(sheetName);

sqlAdapter.Fill(dt);

I tested it under NET 3.5 (x86) and NET 4.0 (x86) (also tested as windows exe and asp net versions), and the problem remains.

I do not know that I am doing something wrong, but I spent many hours and the problem still remains.

+2
3

NET (MIT License) NET, xls xlsx. oledb.

:

+2

Windows, Excel .

ForceExcelImportAsText.reg, , Windows.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Jet\4.0\Engines\Excel]
"TypeGuessRows"=dword:00000000


[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Excel]
"TypeGuessRows"=dword:00000000

, Excel , .

, , Excel / , , , Excel /, , , - , .


Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\YourExcelFile.xls;Extended Properties="Excel 8.0;HDR=YES;IMEX=1"

" Excel 8.0" .xls Excel 2003.
Excel 2007-2013 .xlsx " Excel 12.0 Xml"
Excel 2007-2013 .xlsb " Excel 12.0"
Excel 2007-2013 .xlsm Excel 12.0 Macro "

, " Microsoft.Jet.OLEDB.4.0" .xls Excel 2003.
Excel 2007-2013 xlsx/xlsb/xlsm Microsoft.ACE.OLEDB.12.0.

+1

, ado.net , , :

  • , , Excel, script, Excel . xml. Xls2Xml.vbs ' VB Script Document if WScript.Arguments.Count < 2 Then WScript.Echo "Error! Please specify the source path and the destination. Usage: Xls2Xml SourcePath.xls Destination.xml" Wscript.Quit End If Dim oExcel Set oExcel = CreateObject("Excel.Application") Dim oBook Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0)) oBook.SaveAs WScript.Arguments.Item(1), 46 oBook.Close False oExcel.Quit Wscript.Quit 46 xlXMLSpreadsheet. script, XML ( XSLT, script)

  • Excel , XLSX ClosedXML, . XLSX,

Excel , - (, ). , Excel, .

OpenXML SDK , DocumentFormat.OpenXml.dll, / XLSX

0

Source: https://habr.com/ru/post/1537463/


All Articles