0
点赞
收藏
分享

微信扫一扫

C#连接读取excel并展示相关数据实例

想溜了的蜗牛 2022-01-05 阅读 64
private void button1_Click(object sender, EventArgs e)
{
	string filePath = @"D:ReadExcel\ReadExcel\bin\Debug\Car.xls";
	string strConn1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
	
	//实例化对象
     OleDbConnection conn = new OleDbConnection(strConn1);

     //打开数据库
     conn.Open();

     //读取数据表
     string strExcel = "select *from [Sheet1$]";
            
     //实例化命令:把Sheet和Excel连接起来
     OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, strConn1);

     //实例化数据集
     DataSet ds = new DataSet();
           
     //填充数据集
     myCommand.Fill(ds, "Sheet1");
     
	//引用函数Show来展示excel的数据	
	 Show(ds, i);
}

定义一个函数show用来展示数据,展示在textBox1控件上

public void Show(DataSet ds)
{
	textBox1.Text = Convert.ToString(ds.Tables["Sheet1"].Rows[0]["车牌号"]);
}
举报

相关推荐

0 条评论