Solved

C# reading Excel 2007 worksheet is limited to 255 characters per Cell

Posted on 2014-02-26
6
800 Views
Last Modified: 2014-02-26
Hi,
I have a c# application which is scccessfully reading data from various excel sheets. However once the cell exceeds 255 characters the value is either truncated to 255 or returned as null.  

If I run the code below on my windows 7 64bit o/s - It reads the full contents of the cell which has more than 255 characters (700 characters). However if I run it on my windows 7 32bit o/s laptop then it returns null for every cell exceeding 255 characters.

Can anyone tell me why it works in 64b but not in 32b. see code below


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string con = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
                                    @"Data Source=c:\temp\Book1.xlsx;" +
                                    @"Extended Properties='Excel 8.0;HDR=Yes; IMEX=1;'";

            using (OleDbConnection connection = new OleDbConnection(con))
            {
                connection.Open();
                OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);

                var TranArr = new string[999, 20];
                int i = 0;

                using (OleDbDataReader dr = command.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        TranArr[i, 1] = dr[0].ToString();
                        TranArr[i, 2] = dr[1].ToString();

                        Console.WriteLine(TranArr[i, 1]);
                        Console.WriteLine(TranArr[i, 2]);
                    }
                }
            }

            Console.ReadKey();

        }
    }
}
0
Comment
Question by:pclarke7
  • 3
  • 3
6 Comments
 
LVL 12

Accepted Solution

by:
HugoHiasl earned 500 total points
Comment Utility
If you can be sure that the data is string then you could try:

                       TranArr[i, 1] = dr.GetString(0);
                        TranArr[i, 2] = dr.GetString(1);

Open in new window

0
 

Author Comment

by:pclarke7
Comment Utility
Thanks Hugo,
this particular cell will always be a string whereas the spreadsheet is a mix of strings and numerics. I assume that I can use dr.GetString(n) for every string field that could exceed 255 characters. Do you see any problems with this ?

regards
Pat
0
 
LVL 12

Expert Comment

by:HugoHiasl
Comment Utility
No... The only problem with using GetString is that it could lead to problems if the value in the cell is not a string. It depends on the implementation of the data provider if it makes an implizit conversion.

dr[] returns the data in an internal data format (which probably is limited to 255 characters)
dr.GetString() returns it as a more or less unlimited sized string.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:pclarke7
Comment Utility
Hi Hugo,
tried this and it works perfectly until it finds an null entry in the cell and then it crashes with the following error  "Unable to cast object of type 'System.DBNull' to type 'System.String'."

any suggestions how I can get away without have to populate a string value into every instance of this cell.?

regards
Pat
0
 
LVL 12

Expert Comment

by:HugoHiasl
Comment Utility
That's not a problem.

    if (!dr.IsDBNull(0)) 
        TranArr[i, 1] = dr.GetString(0);
    } else {
        TranArr[i,1] = "";
    }

Open in new window

0
 

Author Closing Comment

by:pclarke7
Comment Utility
Thanks Hugo,
your a star !!

I was just at the point of re-designing the application because of this restriction. I have been through so many posts on this issue over the past week but you are the first person to supply a simple workaround that works.

Pat
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

763 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

9 Experts available now in Live!

Get 1:1 Help Now