C#读取EXCEL数据
2012-10-24 16:02
337 查看
#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
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
相关文章推荐
- 使用c#读取excel中的数据
- C# 读取Excel数据 并对日期是数字的进行处理
- c# 读取excel数据的两种方法
- c#读取Excel数据到Gridview
- C#读取excel文件数据丢失问题
- C#读取Excel数据
- C#读取Excel数据
- C#操作读取excel数据
- C#读取Excel数据
- C#读取EXCEL 文件同时向文件中写入数据和Excel的Range对象
- C# web 读取excel并导入数据库
- C# 使用OLEDB读取不同版本Excel数据的连接字符串
- C#读取Excel数据在CAD上展图
- C#使用sql语句读取excel文件数据
- C#读取Excel数据
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- C# 读取Excel中的数据到DataTable中
- c#中采用OLEDB方式来读取EXCEL文件和将数据写入EXCEL文件
- C#读取excel数据时,文本格式和数值格式的内容无法正常全部读取
- C#利用Openxml读取Excel数据实例