C#读取Excel文件数据

本文介绍了一种使用Microsoft.Jet.OLEDB驱动程序来读取Excel文件的方法,并提供了详细的C#代码示例,展示了如何通过DataReader进行数据读取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

相当简单,Excel就像数据库,每个Sheet就是一个Table. Microsoft.Jet.OLEDB驱动.
之后是DataReader循环,或DataSet处理都非常简单.

HTTP://BLOG.youkuaiyun.com/kissyougoodby/
注意:数据类型的转换!!

#region set connection
string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= "+this.txtPath.Text+";Extended Properties=Excel 8.0;";
myDataReader = null;
craboDbConnection = new OleDbConnection(strConn);
OleDbCommand myOleDbCommand = new OleDbCommand("SELECT * FROM [Sheet1$]", myOleDbConnection);
#endregion

try
{
myOleDbConnection.Open();
myDataReader = myOleDbCommand.ExecuteReader();
while (myDataReader.Read())
{
this.txtSeq.Text=Convert.ToString(myDataReader.GetValue(0));//列1
this.txtName.Text=Convert.ToString(myDataReader.GetValue(1));//列2
this.txtPIN.Text=Convert.ToString(myDataReader.GetValue(2));//列3
}
}
#region Catch
catch(System.Threading.ThreadAbortException e)
{
System.Threading.Thread.ResetAbort();
this.lblResult.Text = "线程被中断..."+e.Message;
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
finally
{
// Always call Close when done reading.
if (myDataReader != null)
myDataReader.Close();

// Close the connection when done with it.
if (craboDbConnection!=null && craboDbConnection.State == ConnectionState.Open)
craboDbConnection.Close();

if(webResponse!=null)
webResponse.Close();
}
#endregion

引自: http://tb.blog.youkuaiyun.com/TrackBack.aspx?PostId=547149
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值