Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EPPlus clone worksheet

Tags:

c#

epplus

npoi

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

like image 384
Bùi Đức Khánh Avatar asked Feb 01 '18 09:02

Bùi Đức Khánh


2 Answers

Try the ExcelWorksheets.Copy method:

public ExcelWorksheet Copy(ExcelWorkbook workbook, string existingWorksheetName, string newWorksheetName)
{
    ExcelWorksheet worksheet = workbook.Worksheets.Copy(existingWorksheetName, newWorksheetName);
    return worksheet;
}
like image 59
SpruceMoose Avatar answered Sep 19 '22 12:09

SpruceMoose


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);
     }
}
like image 31
TK_18 Avatar answered Sep 19 '22 12:09

TK_18