问题在C#中的Excel文件中读取 [英] Problems reading in an Excel file in C#

查看:171
本文介绍了问题在C#中的Excel文件中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读一个Excel与OLDB连接使用此代码

I'm reading an Excel file with OLDB Connection using this code

        var connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", fileName);

        var fileName = string.Format("{0}\\s23.xls", Directory.GetCurrentDirectory());
        var adapter = new OleDbDataAdapter("SELECT * FROM [TEJ3$]  ", connectionString);
        DataTable dt=new DataTable();
        adapter.Fill(dt, "Table1");

和乳宁这个代码后我的数据表被填满。但我有一个有许多字串细胞和几个空单元格的列;在Excel文件,该细胞具有数值。

and after runing this code my data table is filled. But I have a column that has many string cells and few empty cells ; in excel file this cells have numeric values.

有人有一个想法?

推荐答案

检查这里的第一个例子: http://www.connectionstrings.com/excel

Check the first examples here: http://www.connectionstrings.com/excel

什么往往不顺心的是,Excel将估计基于第一个X行的列的类型。当后该值不匹配,这些行获得空值。恐怕进入注册表是一段让Excel驱动程序先扫描所有行(如connectionstrings.com文章中介绍)的唯一途径。

What often goes wrong is that Excel will estimate the type of a column based upon the first X rows. When after that the values don't match, these rows get empty values. I'm afraid that going into the registry is sometime the only way to get the Excel driver to scan all rows first (as described in the connectionstrings.com article).

玩弄环境中的HDR和IMEX设置。在某些情况下,这将有所帮助。

Play around with the HDR and IMEX settings in your environment. In some cases that will help as well.

这篇关于问题在C#中的Excel文件中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