导入Excel数据(使用LinqToExcel)仅适用于工作表是否打开C#MVC

时间:2016-06-10 06:56:07

标签: c# asp.net-mvc-5 linq-to-excel excel-import

我正在将excel数据导入到我的数据库中,该数据非常正常,但仅当电子表格处于打开状态时才能正常工作。如果它没有打开,则会出现“预期表格格式不正确”的错误,为什么会有任何想法?在此先感谢您的帮助!

[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?

1 个答案:

答案 0 :(得分:0)

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