0
Sr No   PS_Name    PS_Name Area
  1       ABC       ABC-1
                    ABC-2
                    ABC-3
  2       ABD       ABD-1
                    ABD-2
                    ABD-3


string filePath = null;
        foreach (string inputTagName in Request.Files)
        {
            HttpPostedFileBase Infile = Request.Files[inputTagName];
            if (Infile.ContentLength > 0 && (Path.GetExtension(Infile.FileName) == ".xls" || Path.GetExtension(Infile.FileName) == ".xlsx" || Path.GetExtension(Infile.FileName) == ".xlsm"))
            {
                filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                              Path.GetFileName(Infile.FileName));
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }
                Infile.SaveAs(filePath);
                //Infile.SaveAs(filePath); 
            }

            if (filePath != null)
            {
                System.Data.OleDb.OleDbConnection oconn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath.ToString() + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";");
                oconn.Open();

                try
                {
                    if (oconn.State == System.Data.ConnectionState.Closed)
                        oconn.Open();
                }
                catch (Exception ex)
                {
                    // MessageBox.Show(ex.Message);
                }

                dynamic myTableName = oconn.GetSchema("Tables").Rows[0]["TABLE_NAME"];                    
                OleDbCommand ocmd = new OleDbCommand("select * from [" + myTableName + "]", oconn);


                OleDbDataReader odr = ocmd.ExecuteReader();
                if (odr.HasRows)
                {
                    while (odr.Read())
                    {                            
                        var c = odr[0];
                        var t = odr[1];
                        var de = odr[2];
                        //model.PS_NAME = odr[1].ToString().Trim();                            
                    }
                }
            }
        }

我想导入 Excel 数据并将其保存在我的数据库 (SQL Server 2008) 中。我在excel中使用合并单元格选项。我在控制器中使用上面的代码。我得到了前两列,但是在获取合并单元格数据时遇到了问题。

4

0 回答 0