JanKallman / EPPlus

Create advanced Excel spreadsheets using .NET
3.76k stars 1.18k forks source link

Add ability to add rows in their entirety rather than editing cell by cell. #547

Open AdamHess opened 5 years ago

AdamHess commented 5 years ago

Currently we can access rows by using Row() but no way to set the entire data for a specific row.

At best we can just "InsertRow" which adds an empty row, but no means to build a row outside of the spreadsheet and add it as a whole.

This could lead to less verbose code, via an improved API, to add a bunch of data like this:

worksheet.AddRow(new ExcellRow()
  {
   Values = new List<string>() {
      "Column1 Value",
      "Column2 Value",
      "Column3 Value"
  }
})
aricept commented 5 years ago

This is basically what the .LoadFrom methods do.

LoadFromText() has an overload that takes a comma-delimited set of text and makes a row for each line - designed to read data from a CSV, but will convert the string "Column1 Value, Column2 Value, Column3 Value" into a row in a spreadsheet.

LoadFromCollections() works great with strongly typed collections, say a list of class Employee.

var xl = new ExcelPackage();
var empList = new List<Employee>
{
    new Employee
    {
        Name = "Adam Smith",
        Age = 33,
        Hobbies = "Economics"
    },
    new Employee
    {
        Name = "Nathan Hale"
        Age = 35,
        Hobbies = "Espionage"
    }
};
xl.Workbook.Worksheets["Employees"].Cells["A2"].LoadFromCollection(empList);

That will add two rows into the spreadsheet, starting at A2.