Thursday, 4 March 2021

How to print Excel worksheets in Java

In our daily work or study, we often encounter situations where we need to print out the prepared Excel worksheet and post it on the bulletin board or workspace. This tutorial will demonstrate how to print Excel worksheets in Java using a free third-party library called Free Spire.XLS for Java. Normally, printing can be done from the following two methods:

l  Printing Excel worksheets using default printer

l  Printing Excel worksheets using specified printer

Dependency

Before running codes, we need to insert the Jar file in the library into IDEA. You can download it from the website or directly reference it using the following Maven configurations.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls.free</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>

Using the code

Print Excel worksheets using default printer

import com.spire.xls.*;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

public class PrintByDefault {
public static void main(String[] args) {
//Create a workbook and load an Excel file
Workbook workbook = new Workbook();
workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.xlsx");

//Create a PrinterJob object
PrinterJob printerJob = PrinterJob.getPrinterJob();

//Create a PageFormat object and set it to the default size and orientation
PageFormat pageFormat = printerJob.defaultPage();

//Return a copy of the Paper object associated with this PageFormat
Paper paper = pageFormat.getPaper();

//Set the imageable area of this Paper
paper.setImageableArea(0, 0, pageFormat.getWidth(), pageFormat.getHeight());

//Set the Paper object for this PageFormat.
pageFormat.setPaper(paper);

//Set the number of copies
printerJob.setCopies(1);

//Call painter to render the pages in the specified format
printerJob.setPrintable(workbook, pageFormat);

//execute print
try {
printerJob.print();
} catch (PrinterException e) {
e.printStackTrace();
}
}
}

Print Excel worksheets using specified printer

import com.spire.xls.*;
import javax.print.PrintService;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

public class SpecifiedPrinter {
public static void main(String[] args) throws PrinterException {
//Create a workbook and load an Excel file
Workbook workbook = new Workbook();
workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.xlsx");

//Create a PrinterJob object
PrinterJob printerJob = PrinterJob.getPrinterJob();

//Specify printer name
PrintService myPrintService = findPrintService("\\\\192.168.1.104\\HP LaserJet P1007");
printerJob.setPrintService(myPrintService);

//Create a PageFormat object and set it to the default size and orientation
PageFormat pageFormat = printerJob.defaultPage();

//Return a copy of the Paper object associated with this PageFormat.
Paper paper = pageFormat .getPaper();

//Set the imageable area of this Paper.
paper.setImageableArea(0,0,pageFormat .getWidth(),pageFormat .getHeight());

//Set the Paper object for this PageFormat.
pageFormat .setPaper(paper);

//Set the number of copies
printerJob .setCopies(1);

//Call painter to render the pages in the specified format
printerJob .setPrintable(workbook,pageFormat);

//execute print
try {
printerJob.print();
} catch (PrinterException e) {
e.printStackTrace();
}
}
//Get print service by printer name
private static PrintService findPrintService(String printerName) {

PrintService[] printServices = PrinterJob.lookupPrintServices();
for (PrintService printService : printServices) {
if (printService.getName().equals(printerName)) {
return printService;
}
}
return null;
}
}




No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...