excel标题为sql server数据库表中的主键

时间:2015-03-27 07:10:59

标签: sql-server

问题是我想从excel文件中选择excel表头,并将表头列与表主键匹配,并根据excel标题列更新require字段。 到目前为止,我试图在sql server中导入excel文件,但excel标头不在我的表中导入,所以有任何解决方案从excel文件直接选择标头并更新我的表。 基本上我想要这样 更新表集列=(选择

   SELECT *  FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
  'Excel 8.0;Database=I:\test.xls;HDR=No',)

enter image description here

1 个答案:

答案 0 :(得分:1)

如果您说第一行是SQL表的主键,则无法使用“导入/导出”向导直接导入此Excel。首先使用以下代码在DataTable中导入excel数据:

public class ExcelRow
    {
        List<object> columns;

        public ExcelRow()
        {
            columns = new List<object>();
        }

        internal void AddColumn(object value)
        {
            columns.Add(value);
        }

        public object this[int index]
        {
            get { return columns[index]; }
        }

        public string GetString(int index)
        {
            if (columns[index] is DBNull)
            {
                return null;
            }
            return columns[index].ToString();
        }

        public int Count
        {
            get { return this.columns.Count; }
        }
    }

    public class ExcelProvider:IEnumerable<ExcelRow>
    {
        private string sheet;
        private string filePath;
        private List<ExcelRow> rows;


        public ExcelProvider()
        {
            rows = new List<ExcelRow>();
        }

        public static ExcelProvider Create(string filePath, string sheet)
        {
            ExcelProvider provider = new ExcelProvider();
            provider.sheet = sheet;
            provider.filePath = filePath;
            return provider;
        }

        private void Load()
        {
            string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=    ""Excel 8.0;HDR=YES;""";
            connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=Excel 12.0;";
            rows.Clear();
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                conn.Open();
                using (OleDbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select * from [" + sheet + "$]";
                    using (OleDbDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            ExcelRow newRow = new ExcelRow();
                            for(int count = 0; count < reader.FieldCount; count++) {
                                newRow.AddColumn(reader[count]);
                            }
                            rows.Add(newRow);
                        }
                    }
                }
            }
        }

        public IEnumerator<ExcelRow> GetEnumerator()
        {
            Load();
            return rows.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            Load();
            return rows.GetEnumerator();
        }

    }

public DataTable GetDataTableFromExcel()
        {
            DataTable dt = new DataTable();

            ExcelProvider objProvider = ExcelProvider.Create(@"Your excel file path", "TableExtract");

            dt.Columns.Add("AA");
            /* Add other columns here */

            foreach (ExcelRow row in (from x in objProvider select x))
            {
                dt.Rows.Add(row.GetString(0));
            }

            return dt;
        }

然后处理数据表中的数据,使用第一行作为每次插入的主键将其插入数据库,使用后端存储过程进行插入。