码迷,mamicode.com
首页 > 其他好文 > 详细

Devpress Excel数据导入

时间:2016-02-26 23:26:11      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

    class ExcelDataBaseHelper
    {
        public static object OpenFile(string fileName, bool hasHeaders)
        {
            var fullFileName = fileName;
            string HDR = hasHeaders ? "Yes" : "No";
            string connectionString;

            if (!File.Exists(fullFileName))
            {
                System.Windows.Forms.MessageBox.Show("File not found");
                return null;
            }

            if (fullFileName.Substring(fullFileName.LastIndexOf(.)).ToLower() == ".xlsx")
                connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fullFileName + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"");
            else
                connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fullFileName + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=0\"";
            var adapter = new OleDbDataAdapter("select * from [Sheet1$]", connectionString);
            var ds = new DataSet();
            string tableName = "excelData";
            adapter.Fill(ds, tableName);
            DataTable data = ds.Tables[tableName];
            return data;
        }
    }

Devpress GridControl绑定DataTable数据

this.gridControl1.DataSource = ExcelDataBaseHelper.OpenFile(fp,true);

 

Devpress Excel数据导入

标签:

原文地址:http://www.cnblogs.com/TonyChan3/p/5222007.html

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