Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am Using this code to read from excel but there are some cells{D2 , D4 }the prog . cant read them from the excel file which contain ('$') {D2 , D4} any help to read this cells ..
try
            {
                OpenFileDialog ex = new OpenFileDialog();
                ex.Filter = "XLS|*.xls|All|*.*";
                if (ex.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    try
                    {
                        var connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", ex.FileName);
                        var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet7$]", connectionString);
                        var ds = new DataSet();
                        adapter.Fill(ds);
                        DataTable data = ds.Tables[0];
                        DATAGRID1.DataSource = data;
                    }


So how to get this values as shown in the picture

http://postimg.org/image/xeanxst5v/[^]
Posted
Comments
Kornfeld Eliyahu Peter 8-Feb-15 7:42am    
What do you mean by "can't read"? Do you got a different value than expected, or got some error?
Dev_Fady... 8-Feb-15 8:10am    
check this photo and you will understand what i mean
http://postimg.org/image/xeanxst5v/
Kornfeld Eliyahu Peter 8-Feb-15 8:12am    
I can't...Can you explain it in words?
Dev_Fady... 8-Feb-15 9:10am    
sure
i have a column with this values on cells
100
105 $
110 $
200
when i use the above code
the returen is as
100


200
all cells with $ don't appears
so can help me to solve it ...
Kornfeld Eliyahu Peter 8-Feb-15 9:11am    
At last I was able to see the image...See my answer...

Finally I was able to see the image you posted (better explain it by words), and understood that some of the values (those with $ in it) return as empty...
The problem is that Excel has no data type, so the OELDB driver have to decide what datatype to assign to every column upon reading it...
In case of mixed types the driver will decide using a look-ahead method, where it will check a certain number of rows and will decide according the majority - in your case the driver decided that this column is numeric and wiped out any non-numeric values...
You can change it by adding some extended attributes to the connection string...
C#
Extended Properties=\"HDR=YES;TypeGuessRows=0;ImportMixedTypes=Text\"

This will tell the driver to read the first row as header-text, do not do any type guessing and use Text as type for mixed columns...
There is more info about Excel connection here: https://www.connectionstrings.com/excel/[^]
 
Share this answer
 
Comments
Richard MacCutchan 8-Feb-15 9:31am    
Sorry, but that will not work, for the reasons I outlined in my answer. The contents of any non-text cell (numbers, dates etc) are stored as numeric values, In order to display them in a particular format an editing template has to be attached to the cell. But that is not used by the Jet driver, it just extracts the cell contents.
Kornfeld Eliyahu Peter 8-Feb-15 9:36am    
According to the image and the clarifications of OP it is a classic case of mixed types...One cell has the value of 1000 while the other has the value of 1000$ (no formatting but actually the $ sign included)...
Richard MacCutchan 8-Feb-15 9:43am    
Makes no difference what the OP posts, I suspect that in the latter case he has changed the cells to text strings, rather than formatted numbers. It's easy enough to demonstrate.
Dev_Fady... 9-Feb-15 2:16am    
Kornfeld Eliyahu Peter
Really Thanks For your Support and sorry for make a more effort when you try to read the photo but ... when i try your solution this error appears to me .
"Format of the initialization string does not conform to specification starting at index 96."
so can support me why this error .... :(
?!!
Kornfeld Eliyahu Peter 9-Feb-15 2:17am    
Use double-double-quote ("") instead of \"...
Most likely because what you see in Excel is the display format of a numeric value. When you read a cell's value you just get the number that is stored inside it. To get the formatting information you may need to use the Excel.Interop.
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 8-Feb-15 8:29am    
I think that's not the problem...Please see my answer...
Dev_Fady... 8-Feb-15 9:01am    
Richard MacCutchan
First thanks for you support but in other example
check this photo
http://postimg.org/image/5u96d3d11/
when i change all column values the reader read all of the so i cant under stand it
Richard MacCutchan 8-Feb-15 9:32am    
What do you mena by "change all column values"?
Dev_Fady... 9-Feb-15 1:54am    
i mean that when i make the columns values
100 $
200 $
300 $
400 $
the program read them as they in excel
100 $
200 $
300 $
400 $
Dev_Fady... 9-Feb-15 2:28am    
i dont know more about the Excel.Interop
can you send me what the edit in code i need to achieve this problem ...

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