Page Set up setHeader and footer not working

Hi,

I am trying to set the header and footer based on an example off the aspose website, but all i get back is an empty excel file. Below is the example I am using. Can you please look into it and let me know if I am missing something?

Workbook workbook = new Workbook();

PageSetup pageSetup = workbook.getWorksheets().get(0).getPageSetup();

pageSetup.setHeader(0, "&A");

pageSetup.setHeader(1, "&\"Times New Roman,Bold\"&D-&T");

pageSetup.setHeader(2, "&\"Times New Roman,Bold\"&12&F");

pageSetup.setFooter(0, "Hello World! &\"Courier New\"&14 123");

pageSetup.setFooter(1, "&G");

pageSetup.setFooter(2, "&Pof&N");

workbook.save("C:\\temp\\test.xls");

Thanks!!

Hi,


Well, I tested your code with v7.0.1.x, it works fine. To see if headers/footers are placed properly, you may open PageSetup dialog box in MS Excel and click the Header/Footer tab in the dialog. Sure, you will get empty Excel file as you are only setting headers and footers on the fist sheet. To see headers and footers, you need to take the print preview of the sheet, but, to see the print preview in MS Excel, you should at least add something into any cell of the sheet otherwise MS Excel will prompt you that it is blank sheet so it could not show the print preview.

For reference, see the document:
http://www.aspose.com/documentation/java-components/aspose.cells-for-java/setting-headers-and-footers.html

Thank you.

1 Like

Thank you for the reply. I have another question. I have a pdf which is already generated. Is it possible to open that pdf and add a page header to it? I am using aspose v7.

Thanks.

Hi,

It is not possible with Aspose.Cells to open the existing pdf and change it. However, Aspose.Pdf can do such a thing.

You should ask such a question from Aspose.Pdf team. Please follow this link: Aspose.Pdf Product Family - Forum

Thank you I will post it there. One more thing, is it possible to add a background color to a page header?

Hi,


I don’t think it is possible in MS Excel. If you have different opinion, kindly create a template Excel file in Ms Excel manually with header/footer set with background/fill color, attach the file here. We will check it soon.

Thank you.

We need to make the report name as the page header. But as per this thread you cannot see it in excel unless you use Print Preview as in you cannot see page headers when you just open the excel file. I first generate excel and export to pdt. So is there a way I can manually set the report name as the first row in excel and when i export to pdf that can be overwritten by the page header?

Hi,

Please check the code below and source xlsx file and its output pdf and see if it is feasible for you.

Actually, it uses PrintTitle Rows, the first row will be printed on all pages. Please see the screenshot.

C#


String path = @“F:\Shak-Data-RW\Downloads\RowsToRepeat.xlsx”;


Workbook workbook = new Workbook(path);


Worksheet worksheet = workbook.Worksheets[0];


worksheet.PageSetup.PrintTitleRows = “$1:$1”;


workbook.Save(path + “.out.pdf”);


Screenshot:

@Amjad_Sahi
Hi !
I have a query regarding the same thing that is Aspose.Cells - Adding Header and Footer.
I am able to add header and footer on a single sheet. But, what if I want to have it on all the sheets of the workbook?
Please help me with this !
Thanks in anticipation :slight_smile:

@Kushal.20,

To add headers/footers to all the worksheets in the workbook, you may easily accomplish the task in a loop. You may try to use the same code but in a loop (that traverse all the sheets in the workbook), see the sample code segment for your reference:
e.g
Sample code:

var workbook = new Workbook("e:\\test2\\Book1.xlsx");
            foreach (Worksheet sheet in workbook.Worksheets)
            {
                     PageSetup pageSetup = sheet.PageSetup;
                     //Setting the Sheet's name in the right section of the page header with the script
                     pageSetup.SetHeader(2, "&A");

            }
            workbook.Save("e:\\test2\\out1.xlsx");

Also, see the document for your further reference:

1 Like

Thanks for this, @Amjad_Sahi !
It worked ! Bravo ! :slight_smile:

@Kushal.20,

Good to know that your issue is sorted out by the suggested line of code. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.

1 Like