Conversión de hoja de trabajo a diferentes formatos de imagen
Convertir hoja de trabajo en imagen
Las hojas de trabajo contienen datos que desea analizar. Por ejemplo, una hoja de cálculo puede contener parámetros, totales, porcentajes, excepciones y cálculos.
Como desarrollador, es posible que deba presentar las hojas de trabajo como imágenes. Por ejemplo, es posible que necesite usar una imagen de una hoja de trabajo en una aplicación o página web. Es posible que desee insertar una imagen en un documento de Word Microsoft, un archivo PDF, una presentación PowerPoint o algún otro tipo de documento. En pocas palabras, desea que una hoja de trabajo se represente como una imagen para poder usarla en otro lugar.
Aspose.Cells admite la conversión de hojas de cálculo de Excel en imágenes. Para utilizar esta función, debe importar elAspose.Cells.Renderingespacio de nombres a su programa o proyecto. Tiene varias clases valiosas para renderizar e imprimir, por ejemplo,ISheetRender, IImageOrPrintOptionsy otros.
La clase Aspose.Cells.Rendering.ISheetRender
representa una hoja de trabajo para representar como imágenes. Tiene un método sobrecargado,A la imagen, que puede convertir una hoja de trabajo 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 de un archivo de Excel en un archivo de imagen.
PNG Formato
Consulte el siguiente código de ejemplo, suejemplo de archivo de Excel , y elSalida PNG Imágenes.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
// Source directory path. | |
StringPtr srcDir = new String("..\\Data\\01_SourceDirectory\\"); | |
// Output directory path. | |
StringPtr outDir = new String("..\\Data\\02_OutputDirectory\\"); | |
// Path of input Excel file. | |
StringPtr sampleConvertingWorksheetToDifferentImageFormats = srcDir->StringAppend(new String("sampleConvertingWorksheetToDifferentImageFormats.xlsx")); | |
// Create an empty workbook. | |
intrusive_ptr<Aspose::Cells::IWorkbook> workbook = Factory::CreateIWorkbook(sampleConvertingWorksheetToDifferentImageFormats); | |
// Access first worksheet. | |
intrusive_ptr<Aspose::Cells::IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
// Create image or print options object. | |
intrusive_ptr<Aspose::Cells::Rendering::IImageOrPrintOptions> imgOptions = Factory::CreateIImageOrPrintOptions(); | |
// Specify the image format. | |
imgOptions->SetImageFormat(Aspose::Cells::Systems::Drawing::Imaging::ImageFormat::GetPng()); | |
// Specify horizontal and vertical resolution | |
imgOptions->SetHorizontalResolution(200); | |
imgOptions->SetVerticalResolution(200); | |
// Render the sheet with respect to specified image or print options. | |
intrusive_ptr<Aspose::Cells::Rendering::ISheetRender> sr = Factory::CreateISheetRender(worksheet, imgOptions); | |
// Get page count. | |
Aspose::Cells::Systems::Int32 pageCount = sr->GetPageCount(); | |
// Create string builder object for string concatenations. | |
intrusive_ptr<Aspose::Cells::Systems::Text::StringBuilder> sb = new Aspose::Cells::Systems::Text::StringBuilder(); | |
// 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->Clear(); | |
sb->Append(outDir); | |
sb->Append((StringPtr)new String("outputConvertingWorksheetToImagePNG_")); | |
sb->Append(i); | |
sb->Append((StringPtr)new String(".png")); | |
// Get the output image path. | |
StringPtr outputPNG = sb->ToString(); | |
// Convert worksheet to png image. | |
sr->ToImage(i, outputPNG); | |
} |
TIFF Formato
Consulte el siguiente código de ejemplo, suejemplo de archivo de Excel , y elSalida TIFF Imagen.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
// Source directory path. | |
StringPtr srcDir = new String("..\\Data\\01_SourceDirectory\\"); | |
// Output directory path. | |
StringPtr outDir = new String("..\\Data\\02_OutputDirectory\\"); | |
// Path of input Excel file. | |
StringPtr sampleConvertingWorksheetToDifferentImageFormats = srcDir->StringAppend(new String("sampleConvertingWorksheetToDifferentImageFormats.xlsx")); | |
// Create an empty workbook. | |
intrusive_ptr<Aspose::Cells::IWorkbook> workbook = Factory::CreateIWorkbook(sampleConvertingWorksheetToDifferentImageFormats); | |
// Access first worksheet. | |
intrusive_ptr<Aspose::Cells::IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
// Create image or print options object. | |
intrusive_ptr<Aspose::Cells::Rendering::IImageOrPrintOptions> imgOptions = Factory::CreateIImageOrPrintOptions(); | |
// Specify the image format. | |
imgOptions->SetImageFormat(Aspose::Cells::Systems::Drawing::Imaging::ImageFormat::GetTiff()); | |
// Specify horizontal and vertical resolution | |
imgOptions->SetHorizontalResolution(200); | |
imgOptions->SetVerticalResolution(200); | |
// Render the sheet with respect to specified image or print options. | |
intrusive_ptr<Aspose::Cells::Rendering::ISheetRender> sr = Factory::CreateISheetRender(worksheet, imgOptions); | |
// Get the output image path. | |
StringPtr outputTiff = outDir->Append((StringPtr)new String("outputConvertingWorksheetToImageTiff.tiff")); | |
// Convert worksheet to tiff image. | |
sr->ToTiff(outputTiff); |
Conversión de hoja de trabajo 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 sido desarrollado por el World Wide Web Consortium (W3C) desde 1999.
Aspose.Cells for C++ ha podido convertir hojas de trabajo a SVG imagen desde la versión 18.5.0.
Para usar esta función, importe el espacio de nombres Aspose.Cells.Rendering
a su programa o proyecto. Tiene varias clases valiosas para renderizar e imprimir, por ejemplo, ISheetRender
, IImageOrPrintOptions
y otras.
La clase Aspose.Cells.Rendering.IImageOrPrintOptions
especifica que la hoja de trabajo se guardará en formato SVG. El siguiente fragmento de código muestra cómo convertir una hoja de trabajo en un archivo de Excel a un archivo de imagen SVG
Consulte el siguiente código de ejemplo, suejemplo de archivo de Excel , y elSalida SVG Imágenes.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
// Source directory path. | |
StringPtr srcDir = new String("..\\Data\\01_SourceDirectory\\"); | |
// Output directory path. | |
StringPtr outDir = new String("..\\Data\\02_OutputDirectory\\"); | |
// Path of input Excel file. | |
StringPtr sampleConvertingWorksheetToDifferentImageFormats = srcDir->StringAppend(new String("sampleConvertingWorksheetToDifferentImageFormats.xlsx")); | |
// Create an empty workbook. | |
intrusive_ptr<Aspose::Cells::IWorkbook> workbook = Factory::CreateIWorkbook(sampleConvertingWorksheetToDifferentImageFormats); | |
// Access first worksheet. | |
intrusive_ptr<Aspose::Cells::IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
// Create image or print options object. | |
intrusive_ptr<Aspose::Cells::Rendering::IImageOrPrintOptions> imgOptions = Factory::CreateIImageOrPrintOptions(); | |
// Specify horizontal and vertical resolution | |
imgOptions->SetHorizontalResolution(200); | |
imgOptions->SetVerticalResolution(200); | |
// Specify the save format. | |
imgOptions->SetSaveFormat(Aspose::Cells::SaveFormat::SaveFormat_SVG); | |
// Render the sheet with respect to specified image or print options. | |
intrusive_ptr<Aspose::Cells::Rendering::ISheetRender> sr = Factory::CreateISheetRender(worksheet, imgOptions); | |
// Get page count. | |
Aspose::Cells::Systems::Int32 pageCount = sr->GetPageCount(); | |
// Create string builder object for string concatenations. | |
intrusive_ptr<Aspose::Cells::Systems::Text::StringBuilder> sb = new Aspose::Cells::Systems::Text::StringBuilder(); | |
// 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->Clear(); | |
sb->Append(outDir); | |
sb->Append((StringPtr)new String("outputConvertingWorksheetToImageSVG_")); | |
sb->Append(i); | |
sb->Append((StringPtr)new String(".svg")); | |
// Get the output image path. | |
StringPtr outputSVG = sb->ToString(); | |
// Convert worksheet to tiff image. | |
sr->ToImage(i, outputSVG); | |
} |