C# Excel阅读器C在额外行中读取
我的excel阅读器在一行中读取的数据太多,并引发异常。帐户名不应为空 excel阅读器跳过第一行,因为这是列名。C# Excel阅读器C在额外行中读取,c#,import-from-excel,C#,Import From Excel,我的excel阅读器在一行中读取的数据太多,并引发异常。帐户名不应为空 excel阅读器跳过第一行,因为这是列名。 excel文件的任何帮助都是.xlsx文件。我发现excel文件存在问题,文件末尾似乎总是有一个空行。我会检查所有的值是否为NULL,考虑到文件的结尾,或者至少跳过它。1。从EXCEL中选择并删除或清除内容file@SaagarEliasJacky我无法清除内容,因为很多人都在使用它。因此,如果一个人的电子表格中有一个空单元格,它将抛出错误。@user3922757另一个选项是在最
excel文件的任何帮助都是.xlsx文件。我发现excel文件存在问题,文件末尾似乎总是有一个空行。我会检查所有的值是否为NULL,考虑到文件的结尾,或者至少跳过它。1。从EXCEL中选择并删除或清除内容file@SaagarEliasJacky我无法清除内容,因为很多人都在使用它。因此,如果一个人的电子表格中有一个空单元格,它将抛出错误。@user3922757另一个选项是在最后一行放置某种标记。我相信可能有空帐号的行,所以你不能检查空并终止,因为这是结束…我刚刚得到一个屏幕截图,那个人上传了错误的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;
}