Convert Excel to SVG with Java REST API

In this simple topic, you’ll learn how to convert XLS to SVG with Java REST API. We will develop an XLS to SVG converter with Java Low Code API by following the detailed and lucid steps mentioned in the following section. The developed application can be integrated with any application supporting Java in macOS, Linux, or Windows environments and conversion can be done for free.

Prerequisite

Steps to Convert XLS to SVG with Java REST API

  1. Set the Client ID and Client Secret for the API to render XLS to SVG
  2. Create an object of the CellsApi class with client credentials to perform XLS to SVG conversion
  3. Specify the source XLS and output SVG file names and load the source XLS using a name and File object in a HashMap
  4. Create an instance of the PostConvertWorkbookToPDFRequest with input HashMap
  5. Call the Excel to SVG request method to convert XLS to SVG with Java REST API
  6. Save the exported SVG file stream on the local disk

The aforementioned steps transform the file type from Excel to SVG with Java low code API. We will start the conversion process with the configuration of the SDK and creating the CellsAPI class object. We will then load the source XLS file using a HashMap and create an instance of the PutConvertWorkbookRequest class that is further used for performing the conversion to a SVG file stream using the PutConvertWorkbook() method.

Code for Excel to SVG Conversion in Java Low Code API

package com.aspose.cloud.cells.api;
import com.aspose.cloud.cells.client.ApiException;
import com.aspose.cloud.cells.request.PutConvertWorkbookRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
public class Example_PutConvertWorkbookToSVG {
public void Run() throws IOException, ApiException {
try {
String clientID = "ID";
String clientSecret = "Secret";
String apiBaseUrl = "https://api.aspose.cloud";
String apiVersion = "v3.0";
// Source and output file names
String localPath = "C:/ExcelFiles/";
String inputFileName = "Source.xls";
String outputFormat = "svg";
String outputFileName = "XlsToSvg" + "." + outputFormat;
CellsApi xlsToSvgApi = new CellsApi(clientID, clientSecret, apiVersion, apiBaseUrl);
HashMap<String, File> fileMap = new HashMap<>();
fileMap.put(inputFileName, new File(localPath + inputFileName));
PutConvertWorkbookRequest xlsToSvgRequest = new PutConvertWorkbookRequest();
xlsToSvgRequest.setFile(fileMap);
File svgFile = xlsToSvgApi.putConvertWorkbook(xlsToSvgRequest);
File destinationFile = new File(localPath + outputFileName);
// Create necessary parent directories
destinationFile.getParentFile().mkdirs();
try (FileInputStream inputStream = new FileInputStream(svgFile);
FileOutputStream outputStream = new FileOutputStream(destinationFile)) {
byte[] buffer = new byte[4096]; // Buffer size of 4KB
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
System.out.println("XLS to SVG conversion completed");
} catch (Exception e) {
e.printStackTrace();
}
} catch (ApiException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

This simple example code lets you convert Excel to SVG with Java cloud API. You only need to provide a valid path for the XLS file on the disk with the help of the Aspose.Cells REST API SDK will perform the conversion and after the conversion process is finished, download the output SVG file stream to save it locally.

We have explored to transform XLS to SVG with Cloud API in this topic. If you want to perform Excel to TIFF conversion, refer to the article on how to Convert Excel to TIFF with Java REST API.

 English