【发布时间】:2013-02-22 13:37:32
【问题描述】:
'正在使用 Microsoft.ACE.OLEDB.12.0 提供程序从 Excel 工作表中读取数据。
我正在使用OleDbDataReader 和他的GetValue() 来获取数据。
第一行/行(可以多于一个)是字符串标题,我不能跳过它。
接下来是设置为 0 小数位的数字数据,但当我选择其中一个时,它会以正确的小数格式显示在栏中。
如何以完整的原始十进制格式读取这些混合数据,如 Excel 中的条形? 我无法更改 Excel 工作表的设置。
这是我的代码:
using System.Data.OleDb;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string query = "SELECT * FROM [List1$]";
string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\Temp\Test.xls;Extended Properties=""Excel 12.0;HDR=NO;IMEX=1""";
using (OleDbConnection connection = new OleDbConnection(connString))
{
connection.Open();
using (OleDbCommand command = new OleDbCommand(query, connection))
{
using (OleDbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
object value = reader.GetValue(0);
}
}
}
}
}
}
}
【问题讨论】:
-
你试过用“.GetDecimal”代替“.GetValue”吗?
-
是的。 GetDecimal 抛出异常,因为有空格字符。 GetFieldType 返回字符串。所以它必须在 OleDbCommand 或 OleDbConnection 设置中的某个地方,可能是连接字符串中的某个属性。
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
标签: c# sql-server excel c#-3.0 oledb