Exporting selected sheets from excel workbook to pdf

Hi
I’m very new to Aspose. My employers have provided a licensed copy of Aspose Total .Net.
I need some pointers to the best approach for using your tools to export selected sheets from an excel workbook to pdf format.
Many thanks for your attention.
Jeff

Hi Jeff,

Thanks for your posting and considering Aspose.Cells.

You can hide your unwanted worksheets and then save your workbook into pdf format. In this way, only the visible worksheets will be exported to pdf.

Please see the following documentation article. It saves each worksheet into a separate pdf by hiding all the other worksheets except the one which needs to be saved into pdf format. You can use the same approach and hide all of your unwanted worksheets and keep visible your wanted worksheets and then save the workbook into pdf format.

Thanks for the quick reply, very impressed.

Please advise which dll I should reference for a VS2010 c# .Net4.0 project.
I have folders for net2.0, net3.5, net3.5_ClientProfile.
In each the dll version is 8.2.1.0.
And is this the latest?

Hi,

Thanks for your posting and considering Aspose.Cells.

Please use the DLL found inside the net2.0 folder. This DLL can be used for any NET Framework like 2.0, 3.5, 4.0, 4.5 etc. It will suit your needs. Let us know if you face any issue, we will be glad to look into it and help you further.

Thanks
I shall be running from within a .Net VSTO add-in where the workbook is already open. Can your tool use the workbook in memory for its own workbook object? Or do we need to always save and open in your workbook object?

Hi Jeff,

Thanks for your posting and considering Aspose.Cells.

You can create a Memory Stream object from your Excel spreadsheet and then create as many Workbook objects as you like.

For example, see the following code. It first reads all the bytes of Excel spreadsheet, then it creates the memory stream object from the byte array and finally it creates Workbook object from memory stream.

C#


string filePath = @“F:\Shak-Data-RW\Downloads\sample.xlsx”;


byte[] bytes = File.ReadAllBytes(filePath);


//Create memory stream object from byte array

MemoryStream ms = new MemoryStream();

ms.Write(bytes, 0, bytes.Length);

ms.Position = 0;


//Create as many workbook objects from memory stream

Workbook workbook = new Workbook(ms);


//Save the workbook on disk

workbook.Save(“output.xlsx”, SaveFormat.Xlsx);


Thanks for these excellent replies.
Please advise if there is any method that allows an Excel Named Range to be the only part of the worksheet that is exported to PDF.
Thanks for your attention

Hi Jeff,

Thanks for your posting and using Aspose.Cells.

You can set the print area using the Worksheet.PageSetup.PrintArea property and set it with your desired range, then only that range will be rendered inside the PDF.

Please see the following code. I have attached the sample Excel file used in this code and PDF file generated by it for your reference. As you can see, the range inside the yellow fill color is rendered inside the pdf. i.e E8:K17

C#


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


Workbook workbook = new Workbook(filePath);


Worksheet worksheet = workbook.Worksheets[0];

worksheet.PageSetup.PrintArea = “E8:K17”;


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

Thanks hugely for this, works nicely.



I have found some issues which I’ve had to work around:



1) Aspose.Cells.Name.SheetIndex is not zero based but 1-based.



2) When Name Range is Workbook scope Aspose.Cells.Name.SheetIndex not very helpfully returns 0, but I can understand why.



3) worksheet.PageSetup.PrintArea ignores R1C1 references, remaining null after assignment.



4) workbook.Save using worksheet.PageSetup.PrintArea still requires the worksheet to be the only visible worksheet.



but these are very minor issues easily resolved.

Hi Jeff,

Thanks for your posting and using Aspose.Cells.

1,2)
Yes, you are right. Aspose.Cells.Name.SheetIndex is not 0 based and 0 here means Workbook scope while the other positive number means the 1-based index of the sheet.

3)
You can convert your R1C1 references to A1 using the CellsHelper.ConvertR1C1FormulaToA1() method.

4)
You will have to keep the sheet visible if you want to render the sheet into pdf otherwise it will not render.