将图表转换为 SVG 格式的图像
Contents
[
Hide
]
Scalable Vector Graphics (SVG) 是一种基于 XML 的矢量图像格式,适用于二维图形,还支持交互性和动画。 SVG 规范是万维网联盟 (W3C) 自 1999 年以来制定的开放标准。
SVG 图像及其行为在 XML 文本文件中定义。这意味着它们可以被搜索、索引、编写脚本和压缩。作为 XML 文件,SVG 图像可以使用任何文本编辑器创建和编辑,但更常见的是使用绘图软件创建。
Aspose.Cells可以将图表保存为多种格式的图片,如BMP、JPEG、PNG、GIF、SVG等。本文介绍如何将图表保存为SVG图片。
以下示例代码解释了如何使用 Aspose.Cells 将图表转换为 SVG 格式的图像。该代码加载源 Excel 文件,然后将在第一个工作表上找到的第一个图表保存到 SVG。
以下屏幕截图显示了使用示例代码创建的 SVG 格式的转换图表图像。
输出图像
因为 SVG 是一种基于 XML 的格式,您还可以在记事本等文本编辑器中打开输出图表图像,如该屏幕截图所示。
在文本编辑器中输出 SCG
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(ConvertCharttoImageinSVGFormat.class); | |
// Create workbook object from source Excel file | |
Workbook workbook = new Workbook(dataDir + "sample.xlsx"); | |
// Access the first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Access the first chart inside the worksheet | |
Chart chart = worksheet.getCharts().get(0); | |
// Save the chart into image in SVG format | |
ImageOrPrintOptions options = new ImageOrPrintOptions(); | |
options.setImageType(ImageType.SVG); | |
chart.toImage(dataDir + "ChartImage.svg", options); |