我正在尝试将数据从Excel文件读取到DataSet中,但我的问题是DataTable列名称与Excel中的列名称不匹配。似乎只有列名很长的列。在DataTable列中,它会在某一时刻被截断。
这是我的尝试:
DataSet mainExcelDataToImport = new DataSet();
using (OleDbConnection mainExcelOleDbConnection = new OleDbConnection())
{
string theMainExcelConnectionString = this.ExcelConnectionString.Replace("{FullFilePath}", this.SelectedFilePath);
mainExcelOleDbConnection.ConnectionString = theMainExcelConnectionString;
// Open
mainExcelOleDbConnection.Open();
string mainExcelSQL = "SELECT * FROM [{ExcelSheet}$]";
mainExcelSQL = mainExcelSQL.Replace("{ExcelSheet}", selectedExcelSheet);
using (OleDbCommand mainExcelOleDbCommand = mainExcelOleDbConnection.CreateCommand())
{
mainExcelOleDbCommand.CommandText = mainExcelSQL;
mainExcelOleDbCommand.CommandType = CommandType.Text;
// Prepare
mainExcelOleDbCommand.Prepare();
using (OleDbDataAdapter mainOleDbDataAdapter = new OleDbDataAdapter(mainExcelOleDbCommand))
{
mainOleDbDataAdapter.Fill(mainExcelDataToImport);
// Close
mainExcelOleDbConnection.Close();
}
}
}这是我试过的.xlsx:
A1234567890123456789012345678901234567890123456789012345678901234567890
TEST
'A12345.....' is the Column Name at A1
'TEST' is the Value at A2
When I check the `ColumnName` in the `DataTable` I get: 'A123456789012345678901234567890123456789012345678901234567890123' which is 64 characters.ColumnName MaxLength限制是如何工作的?
我能摆脱它吗?
这可能是OleDbDataAdapter上的一个限制
如有任何帮助/建议,我们将不胜感激。
发布于 2019-05-05 21:02:40
对于数据库列的名称来说,64个字符非常长。我从来没有见过列名有这么长。
有这么长的列名似乎很奇怪。
当然,它们必须存储在某个地方,所以任何数据库软件都可能有一个最大值,例如SQL server最多有128个。如果像sqlclient这样的中间软件有一个更低的限制,我不会感到惊讶。很长的名字是很不寻常的。
这可能是适配器的限制。
您可以尝试本文中提到的一些替代方案:
Best /Fastest way to read an Excel Sheet into a DataTable?
或者,您可以只使用xml -而不是datatable。Xml将能够处理excel所能处理的任何大小字段。
https://docs.microsoft.com/en-us/office/open-xml/understanding-the-open-xml-file-formats https://docs.microsoft.com/en-us/office/open-xml/how-to-parse-and-read-a-large-spreadsheet i
https://stackoverflow.com/questions/55991927
复制相似问题