Specify spreadsheet rendering options

GroupDocs.Viewer ships with the SpreadsheetOptions class that allows you to specify different spreadsheet rendering options (for example, you can display row and column headings in the output file, render grid lines, or adjust cell text overflow). To access these options, use the setSpreadsheetOptions method for one of the following classes (depending on the output file format):

Render row and column headings

Rows and columns in a worksheet have unique names displayed on the worksheet’s left and top side. Rows are numbered (1, 2, 3, …, 1048576), and columns are lettered (A, B, C, …, XFD).

Row and column headings in a worksheet

Use the SpreadsheetOptions.setRenderHeadings method to display row and column headings in the output file when you render your spreadsheet in HTML, PDF, PNG, or JPEG format.

The following example demonstrates how to convert an Excel workbook to PDF and display row and column headings in the output PDF file:

import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.options.PdfViewOptions;
// ...

try (Viewer viewer = new Viewer("invoice.xlsx")) {
    // Convert the spreadsheet to PDF.
    PdfViewOptions viewOptions = new PdfViewOptions("output.pdf");
    // Render row and column headings.
    viewOptions.getSpreadsheetOptions().setRenderHeadings(true);
    viewer.view(viewOptions);
}

The following image demonstrates the result:

Render an Excel file with row and column headings to PDF

Render worksheet gridlines

Use the SpreadsheetOptions.setRenderGridLines method to display gridlines (lines that separate worksheet rows and columns) in the output file.

The following code example demonstrates how to convert an Excel workbook to PDF and display gridlines in the output PDF file:

import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.options.PdfViewOptions;
// ...

try (Viewer viewer = new Viewer("invoice.xlsx")) {
    // Convert the spreadsheet to PDF.
    PdfViewOptions viewOptions = new PdfViewOptions("output.pdf");
    // Render gridlines.
    viewOptions.getSpreadsheetOptions().setRenderGridLines(true);
    viewer.view(viewOptions);
}

The following image demonstrates the result:

Render an Excel file with gridlines to PDF

Control cell text overflow

The SpreadsheetOptions.setTextOverflowMode method allows you to prevent text overflow in worksheet cells (see the image below) when you convert your spreadsheet file to HTML, PDF, or image format.

Text overflow in a cell

You can set the TextOverflowMode property to one of the following values:

The following example demonstrates how to set this option in code:

import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.options.PdfViewOptions;
// ...

try (Viewer viewer = new Viewer("invoice.xlsx")) {
    // Convert the spreadsheet to PDF.
    PdfViewOptions viewOptions = new PdfViewOptions("output.pdf");
    // Specify the AutoFitColumn mode.
    viewOptions.getSpreadsheetOptions().setTextOverflowMode(TextOverflowMode.AUTO_FIT_COLUMN);
    viewer.view(viewOptions);
}

Render hidden rows and columns

Use the ViewOptions.getSpreadsheetOptions().setRenderHiddenRows and ViewOptions.getSpreadsheetOptions().setRenderHiddenColumns properties to display hidden rows and columns in the output file when you render your spreadsheet in HTML, PDF, PNG, or JPEG format.

The example below demonstrates how to set this option in code. The rows 20 and 21 and the column E are hidden in the source Excel workbook.

Hidden rows and columns in a worksheet

import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.options.PdfViewOptions;
// ...

try (Viewer viewer = new Viewer("invoice.xlsx")) {
    // Convert the spreadsheet to PDF.
    PdfViewOptions viewOptions = new PdfViewOptions("output.pdf");
    // Enable rendering hidden rows and columns.
    viewOptions.getSpreadsheetOptions().setRenderHiddenRows(true);
    viewOptions.getSpreadsheetOptions().setRenderHiddenColumns(true);
    viewer.view(viewOptions);
}

The image below demonstrates the result. Hidden rows and columns appear in the generated PDF file.

Hidden rows and columns in a worksheet

Render hidden worksheets

If your spreadsheet file contains hidden worksheets, use the ViewOptions.setRenderHiddenPages method to display data from hidden worksheets in the output HTML, PDF, or image files.

The following example demonstrates how to set this option in code:

import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.options.PdfViewOptions;
// ...

try (Viewer viewer = new Viewer("invoice.xlsx")) {
    // Convert the spreadsheet to PDF.
    PdfViewOptions viewOptions = new PdfViewOptions("output.pdf");
    // Enable rendering hidden pages.
    viewOptions.setRenderHiddenPages(true);
    viewer.view(viewOptions);
}

Skip empty rows and columns

GroupDocs.Viewer supports the SpreadsheetOptions.setSkipEmptyRows and SpreadsheetOptions.setSkipEmptyColumns methods that allow you to skip blank rows and columns when you convert your spreadsheet file to HTML, PDF, or image format.

import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.options.PdfViewOptions;
// ...

try (Viewer viewer = new Viewer("invoice.xlsx")) {
    // Convert the spreadsheet to PDF.
    PdfViewOptions viewOptions = new PdfViewOptions("output.pdf");
    // Enable skipping blank rows and columns.
    viewOptions.getSpreadsheetOptions().setSkipEmptyRows(true);
    viewOptions.getSpreadsheetOptions().setSkipEmptyColumns(true);
    viewer.view(viewOptions);
}

The following image demonstrates the result:

Skip empty columns and rows

Render cell comments

Use the ViewOptions.setRenderComments method to display cell comments in the output file when you render your spreadsheet in HTML, PDF, PNG, or JPEG format.

import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.options.PdfViewOptions;
// ...

try (Viewer viewer = new Viewer("invoice.xlsx")) {
    // Convert the spreadsheet to PDF.
    PdfViewOptions viewOptions = new PdfViewOptions("output.pdf");
    // Enable rendering comments.
    viewOptions.setRenderComments(true);
    viewer.view(viewOptions);
}

The following image demonstrates the result:

Render cell comments