导入 Excel 数据(使用 LinqToExcel)仅在工作表打开时有效 C# MVC

Importing Excel Data(using LinqToExcel) Only works If worksheet is open C# MVC

我正在将 excel 数据导入我的数据库,它工作得很好,但前提是电子表格是打开的。如果它没有打开,它会给出 "Expected table not in correct format" 的错误,有什么想法吗?在此先感谢您的帮助!

[HttpPost]
    public ActionResult ImportExcel(PipelineDetails model, HttpPostedFileBase FileUpload, int ProjectID)
    {
        string Result = "";

        if (FileUpload != null)
        {
            if (FileUpload.ContentType == "application/vnd.ms-excel" || FileUpload.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                string filename = FileUpload.FileName;
                string pathToExcelFile = filename;

                var excelFile = new ExcelQueryFactory();
                excelFile.FileName = pathToExcelFile;

                excelFile.AddMapping<PipelineDetails>(x => x.Accumulated_Length, "Accumulated Length");
                excelFile.AddMapping<PipelineDetails>(x => x.Elevation, "Elevation");
                excelFile.AddMapping<PipelineDetails>(x => x.Pipe_Outside_Diameter, "Pipe Outside Diameter");
                excelFile.AddMapping<PipelineDetails>(x => x.Wall_Thickness, "Wall Thickness");
                excelFile.AddMapping<PipelineDetails>(x => x.Control_Point_Description, "Control Point Description");
                excelFile.AddMapping<PipelineDetails>(x => x.Control_Point_Size, "Control Point Size");

                var pipelineData = from PLD in excelFile.Worksheet<PipelineDetails>() select PLD;

                try
                {
                    foreach (var a in pipelineData)
                    {
                        if (a.Accumulated_Length == null || a.Elevation == null || a.Pipe_Outside_Diameter == null ||
                                a.Wall_Thickness == null)
                        {
                            Result = "There is a problem with the Import File. Please check the file format or data.";
                            return RedirectToAction("Index", "Pipeline", new { id = ProjectID, result = Result });
                        }
                    }
                }
                catch (Exception ex)
                {
                    Result = "There is a problem with the Import File. Please check the file format or data.";
                    return RedirectToAction("Index", "Pipeline", new { id = ProjectID, result = Result });
                }


                ProjectManager PM = new ProjectManager();
                PM.DeleteALLPipeLine_forProject(ProjectID);

                foreach (var a in pipelineData)
                {
                    try
                    {
                        if (a.Accumulated_Length.ToString() != "" && a.Elevation.ToString() != "" && a.Pipe_Outside_Diameter.ToString() != "" && 
                            a.Wall_Thickness.ToString() != "")
                        {
                            using (RexusTradingEntities RTE = new RexusTradingEntities())
                            {
                                PipelineData PD = new PipelineData();
                                PD.Accumulated_Length = Convert.ToDecimal(a.Accumulated_Length);
                                PD.Elevation = Convert.ToDecimal(a.Elevation);
                                PD.Pipe_Outside_Diameter = Convert.ToDecimal(a.Pipe_Outside_Diameter);
                                PD.Wall_Thickness = Convert.ToDecimal(a.Wall_Thickness);
                                PD.Control_Point_Description = a.Control_Point_Description;
                                PD.Control_Point_Size = a.Control_Point_Size;
                                PD.fkiProjectID = ProjectID;
                                PD.CreateDateTimeStamp = DateTime.Now;

                                RTE.PipelineDatas.Add(PD);
                                RTE.SaveChanges();
                            }
                        }
                    }

                    catch (DbEntityValidationException ex)
                    {
                        foreach (var entityValidationErrors in ex.EntityValidationErrors)
                        {

                            foreach (var validationError in entityValidationErrors.ValidationErrors)
                            {

                                Response.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);

                            }

                        }
                    }
                }

                //Adding the Node Numbers in sequencial order
                PM.UpdatePipelineNodeNumbers(ProjectID, model);

                Result = "Import Success";

                return RedirectToAction("Index", "Pipeline", new { id = ProjectID, result = Result });
            }
            else
            {
                Result = "Only Excel file format is allowed.";
                return RedirectToAction("Index", "Pipeline", new { id = ProjectID, result = Result });
            }
        }
        else
        {
            Result = "No Import File Selected.";
            return RedirectToAction("Index", "Pipeline", new { id = ProjectID, result = Result });
        }
    }

编辑: 我也尝试添加以下内容,但仍然出现错误:

if (FileUpload.FileName.EndsWith("xlsx"))
                {
                    excelFile.DatabaseEngine = LinqToExcel.Domain.DatabaseEngine.Ace;
                }

if (FileUpload.FileName.EndsWith("xls"))
                {
                    excelFile.DatabaseEngine = LinqToExcel.Domain.DatabaseEngine.Jet;
                }

编辑: 我刚刚将我的 .xlsx 文件保存为 .xls 并且导入完美!?为什么不使用 .xlsx?

不幸的是,我需要找到一个解决方案,但由于我没有得到任何帮助(即回复和其他帖子),我不得不做出决定并放弃上述内容,只允许导入 csv 文件。