从C#中的Excel导入

我使用下面的代码将数据从excel文件保存到表中

private void RetrieveAndStoreExcelData(String filePath) { String excelConnectionStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source='" + filePath + "'; Extended Properties='Excel 8.0;'"; OleDbConnection excelConnection = new OleDbConnection(excelConnectionStr); excelConnection.Open(); try { //Get the name of the first worksheet DataTable schema = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); if (schema == null || schema.Rows.Count < 1) { throw new Exception("Error: Could not determine the name of the first worksheet."); } string firstSheetName = schema.Rows[0]["TABLE_NAME"].ToString(); //Retrieve data from worksheet into reader string query = "SELECT * FROM [" + firstSheetName + "]"; OleDbCommand command = new OleDbCommand(query, excelConnection); OleDbDataReader dbReader = command.ExecuteReader(); //IEnumerable //populate IEnumerable if (dbReader.HasRows) { populateRecords(dbReader); } } finally { excelConnection.Close(); } } 

这工作正常。 但是,如果其中一个字段的长度大于255个字符,那么它会将该string截断为255,并且这也是该行出现在Excel表格的第10行之后的时候。

因此,如果前10行的长度小于255,则假定所有行的长度小于255个字符。

那么有没有办法解决这个问题?