EPPlus clone worksheet
Asked Answered
O

2

6

I want to clone worksheet int my excel template file programatically. When Using NPOI library I can use

    HSSFWorkbook workbook = new HSSFWorkbook(fs, true);
    workbook.CloneSheet(1);

I wonder is there something equivalent to that with EPPlus ExcelWorkbook. I want to copy overall ExcelWorksheet to keep my format and value, not just copy each cell or range of cell manually

Osteoclast answered 1/2, 2018 at 9:37 Comment(0)
A
13

Try the ExcelWorksheets.Copy method:

public ExcelWorksheet Copy(ExcelWorkbook workbook, string existingWorksheetName, string newWorksheetName)
{
    ExcelWorksheet worksheet = workbook.Worksheets.Copy(existingWorksheetName, newWorksheetName);
    return worksheet;
}
Alonso answered 1/2, 2018 at 9:49 Comment(0)
N
4

first u need to open both of workbooks and then u can add the whole worksheet: (example: copy from workbook_1 -> workbook_2)

FileInfo existingFile1 = new FileInfo(path_of_workbook_1);
using (ExcelPackage package = new ExcelPackage(existingFile1 ))
{
     FileInfo existingFile = new FileInfo(path_of_workbook_2);
     using (ExcelPackage package_0 = new ExcelPackage(existingFile))
     {
         ExcelWorksheet worksheet0 = package_0.Workbook.Worksheets["Original Sheet"];
         package.Workbook.Worksheets.Add("Copied Sheet", worksheet0);
     }
}
Nicolettenicoli answered 2/9, 2020 at 17:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.