Friday, 28 January 2022

Convert Excel to Office Open XML and Vice Versa in Java

Office Open XML (also informally called OOXML) is a zipped, XML-based file format used to represent spreadsheets, charts, presentations and word processing documents. This article will show you how to convert Excel to Office Open XML and vice versa using Java codes.

DEPENDENCY

First of all, you're required to download the package of Free Spire.XLS for Java from this link, and then add the Spire.Xls.jar file as a dependency in your Java program. If you use Maven, you can add the following code to the pom.xml file to import the JAR file.

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

Convert Excel to Office Open XML

Free Spire.XLS for Java offers the Workbook.saveAsXml() method to convert Excel to Office Open XML. The following are detailed steps.

l  Create a Workbook instance.

l  Load a sample Excel document using Workbook.loadFromFile() method.

l  Save the document as Office Open XML using Workbook.saveAsXml() method.

import com.spire.xls.Workbook;

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

//Save as Office Open XML file format
workbook.saveAsXml("output/ToOpenXML.xml");
}
}


Convert Office Open XML to Excel

 The following are the steps to convert an Office Open XML file to Excel:

l  Create a Workbook instance.

l  Load an Office Open XML file using Workbook.loadFromXml() file.

l  Use Workbook.saveToFile() method to save the Office Open XML file as Excel.

import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;

public class OpenXMLToExcel {
public static void main(String[] args) {
//Create an instance of Workbook class
Workbook workbook = new Workbook();
//Load an Office Open XML file
workbook.loadFromXml("C:\\Users\\Test1\\Desktop\\ToOpenXML.xml");

//Save as Excel XLSX file format
workbook.saveToFile("output/ToExcel.xlsx", ExcelVersion.Version2016);
}
}







Tuesday, 25 January 2022

Print PowerPoint Documents in Java

PowerPoint presentations are often shared on a projector or display, but sometimes you might need a physical copy to share with your audience. This article will show you how to programmatically print all slides or some slides using Java codes.

DEPENDENCY

In order to realize the operation mentioned above, you’re required to download the package of Free Spire.Presentation for Java from this link, and then manually add Spire.Presentation.jar to your Java program. Or you can create a Maven project, and type the following codes in the pom.xml file to easily add the JAR file.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

Print All Slides of a PowerPoint Document

Free Spire.Presentation for Java supports printing all slides of a PowerPoint document using the PresentationPrintDocument.print() method. The following are detailed steps.

l  Create a Presentation instance.

l  Load a sample PowerPoint document using Presentation.loadFromFile() method.

l  Create a PresentationPrintDocument instance.

l  Print all slides of the document using PresentationPrintDocument.print() method.

import com.spire.presentation.Presentation;
import com.spire.presentation.PresentationPrintDocument;

public class PrintAllSlides {
public static void main(String[] args) throws Exception {

//Create a Presentation instance
Presentation presentation = new Presentation();
//Load a sample PowerPoint document
presentation.loadFromFile("Sample.pptx");

//Print all slides of the document with the default printer
PresentationPrintDocument document = new PresentationPrintDocument(presentation);
document.print();
presentation.dispose();
}
}

Print All Slides of a PowerPoint Document

Free Spire.Presentation for Java supports printing all slides of a PowerPoint document using the PresentationPrintDocument.print() method. The following are detailed steps.

l  Create a Presentation instance.

l  Load a sample PowerPoint document using Presentation.loadFromFile() method.

l  Create a PresentationPrintDocument instance.

l  Print all slides of the document using PresentationPrintDocument.print() method.

import com.spire.presentation.Presentation;
import com.spire.presentation.PresentationPrintDocument;

public class PrintSomeSlides {
public static void main(String[] args) throws Exception {

//Create a Presentation instance
Presentation presentation = new Presentation();
//Load a sample PowerPoint document
presentation.loadFromFile("Sample.pptx");

//Select some slides of the document to print
PresentationPrintDocument document = new PresentationPrintDocument(presentation);
document.selectSlidesForPrint("2-4");
document.print();
presentation.dispose();
}
}


Wednesday, 19 January 2022

Add Borders to Text in Word Documents Using Java

In Microsoft Word, borders can be used to make documents more attractive and beautiful. You can add them to specific text of a paragraph, a whole paragraph or a page. This article will introduce how to add borders to specific text or a whole paragraph using Java codes.

DEPENDENCY

First of all, you’re required to add Spire.Doc.jar to your Java program. You can get it from the link, or create a Maven project, and import the following codes to the pom.xml file.

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

USING THE CODE

 Free Spire.Doc for Java supports not only adding a border to a specific text of a paragraph, but also appending a border to a whole paragraph. The following are detailed steps for reference.

l  Create a Document instance and a new Word document.

l  Add a section to the document using Document.addSection() method.

l  Add a paragraph to the section using Section.addParagraph() method.

l  Append text to the paragraph, add border to the text and set style and color of the border.

l  Append other text to the same paragraph using Paragraph.appendText() method.

l  Append break to the end of the paragraph using Paragraph.appedBreak() method.

l  Add another paragraph to the document using Section.addParagraph() method.

l  Append text to the paragraph, add border to it and set style and color of the border.

l  Save the document to another file using Document.saveToFile() method.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.BorderStyle;
import com.spire.doc.documents.BreakType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextRange;

import java.awt.*;

