从 Excel 导入数据集之前操作数据
我的 Excel 工作表中有几列数据,我必须将其导入到我的应用程序中。
我这样做是使用 -
string strConn;
OleDbDataAdapter oledaExcelInfo;
strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Server.MapPath(strSavePath + strRepFileName) + ";Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1\"";
oledaExcelInfo = new OleDbDataAdapter("SELECT * FROM [Book1]", strConn);
dsetExcelInfo = new DataSet();
oledaExcelInfo.Fill(dsetExcelInfo, "CCInfo");
虽然成功导入数据,但有时数据导入不正确,即有时可以将数字 1234567
导入为 1.23E+06
这可以解决,如果excel 文件中的数据是 '1234567
而不是 1234567
(附加单引号)
我现在尝试操作从 excel 获取的数据,以便在导入之前我可以通过编程方式将 '
附加到所有值的数据,以防止错误导入。
我什至尝试使用 OleDbDataAdapter.Update 但我想这不会有帮助,因为这种情况发生在导入数据之后。这是正确的吗?
我可以操纵数据以便导入正确的数据吗?我该怎么做呢?
I have a couple of columns of data in an excel sheet which I have to import to my application.
I do this using -
string strConn;
OleDbDataAdapter oledaExcelInfo;
strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Server.MapPath(strSavePath + strRepFileName) + ";Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1\"";
oledaExcelInfo = new OleDbDataAdapter("SELECT * FROM [Book1]", strConn);
dsetExcelInfo = new DataSet();
oledaExcelInfo.Fill(dsetExcelInfo, "CCInfo");
While this imports data successfully, sometimes the data is imported incorrectly i.e. sometimes a number 1234567
could be imported as 1.23E+06
This can be solved if the data in the excel file is '1234567
instead of 1234567
(append the single quotation mark)
I am now trying to manipulate data that I fetch from the excel so that before I import the data I can programmatically append a '
to all the values to prevent the incorrect import.
I even tried using OleDbDataAdapter.Update but I guess this will not help as this happens after the data is imported. Is that correct?
Can I manipulate data so that I import the correct data? How can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用开源NPOI库从Excel导入数据并保留数据的格式,因为它是在电子表格即
1234567
将作为1234567
导入,而不是以1.23E+06
等不同格式导入数据,而不是如图所示导入数据上面的问题,我使用 NPOI 库导入数据 -
I used the open source NPOI library to import data from excel and preserve format of the data as it was in the spread sheet i.e.
1234567
would be imported as1234567
instead of importing data in a different format like1.23E+06
Instead of importing data as shown in the question above, I import data using NPOI library -
我刚刚遇到了类似的问题(在这里找到它:Quirky SELECT from通过 OleDbDataAdapter 方法 (C#) 的 Excel 文件。
如果您只想使用 .NET 框架而不使用外部库,请尝试使用以下连接字符串:
将引擎从 JET 切换到 ACE 对我来说是这样。
I just got a similar problem ( find it here: Quirky SELECT from Excel file via OleDbDataAdapter method (C#) ).
Try with the following connection string, if you want to use just .NET framework without external libraries:
Switching the engine from JET to ACE did it for me.