If I copy a worksheet using Worksheets.Add(name, original), the PrintArea for the original sheet gets screwed up.
Code:
```
string templatePath = "c:\\docWithPrintAreaSetInFirstWorksheet.xlsx";
string newPath = "c:\\newDoc.xlsx";
var package = new ExcelPackage(new FileInfo(templatePath), true);
var original = package.Workbook.Worksheets[1];
package.Workbook.Worksheets.Add("My new worksheet", original)
package.SaveAs(new FileInfo(newPath));
using (var newPackage = new ExcelPackage(new FileInfo(newPath)))
{
var firstSheet = newPackage.Workbook.Worksheets[1];
// this will crash as Current throws an exception
bool b = firstSheet.PrinterSettings.PrintArea.Current != null;
}
```
I'm using EPPlus v3.1.2. Just a hunch, but it might have something to do with line 245 of ExcelWorksheets.cs:
```
// first delete the attribute from the XML
pageSetup.Attributes.Remove(attr);
```
My workaround is to duplicate the original worksheet twice, then delete the original. I then have two worksheets with working PrintAreas.
Thanks
Comments: Weirdly, this workaround fails if the original worksheet name includes any characters other than [A-Z0-9_] such as a space or period. For example if it is "Foo Bar" (without quotes). In this case, the PrintArea no longer contains any cells in the copied worksheets.
Code:
```
string templatePath = "c:\\docWithPrintAreaSetInFirstWorksheet.xlsx";
string newPath = "c:\\newDoc.xlsx";
var package = new ExcelPackage(new FileInfo(templatePath), true);
var original = package.Workbook.Worksheets[1];
package.Workbook.Worksheets.Add("My new worksheet", original)
package.SaveAs(new FileInfo(newPath));
using (var newPackage = new ExcelPackage(new FileInfo(newPath)))
{
var firstSheet = newPackage.Workbook.Worksheets[1];
// this will crash as Current throws an exception
bool b = firstSheet.PrinterSettings.PrintArea.Current != null;
}
```
I'm using EPPlus v3.1.2. Just a hunch, but it might have something to do with line 245 of ExcelWorksheets.cs:
```
// first delete the attribute from the XML
pageSetup.Attributes.Remove(attr);
```
My workaround is to duplicate the original worksheet twice, then delete the original. I then have two worksheets with working PrintAreas.
Thanks
Comments: Weirdly, this workaround fails if the original worksheet name includes any characters other than [A-Z0-9_] such as a space or period. For example if it is "Foo Bar" (without quotes). In this case, the PrintArea no longer contains any cells in the copied worksheets.