我使用以下代码将数据从 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 个字符,那么它会将字符串截断为 255,这也是该行出现在 excel 表中的第 10 行之后的情况。
因此,如果前 10 行的长度小于 255,则假定所有行的长度都小于 255 个字符。
那么有什么办法可以解决这个问题吗?