ImageOrPrintオプションを使用してワークシートを画像に変換する
ワークシートをイメージとして保存する - 異なるアプローチ
時々、ワークシートを図解的に表示する必要があります。ワークシートの画像をアプリケーションやWebページに挿入する必要があります。画像をWord文書、PDFファイル、PowerPointプレゼンテーションに挿入したり、他のシナリオで使用する必要があります。別の場所で使用するためにワークシートを画像としてレンダリングしたいと思うだけです。Aspose.CellsはExcelファイルのワークシートを画像に変換することをサポートしています。また、Aspose.Cellsは画像形式、解像度(縦と横の両方)、画像品質、およびその他の画像および印刷オプションを設定することをサポートしています。
Office Automationを試すことができますが、Office Automationには独自の欠点があります。セキュリティ、安定性、拡張性、速度、価格、機能など、いくつかの理由や問題があります。要するに、Microsoft自身もソフトウェアソリューションからのOffice Automationを強く勧めていません。
この記事は、Aspose.Cells APIを使用して、C#コンソールアプリケーションを作成し、わずかなコード行でさまざまなイメージと印刷オプションを使用してワークシートをイメージに変換する方法を示しています。
あなたはあなたのプログラム/プロジェクトにAspose.Cells.Rendering名前空間をインポートする必要があります。例えば、SheetRender、ImageOrPrintOptions、WorkbookRenderなどの価値のあるクラスがいくつかあります。
Aspose.Cells.Rendering.SheetRenderクラスは、イメージを描画するためのワークシートを表し、あなたが望む属性やオプションでワークシートをイメージファイルに直接変換できるオーバーロードされたToImageメソッドを持っています。それはSystem.Drawing.Bitmapオブジェクトを返すことができ、イメージファイルをディスク/ストリームに保存することができます。サポートされるイメージ形式はいくつかあります、例えばBMP、PNG、GIFF、JPEG、TIFF、EMFなどです。
ImageOrPrintオプションを使用して、Aspose.Cellsを使用してワークシートをイメージに変換する。
Microsoft Excelでテンプレートワークブックを作成する
私はMS Excelで新しいワークブックを作成し、最初のワークシートにいくつかのデータを追加しました。これで、テンプレートファイルのワークシート Sheet1 を画像ファイル SheetImage.tiff に変換し、水平および垂直解像度、TiffCompressionなどの異なるイメージオプションを適用します。
Aspose.Cellsをダウンロードしてインストールする
まず、.Net向けのAspose.Cellsをダウンロードします。開発コンピューターにインストールします。すべてのAsposeコンポーネントはインストールされると評価モードで動作します。評価モードには時限がなく、生成されたドキュメントにのみ透かしを挿入します。
プロジェクトを作成する
Visual Studio .Netを起動して、新しいコンソールアプリケーションを作成します。この例ではC#コンソールアプリケーションを示しますが、VB.NETも使用できます。
参照の追加
このプロジェクトではAspose.Cellsを使用します。そのため、プロジェクトにAspose.Cellsコンポーネントへの参照を追加する必要があります。例えば、….\Program Files\Aspose\Aspose.Cells for .NET\Bin\Net1.0\Aspose.Cells.dllに参照を追加します。
ワークシートを画像ファイルに変換
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
//Open template | |
Workbook book = new Workbook(sourceDir + "sampleWorksheetToAnImage.xlsx"); | |
// Get the first worksheet | |
Worksheet sheet = book.Worksheets[0]; | |
// Apply different Image and Print options | |
Aspose.Cells.Rendering.ImageOrPrintOptions options = new Aspose.Cells.Rendering.ImageOrPrintOptions(); | |
// Set Horizontal Resolution | |
options.HorizontalResolution = 300; | |
// Set Vertical Resolution | |
options.VerticalResolution = 300; | |
// Set TiffCompression | |
options.TiffCompression = Aspose.Cells.Rendering.TiffCompression.CompressionLZW; | |
// Set Image Format | |
options.ImageType = Drawing.ImageType.Tiff; | |
// Set printing page type | |
options.PrintingPage = PrintingPageType.Default; | |
// Render the sheet with respect to specified image/print options | |
SheetRender sr = new SheetRender(sheet, options); | |
// Render/save the image for the sheet | |
int pageIndex = 3; | |
sr.ToImage(pageIndex, outputDir + @"outputWorksheetToAnImage_"+ (pageIndex + 1) + ".tiff"); |
変換オプション
特定のページを画像に保存することが可能です。次のコードは、ワークブック内の最初と2番目のワークシートをJPG画像に変換します。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
// Open a template excel file | |
Workbook book = new Workbook(sourceDir + "sampleSpecificPagesToImages.xlsx"); | |
// Get the first worksheet. | |
Worksheet sheet = book.Worksheets[0]; | |
// Define ImageOrPrintOptions | |
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions(); | |
// Specify the image format | |
imgOptions.ImageType = Drawing.ImageType.Jpeg; | |
// Render the sheet with respect to specified image/print options | |
SheetRender sr = new SheetRender(sheet, imgOptions); | |
//Specify page index to be rendered | |
int idxPage = 3; | |
// Render the third image for the sheet | |
Bitmap bitmap = sr.ToImage(idxPage); | |
// Save the image file | |
bitmap.Save(outputDir + "outputSpecificPagesToImage_"+ (idxPage+1)+".jpg"); |
WorkbookRenderを使用した画像変換
TIFF画像には複数のフレームを含めることができます。複数フレームまたはページを持つTIFF画像としてワークブック全体を保存できます。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(sourceDir + "sampleUseWorkbookRenderForImageConversion.xlsx"); | |
ImageOrPrintOptions opts = new ImageOrPrintOptions(); | |
opts.ImageType = Drawing.ImageType.Tiff; | |
WorkbookRender wr = new WorkbookRender(wb, opts); | |
wr.ToImage(outputDir + "outputUseWorkbookRenderForImageConversion.tiff"); |