public class AddBorder {
public static void main(String[] args) {
//Create a Document instance
Document doc = new Document();

//Add a section
Section section = doc.addSection();

//Append text and add a border to it
Paragraph para = section.addParagraph();
TextRange tr = para.appendText("Java");
tr.getCharacterFormat().getBorder().setBorderType(BorderStyle.Single);
tr.getCharacterFormat().getBorder().setColor(Color.RED);
String text = " is a popular programming language, created in 1995. " +
"It is owned by Oracle, and more than 3 billion devices run Java.";
para.appendText(text);
para.appendBreak(BreakType.Line_Break);

//Add a border to a paragraph
para = section.addParagraph();
String text2 = "Java is an object oriented language which gives a clear structure to programs " +
"and allows code to be reused, lowering development costs" ;
para.appendText(text2);
para.getFormat().getBorders().setBorderType(BorderStyle.Single);
para.getFormat().getBorders().setColor(Color.RED);

//Save the document
doc.saveToFile("output/AddBorder.docx", FileFormat.Docx_2013);
}
}




Wednesday, 12 January 2022

Count the Number of Worksheets in Excel Using Java

In Microsoft Excel, you can use the Defined Name and a Formula to count the number of worksheets, and you can also use an Excel VBA Macro to achieve it. Today, I'd like to show you how to automate the work using Java codes.

DEPENDENCY

In order to realize the operation mentioned above, you need a free third-party library called Free Spire.XLS for Java. First of all, please get its package from this link, and manually add Spire.Xls.jar as a dependency to your Java program. If you use Maven, just type the following code in the pom.xml file to easily import the JAR file to the program.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://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

Free Spire.XLS for Java supports counting the number of worksheets in Excel using the getCount() method provided by the IWorksheets interface. The following are detailed steps.

l  Create a Workbook instance.

l  Load a sample Excel document using Workbook.loadFromFile() method.

l  Get a collection of worksheets using Workbook.getWorksheets() method and get the number of worksheets in the collection using the getCount() method provided by the IWorksheets interface.

l  Output the result.

import com.spire.xls.Workbook;

public class CountNumberOfWorsheets {
public static void main(String[] args) {
//Create a Workbook instance
Workbook workbook = new Workbook();

//Load a sample Excel file
workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.xlsx");

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

//Output the result
System.out.println("The number of sheets is "+sheetCount);
}
}



Sunday, 9 January 2022

Convert a Multi-page PDF Document to One Excel Worksheet in Java

In the previous article, I introduced how to convert PDF to Excel using a free third-party library called Free Spire.PDF for Java. Actually that article is to convert each PDF page to a single Excel worksheet, which means the number of pages determines the number of worksheets. This tutorial will demonstrate how to convert multiple PDF pages to one Excel worksheet using Spire.PDF for Java.

DEPENDENCY

First of all, you’re required to download the package of Spire.PDF for Java from the link, and then add Spire.Pdf.jar as a dependency in your Java program. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>

USING THE CODE

Spire.PDF for Java supports setting the conversion option during converting PDF to Excel, for example, multiple pages can be rendered on a single worksheet. The following are detailed steps.

l  Create a PdfDocument instance.

l  Load a sample PDF document including several pages using PdfDocument.loadFromFile() method.

l  Get some options when doing conversion operation using PdfDocument.getConvertOptions() method, and then set conversion options during converting PDF to Excel using PdfConvertOptions.setPdfToXlsxOptions() method.

l  Save the document to Excel using PdfDocument.saveToFile() method.

import com.spire.pdf.*;
import com.spire.pdf.conversion.XlsxLineLayoutOptions;
public class PDFToExcel {
public static void main(String[] args) {
//Create a PdfDocument object
PdfDocument pdf = new PdfDocument();

//Load a sample PDF document
pdf.loadFromFile("C:\\Users\\Test1\\Desktop\\Test.pdf");

//Set the PDF to XLSX conversion options: rendering multiple pages on a single worksheet
pdf.getConvertOptions().setPdfToXlsxOptions(new XlsxLineLayoutOptions(false,true,true));

//Save the document to Excel
pdf.saveToFile("output/ToOneSheet.xlsx", FileFormat.XLSX);
}
}



Wednesday, 5 January 2022

Wrap or Unwrap Text in Excel Cells using Java

In the process of manipulating Excel worksheets, sometimes you may encounter the situation that the text in a cell is so long that some of it is hidden. At this time, it’s recommended to wrap the extra-long text into multiple lines so you can see all of it. This article will demonstrate how to automatically wrap or unwrap text in Excel cells using Java codes.

DEPENDENCY

First of all, you’re required to get the package of a free third-party library called Free Spire.XLS for Java from the link, and then manually add Spire.Xls.jar to your project. Or if you use Maven, you can import the following code to the pom.xml file to add the JAR file.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://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

Free Spire.XLS for Java supports wrapping or unwrapping text in Excel cells using the setWrapText() method provided by the IExtendedFormat interface. Below are detailed steps for your reference.

l  Create a Workbook instance.

l  Load a sample Excel document using Workbook.loadFromFile() method.

l  Get a specific worksheet of the document using Workbook.getWorksheets().get() method.

l  Get a specific cell of the worksheet using Worksheet.getRange().get() method.

l  Get the style of the specified cell using XlsRange.getStyle() method and set whether the text is wrapped or not using setWrapText() method provided by the IExtendedFormat interface.

l  Save the document to another file using Workbook.saveToFile() method.

import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class WrapOrUnwrapText {
public static void main(String[] args) {
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load a sample Excel document
workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.xlsx");

//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);

//Wrap text in the cell "D8"
sheet.getRange().get("D8").getStyle().setWrapText(true);

//Unwrap text in the cell "D6"
sheet.getRange().get("D6").getStyle().setWrapText(false);

//Save the document to another file
workbook.saveToFile("output/WrapOrUnwrapText.xlsx", ExcelVersion.Version2013);
}
}





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...