TSga
TSga

Reputation: 301

How to split worksheets into separate workbooks using c# // how to copy entire worksheet with EPPLus

Trying to tackle a lot of new tools at once, attempting to split each worksheet in a .xlsx file into individual workbooks.

  1. Is there a simple way to copy the entire worksheet (including images/lines) with spreadsheetlight or EPPlus?

  2. If direct copying one worksheet to another won't work, are you able to copy all cell data into an object and paste it over to the cells of another workbook?

Any advice is greatly appreciated!

Edit: Open to alternatives to Spreadsheetlight as well, looking into EPPlus at the moment.

Upvotes: 2

Views: 1650

Answers (1)

Stewart_R
Stewart_R

Reputation: 14485

EPPlus makes this fairly simple.

Something like this should do it:

using (var sourceExcel = new ExcelPackage(new FileInfo("multisheet.xlsx")))
{
    var sheetsToCopy = sourceExcel.Workbook.Worksheets;
    foreach(var sheetToCopy in sheetsToCopy)
    {
        using (var destExcel = new ExcelPackage())
        {
            destExcel.Workbook.Worksheets.Add(sheetToCopy.Name, sheetToCopy);
            destExcel.SaveAs(new FileInfo(sheetToCopy.Name + ".xlsx"));
        }
    }
}

Upvotes: 1

Related Questions