C# WinForms中OLEDB读取Excel数据被截断至255字符,求代码级解决方案
Absolutely! You don’t need to mess with the Windows Registry to resolve that frustrating 255-character truncation issue when reading Excel files. The root cause is Excel’s default behavior: it samples only the first 8 rows (governed by the TypeGuessRows setting) to infer data types, which often misclassifies long text as truncated strings.
Thankfully, there are straightforward code-based solutions across common languages and libraries—here’s how to implement them:
Python (with Pandas)
Pandas relies on underlying engines to read Excel files; choosing the right engine and configuration bypasses the TypeGuessRows limitation entirely.
For .xlsx Files (OpenPyXL Engine)
OpenPyXL reads cells directly without relying on Excel’s type sampling, so it preserves full text content:
import pandas as pd # Option 1: Force all columns to read as strings to avoid truncation df = pd.read_excel("your_file.xlsx", engine="openpyxl", dtype=str) # Option 2: Target specific columns if you want to keep other data types df = pd.read_excel( "your_file.xlsx", engine="openpyxl", converters={"your_long_text_column": str} # Replace with your column name )
For .xlsb Binary Files (Pyxlsb Engine)
If you’re working with binary Excel files, use the pyxlsb engine for the same full-content preservation:
df = pd.read_excel("your_file.xlsb", engine="pyxlsb", dtype=str)
C# (with EPPlus)
EPPlus is a powerful library for reading/writing Excel files in .NET, and it ignores Excel’s default type guessing entirely. Just make sure to access the cell’s Text or Value property directly:
using OfficeOpenXml; using System.IO; // Set license context (required for EPPlus 5+) ExcelPackage.LicenseContext = LicenseContext.NonCommercial; using (var package = new ExcelPackage(new FileInfo("your_file.xlsx"))) { var worksheet = package.Workbook.Worksheets["Sheet1"]; // Replace with your sheet name int totalRows = worksheet.Dimension.Rows; int totalCols = worksheet.Dimension.Columns; for (int row = 1; row <= totalRows; row++) { for (int col = 1; col <= totalCols; col++) { // Read the full, untruncated content of the cell string fullContent = worksheet.Cells[row, col].Text; // Process your content here... } } }
VBA
If you’re working directly in Excel with VBA, you have two solid options:
Direct Cell Reading
Simply iterate through cells and read their raw value or text—this avoids any type inference issues:
Sub ReadFullExcelContent() Dim targetWorkbook As Workbook Dim targetSheet As Worksheet Dim cell As Range ' Open the workbook (set ReadOnly to True if you don't need to modify it) Set targetWorkbook = Workbooks.Open("C:\path\to\your_file.xlsx", ReadOnly:=True) Set targetSheet = targetWorkbook.Sheets("Sheet1") ' Replace with your sheet name ' Loop through all used cells in the sheet For Each cell In targetSheet.UsedRange Dim fullText As String fullText = cell.Value ' Use cell.Text if you need the formatted display text ' Process the full text here... Next cell ' Clean up targetWorkbook.Close SaveChanges:=False Set targetSheet = Nothing Set targetWorkbook = Nothing End Sub
ADODB Connection with Modified Properties
If you prefer using ADODB, tweak the connection string to disable TypeGuessRows sampling:
Sub ReadWithADODBNoTruncation() Dim conn As Object Dim recordSet As Object Dim sqlQuery As String Set conn = CreateObject("ADODB.Connection") Set recordSet = CreateObject("ADODB.Recordset") ' Connection string forces reading all rows to infer data types (TypeGuessRows=0) conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _ "Data Source=C:\path\to\your_file.xlsx;" & _ "Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1;TypeGuessRows=0;""" sqlQuery = "SELECT * FROM [Sheet1$]" ' Replace with your sheet name recordSet.Open sqlQuery, conn ' Loop through the recordset to get full content Do While Not recordSet.EOF Dim fullContent As String fullContent = recordSet("your_long_text_column").Value ' Replace with your column name ' Process content here... recordSet.MoveNext Loop ' Clean up recordSet.Close conn.Close Set recordSet = Nothing Set conn = Nothing End Sub
Key Takeaway
All these solutions work by either:
- Reading cell content directly without relying on Excel’s type inference, or
- Forcing the data reader to sample all rows (instead of just 8) to correctly identify long text fields.
No registry edits required—just adjust your code to bypass the default behavior!
内容的提问来源于stack exchange,提问作者Ezequiel Romano




