public List<Project> ImportProjectsFromExcel(string path)
{
LoadOptions loadOptionForXlsx = new LoadOptions(LoadFormat.Xlsx);
Workbook workbook = new Workbook(path, loadOptionForXlsx);
char column = 'A';
var project = new Project();
var projects = new List<Project>();
var rowcount = workbook.Worksheets["Sheet1"].Cells.MaxDataRow;
for (int index = 2; index <= rowcount + 1; )
{
var cell = workbook.Worksheets["Sheet1"].Cells[column + index.ToString()];
switch (column)
{
case 'A':
project.ProjectName = cell.StringValue;
++column;
break;
case 'B':
project.ProjectCode = cell.StringValue;
++column;
break;
case 'C':
project.Description = cell.StringValue;
++column;
break;
case 'D':
project.EngagementManagerId = cell.IntValue;
++column;
break;
case 'E':
project.ProjectManagerId = cell.IntValue;
++column;
break;
case 'F':
project.AdditionalNotes = cell.StringValue;
++column;
try
{
AddProject(project);
projects.Add(project);
}
catch (Exception dbEx)
{
var message = dbEx.Message;
dbEx = null;
}
project = new Project();
++index;
column = 'A';
break;
}
}
return projects;
}
Question: Here i'm reading the data from Excel file and inserting it in database,and projectId is a primary key for project.But if ProjectId is repeated in one cell then exception occurs but for next cell if ProjectId is 'unique' then also it is going inside catch and giving exception for ProjectId. How to resolve this?