Devpress Excel数据导入

    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);

 

posted on 2016-02-26 22:41  TonyChan3  阅读(377)  评论(0)    收藏  举报

导航