如何从ccel中的数据库表列中的excel中获取唯一的列数据

时间:2015-05-19 16:46:16

标签: c# asp.net .net sql-server excel

我有一个150列的excel文件,其中包含特定的列标题。在我的数据库中,有一个表' ExcelImport '列 ExcelHead (包含与excel列标题同名的列标题数据)。

create table ExcelImport( id int primary key,ExcelHead varchar(max))

insert into ExcelImport values(1,'Role'),(2,'Manager')

此表将根据其他规则获取数据。我的要求是我需要从ExcelImport表中的excel中获取唯一的列数据并插入到另一个临时表中。从示例中我只需要获取'角色'和经理' excel列,需要插入临时表。

注意:每次ExcelImport表数据都会被截断,并且ExcelHead的新值将被插入。

任何人都可以建议做同样的编码吗?

2 个答案:

答案 0 :(得分:1)

首先,您必须从excel文件中读取列标题。尝试查看here以了解如何从excel文件中读取列标题。列标题通常是excel文件的第一行,所以如果你只读取列标题的excel文件的第一行就可以了。我通常会使用Dictionary来跟踪列标题和列索引。然后,您将需要使用sql查询ExcelImport表以获取您想要用作过滤器的所有列名。我将交叉引用两个数据(ExcelImport和Dictionary)来获取需要获取的excel文件的所有行索引。然后我将迭代所有行以获取excel文件的所有数据。最后我将删除所有来自ExcelImport的数据并将新数据插入ExcelImport表。你可以看下面的伪代码。

    public Dictionary<string,int> GetDictionary(string fullExcelPath)
    {
           //Dictionary<Column Header,Column Index>

           Dictionary<string,int> columns=new  Dictionary<string,int>();

             ExcelWorkbook excelWorkbook = ExcelWorkbook.ReadXLSX(fullExcelPath);

             ExcelWorksheet excelWorkSheet = excelWorkbook.Worksheets[0];

             DataTable dataTable = excelWorkSheet.WriteToDataTable();

             DataTable columns=dataTable.Rows[0];

             //iterate to get the column header

             for(int i=0;;i++)
             {
                string header=(string )row[i];

                if(!string.IsNullOrWhiteSpace(header))
                {
                       columns.Add(header,i);
                }
                else
                {
                   break;
                }
             }
           return columns;

    }

  public void DoWork()
  {
      Dictionary<string,int> columnsFromExcel=GetDictionary(excelPath);

      List<string> columnToFilter=GetFromDatabase();

      int[] columnIndex=CrossReferenceData(columnsFromExcel,columnToFilter);

      //column index=index we wanted to get data from
      //GetDataFromExcel =basically same as GetDictionary but it read from //second row instead of first row and used columnIndex to get data from //dataTable (look GetDictionary method).
      List<string> dataFromExcel=GetDataFromExcel(columnIndex);

      DeleteExcelImportTable();

      // dynamically created your insert sql

      string insertSQL=GetInsertSQL(dataFromExcel);

      InsertExcelImportTable(insertSQL);

  }

答案 1 :(得分:0)

            DataSet ExcelDataset = DBcom.GetDataset();
            //add logic for getting excel imported data in dataset format.
            string[] columnsNames = new string[100]; int loop = 0;

            //Comparing the Imported Excel Column With Database Table Column
            foreach (DataColumn column in ExcelDataset.Tables[0].Columns)
            {
                string clnameExcel = column.ColumnName;                
                int Exist = 0;
                foreach (DataTable table in DataBaseDataset.Tables)
                {
                    foreach (DataRow dr in table.Rows)
                    {
                        string ColnamesDB = dr["DBColumn"].ToString();//DBColumn is the name of database table column name


                        if (CompclnameExcel == CompColnamesDB)
                        {
                            Exist = 1;
                            break;
                        }

                    }
                }
                if (Exist == 0)
                {                    
                    columnsNames[loop] = clnameExcel;
                    loop++;
                }
            }
            //Deleting Imported Excel Columns which is not there in Database
            foreach (string cNames in columnsNames)
            {
                if (!string.IsNullOrEmpty(cNames))
                {
                    ExcelDataset.Tables[0].Columns.Remove(cNames);
                }
            }