ImageOrPrintオプションを使用してワークシートを画像に変換する
ワークシートをイメージとして保存する - 異なるアプローチ
時には、ワークシートを画像として提示したい場合があります。アプリケーションやWebページにワークシートの画像を挿入したいときです。これには、Word文書やPDF、PowerPointに画像を挿入したり、他のシナリオで活用したりできます。単純に、ワークシートを画像としてレンダリングし、それを他の場所で使用できるようにしたいのです。Aspose.Cells for Python via .NETは、Excelのワークシートを画像に変換することをサポートしています。また、画像のフォーマット、解像度(縦横両方)、画質などさまざまなオプションの設定も可能です。
Office Automationを試すことができますが、Office Automationには独自の欠点があります。セキュリティ、安定性、拡張性、速度、価格、機能など、いくつかの理由や問題があります。要するに、Microsoft自身もソフトウェアソリューションからのOffice Automationを強く勧めていません。
この資料では、Visual Studio .NETでコンソールアプリケーションを作成し、複数の画像と印刷オプションを使ってワークシートを画像に変換する方法を、最も簡単なコード例とともに紹介します。Aspose.Cells for Python via .NET APIを使用します。
あなたはあなたのプログラム/プロジェクトにaspose.cells.rendering名前空間をインポートする必要があります。例えば、SheetRender、ImageOrPrintOptions、WorkbookRenderなどの価値のあるクラスがいくつかあります。
SheetRenderクラスは、イメージを描画するためのワークシートを表し、あなたが望む属性やオプションでワークシートをイメージファイルに直接変換できるオーバーロードされたto_imageメソッドを持っています。それはSystem.Drawing.Bitmapオブジェクトを返すことができ、イメージファイルをディスク/ストリームに保存することができます。サポートされるイメージ形式はいくつかあります、例えばBMP、PNG、GIFF、JPEG、TIFF、EMFなどです。
Aspose.Cellsを使用してワークシートを画像に変換する(ImageOrPrint オプションを使用)
Microsoft Excelでテンプレートワークブックを作成する
私はMS Excelで新しいワークブックを作成し、最初のワークシートにいくつかのデータを追加しました。これで、テンプレートファイルのワークシート Sheet1 を画像ファイル SheetImage.tiff に変換し、水平および垂直解像度、TiffCompressionなどの異なるイメージオプションを適用します。
ワークシートを画像ファイルに変換
from aspose.cells import PrintingPageType, Workbook | |
from aspose.cells.drawing import ImageType | |
from aspose.cells.rendering import ImageOrPrintOptions, SheetRender, TiffCompression | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Source directory | |
sourceDir = RunExamples.Get_SourceDirectory() | |
# Output directory | |
outputDir = RunExamples.Get_OutputDirectory() | |
# Open template | |
book = Workbook(sourceDir + "sampleWorksheetToAnImage.xlsx") | |
# Get the first worksheet | |
sheet = book.worksheets[0] | |
# Apply different Image and Print options | |
options = ImageOrPrintOptions() | |
# Set Horizontal Resolution | |
options.horizontal_resolution = 300 | |
# Set Vertical Resolution | |
options.vertical_resolution = 300 | |
# Set TiffCompression | |
options.tiff_compression = TiffCompression.COMPRESSION_LZW | |
# Set Image Format | |
options.image_type = ImageType.TIFF | |
# Set printing page type | |
options.printing_page = PrintingPageType.DEFAULT | |
# Render the sheet with respect to specified image/print options | |
sr = SheetRender(sheet, options) | |
# Render/save the image for the sheet | |
pageIndex = 3 | |
sr.to_image(pageIndex, outputDir + r"outputWorksheetToAnImage_" + str(pageIndex + 1) + ".tiff") |
WorkbookRenderを使用した画像変換
TIFF画像には複数のフレームを含めることができます。複数フレームまたはページを持つTIFF画像としてワークブック全体を保存できます。
from aspose.cells import Workbook | |
from aspose.cells.drawing import ImageType | |
from aspose.cells.rendering import ImageOrPrintOptions, WorkbookRender | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Source directory | |
sourceDir = RunExamples.Get_SourceDirectory() | |
# Output directory | |
outputDir = RunExamples.Get_OutputDirectory() | |
wb = Workbook(sourceDir + "sampleUseWorkbookRenderForImageConversion.xlsx") | |
opts = ImageOrPrintOptions() | |
opts.image_type = ImageType.TIFF | |
wr = WorkbookRender(wb, opts) | |
wr.to_image(outputDir + "outputUseWorkbookRenderForImageConversion.tiff") |