Excel 文件可以包含数字以及图形或图表等视觉信息。要在应用程序中以图形格式显示某些信息,您可能需要将 Excel 转换为 C++ 中的 SVG。本文将通过分步详细信息和示例代码解释如何将 C++ 中的 Excel 更改为 SVG。您可以以 XLSX 或 XLS 格式加载输入文件。
在 C++ 中将 Excel 转换为 SVG 的步骤
- 通过使用 NuGet 包管理器工具添加 Aspose.Cells.Cpp 来配置您的应用程序
- 将 Aspose::Cells 命名空间的引用添加到项目中
- 使用 Workbook 类实例加载输入 Excel 文件
- 设置输出 SVG 文件的格式和图像属性
- 将输出的 SVG 图像保存在从 Excel 文件呈现的磁盘上
这种分步方法解释了将 Excel 转换为 C++ 中的矢量图形 的简单过程,其中加载了输入 Excel 文件并设置了输出图像的不同属性。您可以根据需要设置图像的分辨率属性以及渲染格式。在最后一步中,将输出的 SVG 图像写入磁盘以供进一步处理或查看。
在 C++ 中将 Excel 转换为 SVG 的代码
#pragma once | |
#include "Aspose.Cells.h" | |
class ConvertExcelToSVG | |
{ | |
public: void ConvertExcelToSVGInCplusCplus() | |
{ | |
// Set the Aspose.Cells license to create output SVG without watermark | |
intrusive_ptr<License> rowHeightLicense = new License(); | |
rowHeightLicense->SetLicense(new String("Aspose.Cells.lic")); | |
// Load input workbook | |
intrusive_ptr<Aspose::Cells::IWorkbook> targetWorkbook = Factory::CreateIWorkbook(new String("ConvertExcelToSVG.xlsx")); | |
// Access target worksheet say third one | |
intrusive_ptr<Aspose::Cells::IWorksheet> thirdSheet = targetWorkbook->GetIWorksheets()->GetObjectByIndex(2); | |
// Create object to set image options | |
intrusive_ptr<Aspose::Cells::Rendering::IImageOrPrintOptions> OptionsForRenderingImage = Factory::CreateIImageOrPrintOptions(); | |
// Specify SVG save format | |
OptionsForRenderingImage->SetSaveFormat(Aspose::Cells::SaveFormat::SaveFormat_SVG); | |
// Autofit Cells based on the data | |
OptionsForRenderingImage->SetCellAutoFit(true); | |
// Specify values for the horizontal and vertical resolution | |
OptionsForRenderingImage->SetHorizontalResolution(300); | |
OptionsForRenderingImage->SetVerticalResolution(300); | |
// Using the CreateISheetRender() function, render the sheet by providing the sheet reference and image options | |
intrusive_ptr<Aspose::Cells::Rendering::ISheetRender> sheetRender = Factory::CreateISheetRender(thirdSheet, OptionsForRenderingImage); | |
// Get page count | |
Aspose::Cells::Systems::Int32 totalPages = sheetRender->GetPageCount(); | |
// Initialize string builder object for string concatenation | |
intrusive_ptr<Aspose::Cells::Systems::Text::StringBuilder> stringBuilder = new Aspose::Cells::Systems::Text::StringBuilder(); | |
// Parse throught all the pages to convert them to SVG one by one | |
for (int counter = 0; counter < totalPages; counter++) | |
{ | |
// Set the path for output image with string appending | |
stringBuilder->Clear(); | |
stringBuilder->Append(counter); | |
stringBuilder->Append((StringPtr)new String("_Page.svg")); | |
// Convert Excel to SVG image | |
sheetRender->ToImage(counter, stringBuilder->ToString()); | |
} | |
} | |
}; |
根据上面的代码片段,您需要使用 Factory::CreateIWorkbook 方法来加载输入 Excel 文件。然后使用 IImageOrPrintOptions 类为输出 SVG 图像设置几个首选项,并通过指定文件路径和名称继续保存生成的文件。您可以修改此代码中的任何属性值以满足您的输出标准。
这个简短的教程重点介绍如何使用 C++* 将 Excel 更改为矢量图形。然而,如果您需要将 Excel 图表转换为 JPEG 图像,请参阅 如何在 C++ 中将 Excel 图表转换为 JPEG 上的文章。