简体   繁体   中英

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.

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"));
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM