Copying Header/Footer of existing Spreadsheet

Hello, im trying to convert a Program that uses Infragistics to GemBox, The Program loads a .xlsx Template File into a Workbook, This Template has Header/Footer and Orientation defined in it and I have to “copy” these settings to all other worksheets im appending to the workbook.

The Infragistics Code:

excel.Worksheets[excel.Worksheets.Count - 1].PrintOptions.Header = excel.Worksheets[0].PrintOptions.Header;
excel.Worksheets[excel.Worksheets.Count - 1].PrintOptions.Footer = excel.Worksheets[0].PrintOptions.Footer;
excel.Worksheets[excel.Worksheets.Count - 1].PrintOptions.Orientation = Infragistics.Documents.Excel.Orientation.Landscape;

This Code copies the Header/Footer and Orientation Settings for Excel from my Template File to all new Worksheets, my Question is how to do the exact same thing using GemBox.Spreadsheet.

I hope someone can help me with this. Thanks. :slightly_smiling_face:

Hi Erbil,

Try this:

var excel = ExcelFile.Load("input.xlsx");
var sourceSheet = excel.Worksheets[0];
var destinationSheet = excel.Worksheets[excel.Worksheets.Count - 1];

destinationSheet.HeadersFooters = sourceSheet.HeadersFooters;
destinationSheet.PrintOptions.Portrait = sourceSheet.PrintOptions.Portrait;

excel.Save("output.xlsx");

Does this solve your issue?

Regards,
Mario

Hi, thanks for the quick reply!

This looks good to me, I have a question though.

Would it make any kind of problem if I save It as .ods after loading it as .xlsx ?

Kind regards.

No, you can feel free to save the ExcelFile to any supported output format:

1 Like