0

我正在做一个项目,我从一个excel文件中读取多个字段并将它们保存在db中。如果用户输入错误的值,如何在页面上显示多个错误消息。Aspose.cells 用于读取数据。我的代码是

    public List<Data> ImportFromExcel(Stream bytes, out bool isFine)
    {
        isFine = true;
        DateTime DOJ;

        List<Data> list = new List<Data>();
        DataTable dt = new DataTable();

        Workbook workBook = new Workbook();
        workBook.Open(bytes);

        Worksheet workSheet = workBook.Worksheets[0];
        try
        {
            dt = workSheet.Cells.ExportDataTable(0, 0, workSheet.Cells.MaxRow + 1, workSheet.Cells.MaxColumn + 1, true);

        }
        catch (Exception ex)
        {
            isFine = false;
            ShowMessage("Your file has some invalid formats of data. Please review it and try again.", MessageType.Error, true);
            return null;
        }

        try
        {
            int i = 1;

            foreach (DataRow reader in dt.Rows)
            {
                if (reader["LetterId"].ToString().Length > 75)
                {
                    isFine = false;
                    ShowMessage("In Row Number " + i + " Letter Id cannot exceed 75 characters.", MessageType.Error, true);
                    return null;
                }
                if (reader["Subject"].ToString().Length > 75)
                {
                    isFine = false;
                    ShowMessage("In Row Number " + i + " Subject cannot exceed 75 characters.", MessageType.Error, true);
                    return null;
                }
                 .
                 .
                 .

Show message 方法仅显示单个错误消息。

4

1 回答 1

0

您可以创建一个空的字符串列表,然后每次遇到错误时只需将其添加到列表中。然后,一旦您的列表中有所有错误,您就可以这样做:

        string allErrors = string.Empty;
        foreach (string err in errorList)
        {
            allErrors += err + "<br />";
        }
        if (allErrors != string.Empty)
        {
            ShowMessage(allErrors);
        }
于 2013-06-19T08:36:13.483 回答