簡體   English   中英

使用EPPlus驗證Excel(電子表格)文件

[英]Validate Excel(Spreadsheet) File Using EPPlus

場景:有一個包含用戶數據的Excel工作表。 需要在插入(如果新)或更新(如果存在)數據庫中的任何用戶數據之前驗證excel工作表。 如果任何單元格的數據不正確或數據為空,則該單元格將以紅色突出顯示並帶有注釋以解釋錯誤。
例如:電子郵件格式不正確

我想將其發布為解決方案,以便對使用EPPlus驗證excel表格有困難的任何人提供幫助。
EPPlus版本:4.5.2.1

以下是Excel工作表結構。 Excel表格結構

以下是驗證excel工作表的完整代碼。

    [HttpPost]
    public ActionResult BulkUserUpload(HttpPostedFileBase uploadFile)
    {
        string          tempFileName            = string.Empty;
        string          errorMessage            = string.Empty;

        if (uploadFile != null && uploadFile.ContentLength > 0)
        {
            //ExcelExtension contains array of excel extension types
            if ( Config.ExcelExtension.Any(p => p.Trim() == Path.GetExtension(uploadFile.FileName)) )
            {
                //save the uploaded excel file to temp location
                SaveExcelTemp(uploadFile, out tempFileName);
                //validate the excel sheet
                if (ValidateExcel(tempFileName, out errorMessage))
                {
                    //save the data
                    SaveExcelDataToDatabase(tempFileName);
                    //spreadsheet is valid, show success message or any logic here
                }
                else
                {
                    //set error message to shown in front end
                    ViewBag.ErrorMessage        = errorMessage;
                }
            }
            else
            {
                //excel sheet is not uploaded, show error message
            }                                
        }
        else
        {
            //file is not uploaded, show error message
        }

        return View();
    }


    private bool ValidateExcel(string tempFileName, out string errorMessage)
    {
        bool                result              = true;
        string              error               = string.Empty;
        string              filePath            = GetTempFilePath(tempFileName);
        FileInfo            fileInfo            = new FileInfo(filePath);
        ExcelPackage        excelPackage        = new ExcelPackage(fileInfo);
        ExcelWorksheet      worksheet           = excelPackage.Workbook.Worksheets.First();
        int                 totalRows           = worksheet.Dimension == null ? -1 : worksheet.Dimension.End.Row; //worksheet total rows
        int                 totalCols           = worksheet.Dimension == null ? -1 : worksheet.Dimension.End.Column; // total columns

        //check spread sheet has rows (empty spreadsheet uploaded)
        if (totalRows == -1)
        {
            result                              = false;
            error                               = "Empty spread sheet uploaded.";
        }
        //check rows are more than or equal 2 (spread sheet has only header row)
        else if (totalRows < 2)
        {
            result                              = false;
            error                               = "Spread sheet does not contain any data";
        }
        //check total columns equal to headers defined (less columns)
        else if (totalCols > 0 && totalCols != GetColumnHeaders().Count)
        {
            result                              = false;
            error                               = "Spread sheet column header value mismatch.";
        }

        if (result)
        {
            //validate header columns
            result                              &= ValidateColumns(worksheet, totalCols);

            if (result)
            {
                //validate data rows, skip the header row (data rows start from 2)
                result                          &= ValidateRows(worksheet, totalRows, totalCols);
            }

            if (!result)
            {
                error                           = "There are some errors in the uploaded file. Please correct them and upload again.";
            }
        }

        errorMessage                            = error;
        worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();
        excelPackage.Save();

        return result;
    }

