ワークシートを画像に変換し、ページごとにワークシートを画像に変換

Aspose.Cellsを使用してワークシートを画像ファイルに変換する方法

この記事では、Visual Studioでコンソールアプリケーションを作成し、Aspose.Cells APIを使用してわずか数行のコードでワークシートを画像に変換し、複数のページを持つワークシートを1つの画像に変換する方法を示します。

プログラム/プロジェクトにAspose.Cells.Renderingネームスペースをインポートする必要があります。SheetRenderImageOrPrintOptionsWorkbookRenderなどの貴重なクラスがいくつか含まれています。Aspose.Cells.Rendering.SheetRenderクラスは、ワークシートをレンダリングするためのクラスであり、オーバーロードされたToImageメソッドから直接ワークシートを画像ファイルに変換できます。System.Drawing.Bitmapオブジェクトを返し、イメージファイルをディスク/ストリームに保存できます。省略して、BMP、PNG、GIF、JPG、JPEG、TIFF、EMFなど、さまざまな画像形式がサポートされています。

この記事では以下の方法について説明します:

  • ワークシートを画像に変換する
  • ワークシートの各ページを画像に変換する

このタスクでは、Aspose.Cellsを使用して、テンプレートワークブックからワークシートを画像ファイルに変換する方法を示します。

プロジェクトのセットアップ

  1. まず、Aspose.Cells for .NETをダウンロードしてください。
  2. 開発コンピュータにインストールしてください。Asposeのすべてのコンポーネントは、インストールされると評価モードで動作します。評価モードには時間制限はなく、生成された文書にウォーターマークが注入されます。Visual Studio.Netを起動し、新しいコンソールアプリケーションを作成します。この例ではC#のコンソールアプリケーションを使用していますが、VB.NETも使用できます。作成したプロジェクトにAspose.Cellsへの参照を追加してください。

ワークシートを画像ファイルに変換

Microsoft Excelで新しいワークブックを作成し、最初のワークシートにいくつかのデータを追加しました:Testbook.xlsx(1つのワークシート)。次に、テンプレートファイルのワークシートSheet1をSheetImage.jpgという画像ファイルに変換します。

コンポーネントがタスクを達成するために使用したコードは以下の通りです。Testbook.xlsxのSheet1を画像ファイルに変換し、この変換がどれほど簡単であるかを説明します。

// 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 + "sampleConvertWorksheettoImageFile.xlsx");
// Get the first worksheet.
Worksheet sheet = book.Worksheets[0];
// Define ImageOrPrintOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.OnePagePerSheet = true;
// 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);
// Render the image for the sheet
Bitmap bitmap = sr.ToImage(0);
// Save the image file
bitmap.Save(outputDir + "outputConvertWorksheettoImageFile.jpg");

Aspose.Cellsを使用して、ワークシートを画像ファイルにページごとに変換する

この例では、Aspose.Cellsを使用して、複数のページを持つテンプレートワークブックからワークシートを1つの画像ファイルに変換する方法を示します。

ワークシートをページ毎に画像に変換する

私はMicrosoft Excelで新しいワークブックを作成し、最初のワークシートにいくつかのデータを追加しました: Testbook2.xlsx (1 ワークシート)。

これで、テンプレートファイルのワークシート Sheet1 を画像ファイルに変換します(1ページごとのファイル)。すでにコンソールアプリケーションを作成してコピー作業を行う準備ができているため、コンソールアプリケーションの作成手順をスキップして、直接ワークシートの変換手順に移ります。

以下は、そのコンポーネントがタスクを達成するために使用したコードです。それは Testbook2.xls の Sheet1 をページごとに画像ファイルに変換します。

// 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();
Workbook book = new Workbook(sourceDir + "sampleConvertWorksheetToImageByPage.xlsx");
Worksheet sheet = book.Worksheets[0];
Aspose.Cells.Rendering.ImageOrPrintOptions options = new Aspose.Cells.Rendering.ImageOrPrintOptions();
options.HorizontalResolution = 200;
options.VerticalResolution = 200;
options.ImageType = Drawing.ImageType.Tiff;
// Sheet2Image By Page conversion
SheetRender sr = new SheetRender(sheet, options);
for (int j = 0; j < sr.PageCount; j++)
{
sr.ToImage(j, outputDir + "outputConvertWorksheetToImageByPage_" + (j + 1) + ".tif");
}