Conversión de hoja de cálculo a diferentes formatos de imagen

Conversión de hoja de cálculo a imagen

Las hojas de cálculo contienen datos que quieres analizar. Por ejemplo, una hoja de cálculo puede contener parámetros, totales, porcentajes, excepciones y cálculos.

Como desarrollador, es posible que necesites presentar hojas de cálculo como imágenes. Por ejemplo, es posible que necesites utilizar una imagen de una hoja de cálculo en una aplicación o página web. Es posible que desees insertar una imagen en un documento de Microsoft Word, un archivo PDF, una presentación de PowerPoint u otro tipo de documento. En resumen, querrás una hoja de cálculo renderizada como una imagen para poder utilizarla en otro lugar.

Aspose.Cells admite la conversión de hojas de cálculo de Excel a imágenes. Para usar esta función, es necesario importar el espacio de nombres Aspose.Cells.Rendering en tu programa o proyecto. Tiene varias clases valiosas para representar e imprimir, por ejemplo, SheetRender, ImageOrPrintOptions y otras.

La clase Aspose.Cells.Rendering.ISheetRender representa una hoja de cálculo para renderizar como imágenes. Tiene un método sobrecargado, ToImage, que puede convertir una hoja de cálculo en archivo(s) de imagen con diferentes atributos u opciones. Se admiten varios formatos de imagen, por ejemplo, BMP, PNG, GIF, JPG, JPEG, TIFF, EMF.

El siguiente fragmento de código muestra cómo convertir una hoja de cálculo en un archivo de Excel a un archivo de imagen.

Formato PNG

Por favor, consulte el siguiente código de muestra, su archivo Excel de muestra y las imágenes PNG de salida.

Aspose::Cells::Startup();
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
// Source directory path.
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path.
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file.
U16String sampleConvertingWorksheetToDifferentImageFormats = srcDir + u"sampleConvertingWorksheetToDifferentImageFormats.xlsx";
// Create an empty workbook.
Workbook workbook(sampleConvertingWorksheetToDifferentImageFormats);
// Access first worksheet.
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Create image or print options object.
ImageOrPrintOptions imgOptions;
// Specify the image format.
imgOptions.SetImageType(ImageType::Png);
// Specify horizontal and vertical resolution
imgOptions.SetHorizontalResolution(200);
imgOptions.SetVerticalResolution(200);
// Render the sheet with respect to specified image or print options.
SheetRender sr(worksheet, imgOptions);
// Get page count.
int pageCount = sr.GetPageCount();
// Create string builder object for string concatenations.
std::string sb;
// Render each page to png image one by one.
for (int i = 0; i < pageCount; i++)
{
// Clear string builder and create output image path with string concatenations.
sb = "";
sb += outDir.ToUtf8();
sb += "outputConvertingWorksheetToImagePNG_";
sb += std::to_string(i);
sb += ".png";
// Get the output image path.
U16String outputPNG(sb.c_str());
// Convert worksheet to png image.
sr.ToImage(i, outputPNG);
}
Aspose::Cells::Cleanup();

Formato TIFF

Por favor, consulte el siguiente código de muestra, su archivo Excel de muestra y la imagen TIFF de salida.

Aspose::Cells::Startup();
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
// Source directory path.
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path.
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file.
U16String sampleConvertingWorksheetToDifferentImageFormats = srcDir + u"sampleConvertingWorksheetToDifferentImageFormats.xlsx";
// Create an empty workbook.
Workbook workbook(sampleConvertingWorksheetToDifferentImageFormats);
// Access first worksheet.
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Create image or print options object.
ImageOrPrintOptions imgOptions;
// Specify the image format.
imgOptions.SetImageType(ImageType::Tiff);
// Specify horizontal and vertical resolution
imgOptions.SetHorizontalResolution(200);
imgOptions.SetVerticalResolution(200);
// Render the sheet with respect to specified image or print options.
SheetRender sr(worksheet, imgOptions);
// Get the output image path.
U16String outputTiff = outDir + u"outputConvertingWorksheetToImageTiff.tiff";
// Convert worksheet to tiff image.
sr.ToTiff(outputTiff);
Aspose::Cells::Cleanup();

Conversión de hoja de cálculo a SVG

SVG significa Gráficos Vectoriales Escalables. SVG es una especificación basada en estándares XML para gráficos vectoriales bidimensionales. Es un estándar abierto que ha estado en desarrollo por el Consorcio World Wide Web (W3C) desde 1999.

Aspose.Cells for C++ ha podido convertir hojas de cálculo a imagen SVG desde la versión 18.5.0.

Para usar esta función, importa el espacio de nombres Aspose.Cells.Rendering a tu programa o proyecto. Tiene varias clases valiosas para renderizar e imprimir, por ejemplo, ISheetRender, IImageOrPrintOptions y otros.

La clase Aspose.Cells.Rendering.IImageOrPrintOptions especifica que la hoja de cálculo se guardará en formato SVG. El siguiente fragmento de código muestra cómo convertir una hoja de cálculo en un archivo Excel a un archivo de imagen SVG.

Por favor, consulta el siguiente código de muestra, su archivo de Excel de muestra y las Imágenes SVG de salida.

Aspose::Cells::Startup();
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
// Source directory path.
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path.
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file.
U16String sampleConvertingWorksheetToDifferentImageFormats = srcDir + u"sampleConvertingWorksheetToDifferentImageFormats.xlsx";
// Create an empty workbook.
Workbook workbook(sampleConvertingWorksheetToDifferentImageFormats);
// Access first worksheet.
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Create image or print options object.
ImageOrPrintOptions imgOptions;
// Specify the image format.
imgOptions.SetImageType(ImageType::Svg);
// Specify horizontal and vertical resolution
imgOptions.SetHorizontalResolution(200);
imgOptions.SetVerticalResolution(200);
// Render the sheet with respect to specified image or print options.
SheetRender sr(worksheet, imgOptions);
// Get page count.
int pageCount = sr.GetPageCount();
// Create string builder object for string concatenations.
std::string sb;
// Render each page to png image one by one.
for (int i = 0; i < pageCount; i++)
{
// Clear string builder and create output image path with string concatenations.
sb = "";
sb += outDir.ToUtf8();
sb += "outputConvertingWorksheetToImageSVG_";
sb += std::to_string(i);
sb += ".svg";
// Get the output image path.
U16String outputSvg(sb.c_str());
// Convert worksheet to svg image.
sr.ToImage(i, outputSvg);
}
Aspose::Cells::Cleanup();