ワークシートを異なる画像形式に変換

ワークシートをイメージに変換

ワークシートには分析したいデータが含まれています。 例えば、ワークシートにはパラメータ、合計、パーセンテージ、例外、計算などが含まれることがあります。

開発者として、ワークシートを画像として表示する必要があるかもしれません。 たとえば、ワークシートの画像をアプリケーションやWebページで使用する必要があるかもしれません。 Microsoft Word文書、PDFファイル、PowerPointプレゼンテーション、またはその他のドキュメントタイプに画像を挿入したいかもしれません。 要するに、ワークシートを他の場所で使用できるように画像として描画したいのです。

Aspose.CellsはExcelワークシートを画像に変換する機能をサポートしています。 この機能を使用するには、プログラムまたはプロジェクトにAspose.Cells.Rendering名前空間をインポートする必要があります。 レンダリングと印刷用の価値あるクラスが含まれており、たとえば、SheetRenderImageOrPrintOptionsなどがあります。

Aspose.Cells.Rendering.ISheetRenderクラスは、画像としてレンダリングするワークシートを表します。 画像ファイルにワークシートを変換するToImageなどのオーバーロードされたメソッドがあり、BMP、PNG、GIF、JPG、JPEG、TIFF、EMFなどのさまざまな画像形式がサポートされています。

次のコードスニペットは、Excelファイルのワークシートを画像ファイルに変換する方法を示しています。

PNGフォーマット

次のサンプルコードと、参照用のサンプルExcelファイル出力PNGイメージをご覧ください。

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();

TIFFフォーマット

次のサンプルコードと、参照用のサンプルExcelファイル出力TIFFイメージをご覧ください。

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();

ワークシートをSVGに変換

SVGはScalable Vector Graphicsの略です。 SVGは、二次元ベクトルグラフィックのためのXML標準に基づいた仕様です。 1999年以来、World Wide Web Consortium(W3C)によって開発されてきたオープンな標準です。

バージョン18.5.0以降、Aspose.Cells for C++はワークシートをSVG画像に変換することができるようになりました。

この機能を使用するには、プログラムまたはプロジェクトにAspose.Cells.Rendering名前空間をインポートする必要があります。 レンダリングと印刷用の価値あるクラスが含まれており、例えばISheetRenderIImageOrPrintOptionsなどがあります。

Aspose.Cells.Rendering.IImageOrPrintOptionsクラスは、ワークシートをSVG形式で保存することを指定します。 次のコードスニペットは、ExcelファイルのワークシートをSVGイメージファイルに変換する方法を示しています。

次のサンプルコードと、参照用のサンプルExcelファイル出力SVGイメージをご覧ください。

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();