C# Winform读取Excel表格

private void btnCheck_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + txtPath.Text + ";" + "Extended Properties=Excel 8.0;";
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            string strExcel = string.Empty;
            OleDbDataAdapter myCommand = null;
            int index = txtPath.Text.LastIndexOf("\\");
            string filename = txtPath.Text.Substring(index + 1);
            int index2 = filename.IndexOf(".");
            string excelname = filename.Substring(0, index2);
            strExcel = string.Format("select * from [{0}$]", excelname); //excelname为excel中工作薄
            myCommand = new OleDbDataAdapter(strExcel, strConn);
            myCommand.Fill(ds, excelname);
            conn.Close();
}

 

posted @ 2013-01-25 11:47  蜗牛、、、  阅读(444)  评论(0编辑  收藏  举报