Excel Reader C#读取额外的行

时间:2015-04-20 18:27:08

标签: c# import-from-excel

 private bool CompetitorParseFile(Stream file, int competitorId)
    {
        var excelReader = ExcelReaderFactory.CreateOpenXmlReader(file);

        var accounts = new List<CreateEditAccountModel>();
        var _line = 1;

        while (excelReader.Read())
        {
            if (_line == 1)
            {
                _line++;
                continue;
            }
            var accountName = excelReader.GetString(0);
            if (string.IsNullOrWhiteSpace(accountName))
            {
                throw new Exception("The Account Name shouldn't be null on line " + _line + " of the Excel Sheet");
            }

            var subsidiaryName = excelReader.GetString(1);

            if (string.IsNullOrWhiteSpace(subsidiaryName))
            {
                throw new Exception("The Subsidiary Name shouldn't be null on line " + _line + " of the Excel Sheet");
            }

            var subsidiary = Db.Companies.FirstOrDefault(i => i.CompanyCodeName == subsidiaryName);
            if (subsidiary == null)
            {
                throw new Exception("The subsidiary couldn't be found");
            }

            var subsidiaryId = subsidiary.ID;


            accounts.Add(new CreateEditAccountModel
            {
                AccountName = excelReader.GetString(0) == null ? null : excelReader.GetString(0).Trim(),
                SubsidiaryId = subsidiaryId,
                Keywords = excelReader.GetString(2) == null ? null : excelReader.GetString(2).Trim()
            });
            _line++;
        }
        excelReader.Close();


        if (competitorId == 0)
        {
            throw new Exception("The Competitor ID name shouldn't be null");
        }



        InitializeUserImporting(ImportOrigin.EntityWithCompetitors);
        ImportCompetitorFromExcel(new ImportEntities { competitorId = competitorId, Entities = accounts });

        return true;

    }

我的Excel阅读器在一行中阅读太多并抛出异常“帐户名称不应该为空” excel阅读器跳过第一行,因为它们是列名。 任何帮助表示excel文件是.xlsx文件。

0 个答案:

没有答案