[已解决]无法使用C#从Windows窗体读取Excel文件 [英] [Solved] Unable to read an excel file from windows form using c#

查看:334
本文介绍了[已解决]无法使用C#从Windows窗体读取Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够从Windows窗体中读取excel文件,但问题是其中一个包含数字之类的特定列
列名称
1,2,3,4,5,6,7
1,2,3,4,5
7
7
1
6
我读取了所有值并将其放入一个数据表OTable
当我查看该表时,该表包含单个值,如
7
7
1
6
显示空值
问题是只读取一列的单个数字.我将代码放在下面

I am able to read the excel file from windows form but the problem is one particular column containing numbers like
column name
1,2,3,4,5,6,7
1,2,3,4,5
7
7
1
6
I read all the values and put it in one datatable OTable
when I view the table the table which contains single value like
7
7
1
6
are showing null values
problem is only reading the single number of a column. I put the code below

OleDbConnection oledbConn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\My Documents\Visual Studio 2005\Projects\calendarautomation\calendarautomation\masterlist1.xls;Extended Properties=Excel 4.0");
            oledbConn.Open();
            OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn);
            OleDbDataAdapter oleda = new OleDbDataAdapter();
            oleda.SelectCommand = cmd;
          
            DataTable oTable = new DataTable();
            oleda.Fill(oTable);
            dataGridView1.DataSource = oTable;
           
            oledbConn.Close();



OP更新:
现在通过将提供程序更改为ace.12.0可以正常工作了.



UPDATE from OP:
Now it is working fine by changing provider to ace.12.0 it is working

推荐答案

,oledbConn); OleDbDataAdapter oleda =新的OleDbDataAdapter(); oleda.SelectCommand = cmd; DataTable oTable = new DataTable(); oleda.Fill(oTable); dataGridView1.DataSource = oTable; oledbConn.Close();
", oledbConn); OleDbDataAdapter oleda = new OleDbDataAdapter(); oleda.SelectCommand = cmd; DataTable oTable = new DataTable(); oleda.Fill(oTable); dataGridView1.DataSource = oTable; oledbConn.Close();



OP更新:
现在,通过将提供程序更改为ace.12.0可以正常工作.



UPDATE from OP:
Now it is working fine by changing provider to ace.12.0 it is working


问题已由OP自己解决.
将其从未回答的问题中删除.
Issue was resolved by OP himself.
Removing it from Un-Answered Questions.


这篇关于[已解决]无法使用C#从Windows窗体读取Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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