Conversione del foglio di lavoro in diversi formati di immagine
Conversione del foglio di lavoro in immagine
I fogli di lavoro contengono dati che si desidera analizzare. Ad esempio, un foglio di lavoro può contenere parametri, totali, percentuali, eccezioni e calcoli.
Come sviluppatore, potresti aver bisogno di presentare i fogli di lavoro come immagini. Ad esempio, potresti aver bisogno di utilizzare un’immagine di un foglio di lavoro in un’applicazione o una pagina web. Potresti voler inserire un’immagine in un documento di Microsoft Word, un file PDF, una presentazione PowerPoint o qualche altro tipo di documento. In poche parole, desideri un foglio di lavoro reso come immagine in modo che tu possa usarlo altrove.
Aspose.Cells supporta la conversione dei fogli di lavoro di Excel in immagini. Per utilizzare questa funzione, è necessario importare il namespace Aspose.Cells.Rendering nel proprio programma o progetto. Ha diverse classi preziose per il rendering e la stampa, ad esempio SheetRender, ImageOrPrintOptions e altre.
La classe Aspose.Cells.Rendering.ISheetRender
rappresenta un foglio di lavoro da rendere come immagini. Ha un metodo sovraccaricato, ToImage, che può convertire un foglio di lavoro in file immagine con diversi attributi o opzioni. Sono supportati diversi formati di immagine, ad esempio, BMP, PNG, GIF, JPG, JPEG, TIFF, EMF.
Il seguente frammento di codice mostra come convertire un foglio di lavoro in un file immagine.
Formato PNG
Si prega di consultare il seguente codice di esempio, il file di Excel di esempio, e le immagini PNG di output.
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
Si prega di consultare il seguente codice di esempio, il file di Excel di esempio, e l'immagine TIFF di output.
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(); |
Conversione del foglio di lavoro in SVG
SVG sta per Scalable Vector Graphics. SVG è una specifica basata su standard XML per grafica vettoriale bidimensionale. È uno standard aperto che è stato in fase di sviluppo da parte del World Wide Web Consortium (W3C) dal 1999.
Aspose.Cells for C++ è stato in grado di convertire i fogli di lavoro in immagini SVG dalla versione 18.5.0 in poi.
Per utilizzare questa funzionalità, importare lo spazio dei nomi Aspose.Cells.Rendering
nel proprio programma o progetto. Ha diverse classi preziose per il rendering e la stampa, ad esempio, ISheetRender
, IImageOrPrintOptions
, e altre.
La classe Aspose.Cells.Rendering.IImageOrPrintOptions
specifica che il foglio di lavoro sarà salvato nel formato SVG. Il seguente snippet di codice mostra come convertire un foglio di lavoro in un file immagine SVG di Excel
Si prega di vedere il seguente codice di esempio, il file di Excel di esempio, e le immagini SVG di output.
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(); |