Excel チャートを画像に変換する

チャートをイメージに変換する

ここでは、円グラフと棒グラフを画像に変換しています。

円グラフを画像ファイルに変換する

まず、Microsoft Excelで円グラフを作成し、その後Aspose.Cellsで画像ファイルに変換します。この例のコードは、テンプレートとなるMicrosoft Excelファイルの円グラフに基づいてEMF画像を作成します。

出力: 円グラフ画像
todo:image_alt_text
  1. Microsoft Excelで円グラフを作成する:
    1. Microsoft Excelで新しいワークブックを開きます。
    2. ワークシートにデータを入力します。
    3. データに基づいて円グラフを作成します。
    4. ファイルを保存します。
入力ファイル
todo:image_alt_text
  1. Aspose.Cellsをダウンロードしてインストールします。
    1. ダウンロード Aspose.Cells for .NET
    2. 開発コンピュータにインストールします。

全てのAsposeコンポーネントは、最初にインストールした際に評価モードで動作します。評価モードには時間制限はなく、出力ドキュメントに透かしを挿入するだけです。

  1. プロジェクトを作成します。
    1. Visual Studio.Net を起動します。
    2. 新しいコンソールアプリケーションを作成します。この例ではC#コンソールアプリケーションを使用していますが、VB.NETを使用することもできます。
    3. 参照を追加します。このプロジェクトではAspose.Cellsを使用するので、Aspose.Cellsへの参照を追加します。たとえば…\Program Files\Aspose\Aspose.Cells\Bin\Net1.0\Aspose.Cells.dll
    4. チャートを見つけて変換するコードを記述します。以下にコンポーネントがこのタスクを遂行するために使用するコードが示されています。ごく少数の行のコードが使用されます。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Open the existing excel file which contains the pie chart.
Workbook workbook = new Workbook(dataDir+ "PieChart.xlsx");
// Get the designer chart (first chart) in the first worksheet of the workbook.
Aspose.Cells.Charts.Chart chart = workbook.Worksheets[0].Charts[0];
// Convert the chart to an image file.
chart.ToImage(dataDir+ "PieChart.out.emf", ImageType.Emf);

棒グラフを画像ファイルに変換する

まずMicrosoft Excelで棒グラフを作成し、上記のように画像ファイルに変換します。サンプルコードを実行した後、テンプレートExcelファイルの棒グラフに基づいてJPEGファイルが作成されます。

出力ファイル: 棒グラフ画像
todo:image_alt_text
  1. Microsoft Excelで棒グラフを作成します:
    1. Microsoft Excelで新しいブックを開きます。
    2. ワークシートにデータを入力します。
    3. データに基づいて棒グラフを作成します。
    4. ファイルを保存します。
入力ファイル
todo:image_alt_text
  1. 上記のように説明した参照を含むプロジェクトを設定します。
  2. チャートを動的に画像に変換します。以下にコンポーネントがこのタスクを遂行するために使用するコードが示されています。このコードは前述のものと似ています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Open the existing excel file which contains the column chart.
Workbook workbook = new Workbook(dataDir+ "ColumnChart.xlsx");
// Get the designer chart (first chart) in the first worksheet of the workbook.
Aspose.Cells.Charts.Chart chart = workbook.Worksheets[0].Charts[0];
// Convert the chart to an image file.
chart.ToImage(dataDir+ "ColumnChart.out.jpeg", ImageType.Jpeg);