Update: it turns out it was the SaveToStream()
method which saves all sheets instead of the the one, that was the confusing part which lead me to believe the sheet being returned wasn't right
Given the documentation on the CopyAfter() method, the return value is the newly copied sheet
So I wrote the first line of code, now commented out (see below).
The problem is, when I use the sheetWithValues
variable from the return of the method, the code below it, does not work (ie The formulas are not replaced with values)
When I comment out the use of the returned value from CopyAfter() and substitute it with workbook.Worksheets[0]
as done in the code below, it does work (ie the formulas in the sheet are replaced with values)
Given that experiment, it doesn't seem possible that the return value of CopyAfter() is actually returning the newly created sheet, it's returning something else.
So why does the return value of CopyAfter() not return what is obviously expected here? I really want to use the return value, because the alternative code below seems brittle and is unlikely to continue working if sheets are re-arranged.
The goal of the code BTW is simply to copy the entire sheet, with values, before exporting... shortly after this code, I take the copied sheet and stream it to PDF.
//var sheetWithValues = workbook.ActiveWorksheet.CopyAfter(workbook.ActiveWorksheet);
workbook.ActiveWorksheet.CopyAfter(workbook.ActiveWorksheet);
var sheetWithValues = workbook.Worksheets[0]; // no idea why this works and the return value does not
sheetWithValues.UsedRange.Value = sheetWithValues.UsedRange.Value