Converting a specific worksheet into pdf using Aspose.Cells for .NET in C#

Hi,
Is there any way I can convert a specific worksheet into PDF format? I mean, I have XLS spreadsheet with multiple worksheets. Only one of them contain valid output/results while rest of them contain raw data. I only require to convert the worksheet with valid output/results.

I’ve tried this, as per your kb article…
Workbook wb = new Workbook();
wb.Open(“C:\MyExcel.xls”);

wb.Save(“C:\MyExcel.xml”, FileFormatType.AsposePdf);
Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();
pdf.BindXML(“C:\MyExcel.xml”, null);
pdf.Save(“C:\MyExcel.pdf”);

this works fine but it converts all the worksheets into PDF…but I only need to covert one sheet into PDF as rest of the worksheets do not make much sense as they contain raw data…
for example…
Aspose.Cells.Worksheet resultWorkSheet = wb.Worksheets[“Results”];
so is there any way I can convert this specific worksheet into PDF…

Cheers,


Hi,

Thank you for considering Aspose.

Well, you may hide the worksheets you don’t want to export to PDF. Also, you may try our new direct XLS2PDF conversion method which does not require the Aspose.PDF component to generate a PDF file. Please see the following sample code for your desired functionality with direct XLS2PDF conversion option,

Sample Code:

Workbook workbook = new Workbook();

workbook.Open("C:\\ExcelTemplate.xls");

for (int i = 0; i < workbook.Worksheets.Count; i++)

{

if (!workbook.Worksheets[i].Name.Equals("Summary"))

{

workbook.Worksheets[i].IsVisible = false;

}

}

workbook.Save("C:\\output.pdf",FileFormatType.Pdf);

Thank You & Best Regards,

Hi,

And, please check the doc topic for direct conversion (Excel2Pdf) approach:

https://docs.aspose.com/display/cellsnet/Convert+Excel+Workbook+to+PDF

https://docs.aspose.com/display/cellsjava/Convert+Excel+file+to+PDF+format+compatible+with+PDFA-1a

Thank you.

Thanks for your help Guys…that did work, however I’ve noticed one thing, if the Excel cell contains a formula, in pdf, I’m getting an empty cell ( no value in it ). Is there any way we can get around this problem?
PS: I’m using 4.7.1.0 version for Aspose.Cell

Hi,

Please call Workbook.CalculateFormula() method before saving the excel file to pdf format.
e.g.

..................

workbook.CalculateFormula();

workbook.Save("C:\\output.pdf",FileFormatType.Pdf);

Awesome…works like a charm…thanks so much Amjad…much appreciate your help…
Also, after conversion far right side of pdf document is cutting off ( may be 1/4 of an inch…) can we manipulate margins or scale the document?

Hi,

Yes, you may try to set pagesetup options for your requirement, see the following docs:

https://docs.aspose.com/display/cellsnet/Page+Setup+Features
https://docs.aspose.com/display/cellsjava/Page+Setup+Features

Thank you.