gpt4 book ai didi

c# - 我需要解决 Excel 猜测数据类型问题的方法

转载 作者:太空狗 更新时间:2023-10-29 21:15:12 25 4
gpt4 key购买 nike

我正在创建一个将数据从 Excel 导入 Oracle 数据库的实用程序,

我有一个excel文件的固定模板,

现在,当我试图通过 Jet 提供程序和 ADO.Net - Ole 连接工具导入数据时,我发现了以下问题:有些列没有被导入,因为它们中存在混合数据类型列[字符串和数字],

我在网上查了一下这个问题,发现原因是guessing data types from Excel

加载代码:

connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0};Extended Properties=Excel 8.0;");
string columns = "P_ID, FULL_NAME_AR, job_no, GENDER, BIRTH_DATE, RELIGION, MARITAL_STATUS, NAT_ID, JOB_Name, FIRST_HIRE_DATE, HIRE_DATE, CONTRACT_TYPE, GRADE_CODE, QUALIFICATION";
string sheetName = "[Emps$]";
OleDbCommand command = new OleDbCommand(string.Format("select {0} from {1} where p_id is not null", columns, sheetName), connection);

connection.Open();
dr = command.ExecuteReader();
DataTable table = new DataTable();
table.Load(dr);

我应该怎么做才能告诉 Excel 停止猜测并以文本形式提供数据?

如果没有,你能帮我解决任何问题吗?

提前致谢

最佳答案

我通过为连接字符串添加 IMEX=1 找到了解决方案,但它有一种特殊格式,在以下 link 中有描述。 .

The IMEX parameter is for columns that use mixed numeric and alpha values. The Excel driver will typically scan the first several rows in order to determine what data type to use for each column. If a column is determined to be numeric based upon a scan of the first several rows, then any rows with alpha characters in this column will be returned as Null. The IMEX parameter (1 is input mode) forces the data type of the column to text so that alphanumeric values are handled properly.

问候

关于c# - 我需要解决 Excel 猜测数据类型问题的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3595131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com