我想以编程方式将工作表克隆到我的Excel模板文件中。使用NPOI库时我可以使用
HSSFWorkbook workbook = new HSSFWorkbook(fs, true);
workbook.CloneSheet(1);
我想知道是否有与EPPlus ExcelWorkbook
相同的东西。我想复制整体ExcelWorksheet
以保留我的格式和值,而不是手动复制每个单元格或单元格范围
答案 0 :(得分:5)
尝试ExcelWorksheets.Copy
方法:
public ExcelWorksheet Copy(ExcelWorkbook workbook, string existingWorksheetName, string newWorksheetName)
{
ExcelWorksheet worksheet = workbook.Worksheets.Copy(existingWorksheetName, newWorksheetName);
return worksheet;
}
答案 1 :(得分:1)
首先,您需要打开两个工作簿,然后可以添加整个工作表: (例如:从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);
}
}