How to remove a worksheet from an Excel XLSX spreadsheet in Java
XLSX format is notorious for its difficult parsing, to which I am sure you will agree if you have ever attempted it yourself. Rather than try to peel through all the layers of this format to try and find the worksheet you are looking for, we will instead be using a much simpler approach this day. With a little help from an API, we will be ready to start getting results just a few minutes from now.
Let’s begin with our references. First we need a repository reference for Jitpack.
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Jitpack will then dynamically compile our API client after you add the following dependency.
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.54</version>
</dependency>
</dependencies>
And finally, we can call our function by following this example code.
// 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.EditDocumentApi;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");EditDocumentApi apiInstance = new EditDocumentApi();RemoveXlsxWorksheetRequest reqConfig = new RemoveXlsxWorksheetRequest(); // RemoveXlsxWorksheetRequest | Spreadsheet input requesttry {byte[] result = apiInstance.editDocumentXlsxDeleteWorksheet(reqConfig);System.out.println(result);} catch (ApiException e) {System.err.println("Exception when calling EditDocumentApi#editDocumentXlsxDeleteWorksheet");e.printStackTrace();}
Done.