在这个简单的主题中,您将学习如何使用 Java REST API 将 XLS 转换为 SVG。我们将按照以下部分中提到的详细而清晰的步骤,使用 Java Low Code API 开发一个 XLS 到 SVG 转换器。开发的应用程序可以与 macOS、Linux 或 Windows 环境中支持 Java 的任何应用程序集成,并且可以免费进行转换。
先决条件
创建账户并获取 API 凭证管理 XLS 到 SVG 的转换
下载 Aspose.Cells Cloud SDK for Java to convert XLS to SVG
使用上述 SDK 设置 Java 项目,将 XLS 渲染为 SVG
使用 Java REST API 将 XLS 转换为 SVG 的步骤
- 设置 API 的客户端 ID 和客户端密钥,以将 XLS 渲染为 SVG
- 使用客户端凭据创建 CellsApi 类的对象以执行 XLS 到 SVG 的转换
- 指定源 XLS 和输出 SVG 文件名,并使用 HashMap 中的名称和 File 对象加载源 XLS
- 使用输入 HashMap 创建 PostConvertWorkbookToPDFRequest 的实例
- 调用 Excel to SVG 请求方法使用 Java REST API 将 XLS 转换为 SVG
- 将导出的SVG文件流保存在本地磁盘上
上述步骤使用 Java 低代码 API 将文件类型从 Excel 转换为 SVG。我们将通过配置 SDK 并创建 CellsAPI 类对象来启动转换过程。然后,我们将使用 HashMap 加载源 XLS 文件并创建 PutConvertWorkbookRequest 类的实例,该实例进一步用于使用 PutConvertWorkbook() 方法执行到 SVG 文件流的转换。
Java Low Code API 中 Excel 到 SVG 转换的代码
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); | |
} | |
} | |
} |
此简单示例代码可让您使用 Java 云 API 将 Excel 转换为 SVG。您只需在 Aspose.Cells REST API SDK 的帮助下为磁盘上的 XLS 文件提供有效路径,它就会执行转换,转换过程完成后,下载输出 SVG 文件流以将其保存在本地。
在本主题中,我们探索了使用 Cloud API 将 XLS 转换为 SVG。如果您想执行 Excel 到 TIFF 的转换,请参阅有关如何操作 使用 Java REST API 将 Excel 转换为 TIFF 的文章。