Open Source Java Library for Spreadsheet Documents
Convert Excel files in Java applications via Open Source API.
Documents4J is an open-source Java API from converting Microsoft Excel to other file formats. This is achieved by delegating the conversion to any native application which understands the conversion of the given file into the desired target format. The API offers two types of implementations local and remote. Using the local version document can be converted on the same machine which is cable of converting the requested file format. Using the Remote API, you send a document to the server using REST-API and the server performs the requested conversion.
Documents4J is transparent and simple to use. Developers can work with the local version of the API while development and the remote version can be used when the developers publish the app on the production.
Getting Started with Documents4J
First of all, you need to create a copy of documents4j on your local machine. Simply clone documents4j's repository by using git or by cloning it directly on GitHub. Once the repository is cloned, you can build the project using Mave
Install Documents4J via GitHub
git clone https://github.com/documents4j/documents4j.git
cd documents4j
mvn package
Convert Microsoft Excel using Java
Documents4J is a fluent API for performing document conversion. The API does not expose any details of the backing converter implementation. To perform document conversion the API offers IConverter interface. Using this interface you can convert the Microsoft Excel file format to your desired file format. To find out the supported conversion file formats you can query getSupportedConversion() method which will return the source and the target file formats.
Convert Excel File to Other File Format via Java
Const WdExportFormatPDF = 17
Const MagicFormatPDF = 999
Dim arguments
Set arguments = WScript.Arguments
' Transforms a file using MS Excel into the given format.
Function ConvertFile( inputFile, outputFile, formatEnumeration )
Dim fileSystemObject
Dim excelApplication
Dim excelDocument
' Get the running instance of MS Excel. If Excel is not running, exit the conversion.
On Error Resume Next
Set excelApplication = GetObject(, "Excel.Application")
If Err <> 0 Then
WScript.Quit -6
End If
On Error GoTo 0
' Find the source file on the file system.
Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
inputFile = fileSystemObject.GetAbsolutePathName(inputFile)
' Convert the source file only if it exists.
If fileSystemObject.FileExists(inputFile) Then
' Attempt to open the source document.
On Error Resume Next
Set excelDocument = excelApplication.Workbooks.Open(inputFile, , True)
If Err <> 0 Then
WScript.Quit -2
End If
On Error GoTo 0
On Error Resume Next
If formatEnumeration = MagicFormatPDF Then
excelDocument.ExportAsFixedFormat xlTypePDF, outputFile
Else
excelDocument.SaveAs outputFile, formatEnumeration
End If
' Close the source document.
excelDocument.Close False
If Err <> 0 Then
WScript.Quit -3
End If
On Error GoTo 0
' Signal that the conversion was successful.
WScript.Quit 2
Else
' Files does not exist, could not convert
WScript.Quit -4
End If
End Function
' Execute the script.
Call ConvertFile( WScript.Arguments.Unnamed.Item(0), WScript.Arguments.Unnamed.Item(1), CInt(WScript.Arguments.Unnamed.Item(2)) )
Convert Office Documents to PDF via Java
The open source Documents4J library has included several important features for converting Microsoft office documents like Word, Excel and PowerPoint files to other support files formats like PDF or image etc. The following example demonstrated how easily can software programmers load and converts a Microsoft Word Docx file to PDF file with just a couple of lines of code.
Convert Office Docx File to PDF via Java Library
public class NewMain {
/**
* @param args the command line arguments
* @throws java.io.FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException, ExecutionException {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
InputStream in = new BufferedInputStream(new FileInputStream(System.getProperty("user.dir") + File.separator +"out.rtf"));
IConverter converter = LocalConverter.builder()
.baseFolder(new File(System.getProperty("user.dir") + File.separator +"test"))
.workerPool(20, 25, 2, TimeUnit.SECONDS)
.processTimeout(5, TimeUnit.SECONDS)
.build();
Future conversion = converter
.convert(in).as(DocumentType.RTF)
.to(bo).as(DocumentType.PDF)
.prioritizeWith(1000) // optional
.schedule();
conversion.get();
try (OutputStream outputStream = new FileOutputStream("out.pdf")) {
bo.writeTo(outputStream);
}
in.close();
bo.close();
}
}