码迷,mamicode.com
首页 > Windows程序 > 详细

C#读取Excel设置(亲测可用)

时间:2015-01-23 19:55:12      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

 OpenFileDialog openFD = new OpenFileDialog();
            openFD.FileName = "";
            openFD.Filter = "xls|*.xls|xlsx|*.xlsx";
            openFD.Title = "选择更新文件";
            openFD.Multiselect = false;
            if (openFD.ShowDialog() == DialogResult.OK)
            {
                this.tbSelectFile.Text = openFD.FileName;
                //读取Excel
                pDataSet.Clear();
//成功的关键在于下面这句话,网上有很多不同版本,但这个是亲测可用的。
string strConn = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + openFD .FileName + ";" + "Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(strConn); conn.Open(); string strExcel = ""; OleDbDataAdapter myCommand = null; strExcel = "select * from [sheet1$]"; myCommand = new OleDbDataAdapter(strExcel, strConn); myCommand.Fill(pDataSet , "table1"); conn.Close(); }

 

C#读取Excel设置(亲测可用)

标签:

原文地址:http://www.cnblogs.com/pengdage110/p/4244777.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!