以下是驗證方法

    private bool SetError(ExcelRange cell, string errorComment)
    {           
        var         fill        = cell.Style.Fill;
        fill.PatternType        = OfficeOpenXml.Style.ExcelFillStyle.Solid;
        fill.BackgroundColor.SetColor(System.Drawing.Color.Red);
        cell.AddComment(errorComment, "");

        return false;
    }

    private bool ValidateHeaderColumns(ExcelWorksheet worksheet, int totlaColumns)
    {
        bool                result                  = true;
        List<string>        listColumnHeaders       = GetColumnHeaders();

        for (int i = 1; i < totlaColumns; i++)
        {
            var             cell                    = worksheet.Cells[1, i]; //header columns are in first row

            if (cell.Value != null)
            {
                //column header has a value
                if (!listColumnHeaders.Contains(cell.Value.ToString()))
                {
                    result                          &= SetError(cell, "Invalid header. Please correct.");
                }
            }
            else
            {
                //empty header
                result                              &= SetError(cell, "Empty header. Remove the column.");
            }
        }

        return result;
    }

    private bool ValidateRows(ExcelWorksheet worksheet, int totalRows, int totalCols)
    {
        bool        result              = true;

        for (int i = 2; i <= totalRows; i++) //data rows start from 2`
        {
            for (int j = 1; j <= totalCols; j++)
            {
                var         cell        = worksheet.Cells[i, j];

                switch (j)
                {
                    //email address
                    case 1:
                        {
                            result      &= ValidateEmailAddress(cell, "Email address");
                            break;
                        }
                    //first name
                    case 2:
                        {
                            result      &= ValidateText(cell, "First name");
                            break;
                        }
                    //last name
                    case 3:
                        {
                            result      &= ValidateText(cell, "Last name");
                            break;
                        }
                    //address line 1
                    case 4:
                        {
                            result      &= ValidateText(cell, "Address line 1");
                            break;
                        }
                    //address line 2
                    case 5:
                        {
                            result      &= ValidateText(cell, "Address line 2");
                            break;
                        }
                    //city
                    case 6:
                        {
                            result      &= ValidateText(cell, "City");
                            break;
                        }
                    //telephone number
                    case 7:
                        {
                            result      &= ValidateText(cell, "Telephone number");
                            break;
                        }
                    //mobile number
                    case 8:
                        {
                            result      &= ValidateText(cell, "Mobile number");
                            break;
                        }
                    //job title
                    case 9:
                        {
                            result      &= ValidateJobTitle(cell, "Job title");
                            break;
                        }
                    //salary
                    case 10:
                        {
                            result      &= ValidateNumber(cell, "Salary");
                            break;
                        }
                    //role
                    case 11:
                        {
                            result      &= ValidateRole(cell, "Role");
                            break;
                        }
                    //branch
                    case 12:
                        {
                            result      &= ValidateBranch(cell, "Branch");
                            break;
                        }
                    //joined date
                    case 13:
                        {
                            result      &= ValidateDate(cell, "Joined date");
                            break;
                        }
                }                   
            }
        }

        return result;
    }

    private bool ValidateEmailAddress(ExcelRange cell, string columnName)
    {
        bool        result      = true;
        result                  = ValidateText(cell, columnName); //validate if empty or not

        if (result)
        {
            if (!ValidateEmail(cell.Value.ToString())) //ValidateEmail => true, if email format is correct
            {
                result          = SetError(cell, "Email address format is invalid.");
            }
            else if (cell.Value.ToString().Length > 150)
            {
                result          = SetError(cell, "Email address too long. Max characters 150.");
            }
        }

        return result;
    }

    private bool ValidateText(ExcelRange cell, string columnName)
    {
        bool        result      = true;
        string      error       = string.Format("{0} is empty", columnName);

        if (cell.Value != null)
        {
            //check if cell value has a value
            if (string.IsNullOrWhiteSpace(cell.Value.ToString()))
            {
                result          = SetError(cell, error);
            }
        }
        else
        {
            result              = SetError(cell, error);
        }

        return result;
    }


    private bool ValidateNumber(ExcelRange cell, string columnName)
    {
        bool        result      = true;
        double      value       = 0.0;
        string      error       = string.Format("{0} format is incorrect.", columnName);
        result                  = ValidateText(cell, columnName);

        if (result)
        {
            if (!double.TryParse(cell.Value.ToString(), out value))
            {
                result          = SetError(cell, error);
            }
        }

        return result;
    }

    private bool ValidateDate(ExcelRange cell, string columnName)
    {
        bool            result      = true;
        DateTime        date        = DateTime.MinValue;
        string          error       = string.Format("{0} format is incorrect.", columnName);
        result                      = ValidateText(cell, columnName);

        if (result)
        {
            if (!DateTime.TryParse(cell.Value.ToString(), out date))
            {
                result              = SetError(cell, error);
            }
        }

        return result;
    }

    private bool ValidateJobTitle(ExcelRange cell, string columnName)
    {
        bool                result              = true;
        string              error               = "Job title does not exist.";
        List<JobTitle>      listJobTitle        = JobTitle.GetJobTitles((int)JobTitle.JobTitleStatus.Active);
        result                                  = ValidateText(cell, columnName);

        if (result)
        {
            if (!listJobTitle.Any(x => x.Name.ToLowerInvariant() == cell.Value.ToString().ToLowerInvariant()))
            {
                result                          = SetError(cell, error);
            }
        }

        return result;
    }

方法ValidateBranch()和ValidateRole()的實現方式與ValidateJobTitle()方法相同。

以下是經過驗證的帶有錯誤的Excel工作表。 Excel表格錯誤

希望這對任何人都有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM