How to merge two Excel XLSX files together in Java

Merging Excel files is actually a piece of cake if you follow today’s tutorial. We will have everything wrapped up in 5 minutes flat.

Our initial step is to install our library using Jitpack, which requires two references in pom.xml.

Here is our repository reference.


And our depedency reference.


Our next and final step is invoking mergeDocumentXlsx and specifying inputFile1 and inputFile2. Note that inputFile1’s contents will appear first, with inputFile2 below it. With this in mind, you can easily use this to merge more than two files and maintain a specific order.

// Import classes://import com.cloudmersive.client.invoker.ApiClient;//import com.cloudmersive.client.invoker.ApiException;//import com.cloudmersive.client.invoker.Configuration;//import com.cloudmersive.client.invoker.auth.*;//import com.cloudmersive.client.MergeDocumentApi;ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: ApikeyApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");Apikey.setApiKey("YOUR API KEY");// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.setApiKeyPrefix("Token");MergeDocumentApi apiInstance = new MergeDocumentApi();File inputFile1 = new File("/path/to/file"); // File | First input file to perform the operation on.File inputFile2 = new File("/path/to/file"); // File | Second input file to perform the operation on (more than 2 can be supplied).try {byte[] result = apiInstance.mergeDocumentXlsx(inputFile1, inputFile2);System.out.println(result);} catch (ApiException e) {System.err.println("Exception when calling MergeDocumentApi#mergeDocumentXlsx");e.printStackTrace();}

That’s it! You’re done. For all your other document manipulation needs, be sure to look at our other tutorials.

Image for post

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store