Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

OleDB Read from Excel with Cells > 256 Chars?

Posted on 2011-03-14
3
Medium Priority
?
1,157 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

705 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