Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,
I am using VS2010 Wpf application with c#. I am read the xlsx file using oledb connection its working fine but after reading my xlsx some data missing in the reading data set.

Here i attached my Coding and i have the screen shot of the problem but here no option is there for attaching screen shot.

C#
private DataTable ReadExcel(string tabName)
       {
           DataTable retTbl = new DataTable();
           DataTable dtRead = new DataTable();
           try
           {


               System.Data.OleDb.OleDbConnection MyConnection;
               System.Data.DataSet DtSet;
               System.Data.OleDb.OleDbDataAdapter MyCommand;
               MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\csharp.net-informations.xls';Extended Properties=Excel 8.0;");
               MyConnection = conn;
               MyConnection.Open();
               MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [" + tabName + "$]", MyConnection);
               MyCommand.TableMappings.Add("Table", "TestTable");
               DtSet = new System.Data.DataSet();
               MyCommand.Fill(DtSet);
               dtRead = DtSet.Tables[0];
               MyConnection.Close();
           }

           catch (Exception ex)
           {
              MessageBox.Show(ex.ToString());
           }

           return dtRead;
       }


Ex:
Consider as Excel Columns

' c1' c2 ' c3'
---------------
' 1 ' Test ' 0 '
---------------
' 3 ' 1 ' 4 '
----------------

After Read the Excel:

' c1' c2 ' c3'
---------------
' 1 ' ' 0 '
---------------
' 3 ' 1 ' 4 '
----------------

see the Above example "Test" is missing after Read excel.

sorry for my English..

Thanks all,
Regards,
karthik M

What I have tried:

i am tried to read the XLSX file.
Posted
Updated 31-Jul-19 4:38am
v3
Comments
OriginalGriff 9-May-16 2:58am    
And?
What's the problem?
Any error message? What happens that you didn't expect, or doesn't happen that you did?
And don't give us screen shots: explain, and copy and paste error messages.
Use the "Improve question" widget to edit your question and provide better information.
Karthik_Mahalingam 9-May-16 4:05am    
What data missing, Please be more specific.
Use Improve question to add more info.
wilford.ramsey 28-Jun-17 7:42am    
You should try this code to read a .xlsx file in C#.

You need the Microsoft ACE engine to read XLSX files. See Working with MS Excel(xls / xlsx) Using MDAC and Oledb[^].
 
Share this answer
 
A quick thought: you say it's an XLSX file, but two things I note:
1) It's an XLS extension, not an XLSX - XLS files aren't the same at all.
2) You're trying to read it as Excel 8.0, but XLSX files weren't introduced until V12.0 with Excel 2007: Microsoft Excel - Wikipedia, the free encyclopedia[^]
So check your file: see what's in it.
 
Share this answer
 
it looks only string data gets missing, to resole this, Use 'IMEX=1' in connection string it helps you to read data in string format
Use this one when you want to treat all data in the file as text, overriding Excels column type "General" to guess what type of data is in the column.
C#
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;
Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1";

hope it helps
 
Share this answer
 
Comments
Member 11889799 11-May-16 1:32am    
Hi prasad,
Thanks for your response its working fine.Thanks you lot.
Regards,
Karthik
koolprasad2003 11-May-16 2:07am    
Please accept it as solution if it resolved :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900