EPPlus.DataExtractor icon indicating copy to clipboard operation
EPPlus.DataExtractor copied to clipboard

Extract data with Collections explanation

Open baffourt opened this issue 5 years ago • 1 comments

This is an excellent project, thank you @ipvalverde for the time. I have a quick question How do i extract this data into this ScoresPoco

public class ScoresPoco
{
    public int ApplicantID { get; set; }
    public List<ScoreData> Scores { get; set; }
}

public class ScoreData
{
    public int SubjectID { get; set; }
    public double Score { get; set; }
}
image Subject ID is in Row 2 hidden for both C & D columns

I am extracting with this

var data = worksheet.Extract<ScoresPoco>()
.WithProperty(p => p.ApplicantID, "B")
.WithCollectionProperty(p => p.Scores,
                        item => item.SubjectID, 1,
                        item => item.Score, "C", "E")
.GetData(model.DataRowStart, row => worksheet.Cells[row, model.DataColStart].Value != null)
.ToList();

This only returns the ApplicantID and Scores correctly but the subjectID is always 0. There can also be more than 2 or Subjects, but all the IDs will be in the Row 2. When debugging, I can see all that data in the worksheet but a way to add it to the SubjectID is my question.

baffourt avatar Jan 17 '21 04:01 baffourt

I was able to solve this after asking the question.

using ExcelPackage excelPackage = new ExcelPackage(model.FileData);
            var worksheet = excelPackage.Workbook.Worksheets[0];
            var data = worksheet.Extract<ScoresPoco>()
                .WithProperty(p => p.ApplicantID, "B")
                    .WithCollectionProperty(p => p.Scores,
                        item => item.SubjectID, 2,
                        item => item.Score, "C", "F")
                .GetData(model.DataRowStart, row => worksheet.Cells[row, model.DataColStart].Value != null)
                .ToList();

Setting this line .WithCollectionProperty(p => p.Scores, item => item.SubjectID, 2, instead of 1 was able to present the SubjectIDs correctly. So am only left with If the subjects in the excel sheet are more than 2. For now I handle it by specifying a higher number of cells (C to F), it may be C to J in the future

baffourt avatar Jan 17 '21 05:01 baffourt