Save Each Worksheet to Different PDF using Aspose.Cells

Aspose.Cells - Save Each Worksheet to Different PDF

Aspose.Cells supports converting XLS files (that contain images, charts, etc.) to PDF documents. Aspose.Cells for Java can work independently to convert a spreadsheet to a PDF document, and you do not need to use Aspose.Pdf for Java for the conversion any longer. The conversion does not require creating or using any temporary files, as the whole process can be done in memory.

Java

 // Get the Excel file path
String filePath = dataDir + "workbook.xlsx";

// Instantiate a new workbook and open the Excel file from its location
Workbook workbook = new Workbook(filePath);

// Get the count of the worksheets in the workbook
int sheetCount = workbook.getWorksheets().getCount();

// Make all sheets invisible except the first worksheet
for (int i = 1; i < workbook.getWorksheets().getCount(); i++)
{
     workbook.getWorksheets().get(i).setVisible(false);
}

// Take PDFs of each sheet
for (int j = 0; j < workbook.getWorksheets().getCount(); j++)
{
    Worksheet ws = workbook.getWorksheets().get(j);
    workbook.save(dataPath + ws.getName() + ".pdf");

    if (j < workbook.getWorksheets().getCount() - 1)
    {
       workbook.getWorksheets().get(j + 1).setVisible(true);
       workbook.getWorksheets().get(j).setVisible(false);
    }
}

Download Running Code

Download Sample Code