I'm trying to read some data from an SQL Server 2008 database into an Excel 2007 spreadsheet with C#, using this connection string:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=foo.xlsx;Extended Properties='Excel 12.0 XML;HDR=YES'
One of the columns in the database is a VARCHAR(1000). When I try recreating the schema in the spreadsheet, it seems like Excel's VARCHAR only supports up to 255. This page suggests that the "Total number of characters that a cell can contain" is around 32K, so in principle, it should be possible to get a longer string in.
Is there a simple way to work around the 255 char limit?
Although XLOPER12 will now support a string up to 32,767 Unicode characters long, xlfEvaluate (and other) excel C-Api function continues to be limited to 255 characters long in Excel 2010. It will return xltypeErr if it is passed an XLOPER12 with a string longer than 255.
All strings the user sees in Excel have for many versions now been stored internally as Unicode strings. Unicode worksheet strings can be up to 32,767 (215 - 1) characters in length and can contain any valid Unicode character.
When the C API was first introduced, worksheet strings were byte strings limited in length to 255 characters, and the C API reflected these limitations. With Excel 2007, the C API is updated to handle Excel long Unicode strings. This means that DLL functions registered in the right way can accept Unicode arguments and return Unicode strings.
Note: Byte strings are still fully supported in the C API for backward compatibility, however they still have the same 255-character limit. No easy solution other than to truncate the string, or divide the string into multiple cells.
来源:https://stackoverflow.com/questions/9711489/varchar-size-limit-in-excel-spreadsheets