C# sample code for Aspose.Cells for .NET

Hello,

Our customer in Japan who is interested in purchasing Aspose.Cells for .NET and send a technical question.

Can you send me a line for C# sample code to convert a specific sheet of Excel to PDF after selecting it?
Thank you in advance for your help.

Best regards,
Thomas Moon

@ThomasMBK,
Please refer to the following codes:

Workbook workbook = new Workbook("Book1.xlsx");

int activeSheetIndex = workbook.Worksheets.ActiveSheetIndex;

PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
//set active sheet index to sheet set.
pdfSaveOptions.SheetSet = new SheetSet(new int[] { activeSheetIndex });
workbook.Save("output.pdf", pdfSaveOptions);

Hi John,

Thank you for your help. Can you please confirm additional question below from the customer?

“It appears that this sample demonstrates how to convert files to PDF by specifying the order of the sheets. Do you happen to have a sample code that demonstrates how to specify sheet names and convert them to PDF?”

Best regards,
Thomas Moon

@ThomasMBK,
You can get the index according to the name of the sheet. Please refer to the following codes:

            Workbook workbook = new Workbook("Book1.xlsx");
            WorksheetCollection sheets = workbook.Worksheets;

            //If you want to export the contents of the following three sheets
            string[] sheetNames = {"Sheet1", "Sheet3", "Sheet5"};
            int[] sheetIndexes = new int[3];
            int index = 0;
            foreach(string sheetName in sheetNames)
            {
                sheetIndexes[index++] = sheets[sheetName].Index;
            }

            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            //set sheet indexes to sheet set.
            pdfSaveOptions.SheetSet = new SheetSet(sheetIndexes);

            workbook.Save("output.pdf", pdfSaveOptions);