How to Split an Excel XLSX File into Separate Worksheets using Java
The multi-worksheet system in Excel makes it possible to store several projects in the same file. While that’s a great feature for consolidating related materials, it also means a lot of potentially irrelevant information can make its way into a stakeholder’s view, creating unnecessary clutter and confusion. That’s why splitting excel files is common practice; worksheet 1 is intended for stakeholder A, worksheet 2 is intended for stakeholder B, and so on.
Thankfully, it’s easy to create new files out of the individual worksheets within an Excel file using our Excel Document Split API. This API allows you to either return the individual worksheet contents directly or create a shareable URL link for each worksheet present (you can configure either option by including a Boolean in the optional parameter ‘returnDocumentContents’). Below, I’ll demonstrate how you can easily take advantage of this API using ready-to-run Java code examples to structure your API call.
Before we call the API, let’s first install the SDK with Maven. Add a reference to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then add a reference to the dependency in pom.xml:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
Now we can call the function. First, add the imports to the top of your file, and below that, include your Cloudmersive API key to authenticate (if you don’t have one, you can get one by registering a free account on our website):
// 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.SplitDocumentApi;ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth 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");SplitDocumentApi apiInstance = new SplitDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
Boolean returnDocumentContents = true; // Boolean | Set to true to return the contents of each Worksheet directly, set to false to only return URLs to each resulting worksheet. Default is true.
try {
SplitXlsxWorksheetResult result = apiInstance.splitDocumentXlsx(inputFile, returnDocumentContents);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling SplitDocumentApi#splitDocumentXlsx");
e.printStackTrace();
}
Once you configure your file path input, you’re good to go — no more code required.