Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

OleDB Read from Excel with Cells > 256 Chars?

Posted on 2011-03-14
3
Medium Priority
?
1,162 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:Praveen Kumar
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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

972 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