0
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?

Asha B.
  • 83
  • 12

1 Answers1

0

I think you are using your own .NET code to add/update values extracted from worksheet cells (via Aspose.Cells APIs). It would be better if you could export data to Arrays or DataTable from worksheets via Aspose.Cells APIs (see the document for your reference) and then update your Database table (by your own .NET code). I am also not sure what exception you find and the exception is thrown by your own .NET API code or Aspose.Cells API? We appreciate if you could post your issue with a sample project and template file in Aspose.Cells forums, we will check it and help you soon there.

I am a developer evangelist at Aspose.

Community
  • 1
  • 1
Amjad Sahi
  • 1,813
  • 1
  • 10
  • 15