Free Support Forum - aspose.com

Wrong output on skipping blank pages in excel sheet pdf conversion

Output_After_Skipping_Blank_Sheet1.zip (124.9 KB)
Output_Before_Skipping_Blank_Sheet1.zip (160.3 KB)

I have attached reference file above

Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(filePath);

                    // Get the count of the worksheets in the workbook
                    int sheetCount = workbook.Worksheets.Count;

                    // Make all sheets invisible except first worksheet
                    for (int i = 1; i < workbook.Worksheets.Count; i++)
                    {
                        workbook.Worksheets[i].IsVisible = false;
                    }

                    // Take Pdfs of each sheet
                    for (int j = 0; j < workbook.Worksheets.Count; j++)
                    {
                        Aspose.Cells.Worksheet ws = workbook.Worksheets[j];
                        if (ws.Cells.Count > 0)
                        {
                            string ouputPath = outputFilePath + "worksheet-" + ws.Name + ".out1.pdf";
                            workbook.Save(ouputPath);
                            convertedPdfList.Add(ouputPath);
                            if (j < workbook.Worksheets.Count - 1)
                            {
                                workbook.Worksheets[j + 1].IsVisible = true;
                                workbook.Worksheets[j].IsVisible = false;
                            }
                        }
                    }

Above is code after skipping blank sheet, it returns wrong output…

                    Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(filePath);

                    // Get the count of the worksheets in the workbook
                    int sheetCount = workbook.Worksheets.Count;

                    // Make all sheets invisible except first worksheet
                    for (int i = 1; i < workbook.Worksheets.Count; i++)
                    {
                        workbook.Worksheets[i].IsVisible = false;
                    }

                    // Take Pdfs of each sheet
                    for (int j = 0; j < workbook.Worksheets.Count; j++)
                    {
                        Aspose.Cells.Worksheet ws = workbook.Worksheets[j];
                        
                            string ouputPath = outputFilePath + "worksheet-" + ws.Name + ".out1.pdf";
                            workbook.Save(ouputPath);
                            convertedPdfList.Add(ouputPath);
                            if (j < workbook.Worksheets.Count - 1)
                            {
                                workbook.Worksheets[j + 1].IsVisible = true;
                                workbook.Worksheets[j].IsVisible = false;
                            }
                        
                    }

Source code before skipping blank sheets

Please check, Why after skipping blank sheets return wrong output of pdf file

Thanks
Satyendra Kumar

@itsathere

Thanks for using Aspose APIs.

Please note, index in Excel starts from 1 but in Aspose.Cells, it starts from 0.

i.e.

1st worksheet index will be 0 in Aspose.Cells.
2nd worksheet index will be 1 in Aspose.Cells
3rd worksheet index will be 2 in Aspose.Cells
4th worksheet index will be 3 in Aspose.Cells

etc.

It seems, you are hiding the worksheet at index 3 which is actually hiding the worksheet number four.

We tested this issue with the following code and it gave the correct output. Please see the output Pdf generated by the code for your reference.

Download Link:
output.pdf (41.0 KB)

C#

Workbook wb = new Workbook("Tables.xlsx");
wb.Worksheets["Sheet4"].IsVisible = false;
wb.Save("output.pdf");

Besides, we have looked into your code and found, it needs some tweaks. Please see the updated sample code with comments for your reference. We have tested it and it works fine.

C#

Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook("e:\\test2\\Tables.xlsx");

// Get the count of the worksheets in the workbook
int sheetCount = workbook.Worksheets.Count;

// Make all sheets invisible except first worksheet
for (int i = 1; i < workbook.Worksheets.Count; i++)
{
	workbook.Worksheets[i].IsVisible = false;
}

// Take Pdfs of each sheet
for (int j = 0; j < workbook.Worksheets.Count; j++)
{
	Aspose.Cells.Worksheet ws = workbook.Worksheets[j];
	if (ws.Cells.Count > 0)
	{
		string ouputPath = "e:\\test2\\" + "worksheet-" + ws.Name + ".out121.pdf";
		workbook.Save(ouputPath);
		convertedPdfList.Add(ouputPath);                    
	}
	
	You have to move this code segment outside of the upper if condition, so it could be evaluated every time for your task.
	if (j < workbook.Worksheets.Count - 1)
	{
		workbook.Worksheets[j + 1].IsVisible = true;
		workbook.Worksheets[j].IsVisible = false;
	}
}