Solved

OleDB Read from Excel with Cells > 256 Chars?

Posted on 2011-03-14
3
1,124 Views
Last Modified: 2012-05-11
Hi,

i am trying to read Data from an Excel spreadsheet. My code works great, but it can not read cells with more than 256 chars.
Is there a solution for it?


OleDbConnection DBConnection;
OleDbCommand DBCommand;

DBConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + @"c:/temp/lpa.xls" + ";"
                + "Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\"");
string SQLString = @"Select [F1],[F2],[F3],[F4],[F5],[F23] FROM [Sheet1$Print_Area]";
DBCommand = new OleDbCommand(SQLString, DBConnection);

           try
            {
                DBConnection.Open();
                DataTable ExcelData = new DataTable();
                ExcelData.Columns.Add("Item", typeof(int));
                ExcelData.Columns.Add("Description", typeof(string));
                ExcelData.Columns.Add("Station", typeof(string));
                ExcelData.Columns.Add("Location", typeof(string));
                ExcelData.Columns.Add("SampleSize", typeof(string));
                ExcelData.Columns.Add("ReactionPlan", typeof(string));
                DBReader = DBCommand.ExecuteReader();
                int num;
                while(DBReader.Read())
                {
                    bool isNum = int.TryParse(DBReader[0].ToString(), out num);
                    if (isNum)
                    {
                        ExcelData.Rows.Add(num, valid(DBReader, 1), valid(DBReader, 2), valid(DBReader, 3), valid(DBReader,              4), valid(DBReader, 5));
                }
                   DB.Connection.Close();
                 }
                           }
            catch (System.Exception ex)
            {
                DBConnection.Close();
                if (DBReader != null)
                {
                    DBReader.Close();
                }


                MessageBox.Show(ex.Message.ToString());

            }
            finally
            {
                MessageBox.Show("Data Inserted");
            }

Open in new window

0
Comment
Question by:ktpoitm
3 Comments
 
LVL 12

Expert Comment

by:Mamtha1982
ID: 35136365
Try using ODBC-driver instead of Oledb
0
 
LVL 1

Author Comment

by:ktpoitm
ID: 35138085
The ODBC-driver did not work since it has a bug with the First Column Name.

I found a registry key that allowed me to change the row scan value from 8 to 30. It sees the Cells as Memos now and reads all the chars.

Thanks anyways for you help.
0
 
LVL 3

Accepted Solution

by:
Sebastian_OH earned 500 total points
ID: 35138273
Change the following Reg key

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Excel]

 REG_DWORD "TypeGuessRows".

That's the key to not letting Excel use only the first 8 rows to guess the columns data type. Set this value to 0 to scan all rows.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now