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

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

问题描述

我正在使用此代码读取具有OLDB Connection的Excel文件

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

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天全站免登陆