LoginSignup
1
1

More than 5 years have passed since last update.

C#でExcelにグラフィックを挿入する

Last updated at Posted at 2018-09-17

Spire.XLSコンポーネントは、Excelに付属の幅広いカスタムグラフィックスをカバーする最大186のグラフィックタイプをサポートしています。 この記事では、Spire.XLSを使用してExcelドキュメントにグラフィックスを挿入し、グラフィックスにテキストを追加し、グラフィックスで色とイメージを塗りつぶす方法について説明します。
使用する必要のあるツール:Spire.XLS for .NET; Visual Studio.
【C#】

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spire.Xls;
using Spire.Xls.Core;
using System.Drawing;

namespace Insert_shapes_in_excel
{
    class Program
    {
        static void Main(string[] args)
        {
            //ブックインスタンスを作成する
            Workbook workbook = new Workbook();
            //最初のワークシートを取得する
            Worksheet sheet = workbook.Worksheets[0];

            //三角形を追加し、その位置、幅、高さを指定する
            IPrstGeomShape triangle = sheet.PrstGeomShapes.AddPrstGeomShape(2, 2, 100, 100, PrstGeomShapeType.Triangle);
            //三角形を無地の色で塗りつぶす
            triangle.Fill.ForeColor = Color.Yellow;
            triangle.Fill.FillType = ShapeFillType.SolidColor;
            //テキストを追加
            triangle.Text = "テキスト";

            //心臓を追加し、その位置、幅、高さを指定する
            IPrstGeomShape heart = sheet.PrstGeomShapes.AddPrstGeomShape(2, 5, 100, 100, PrstGeomShapeType.Heart);
            //グラデーションで心臓を満たす
            heart.Fill.ForeColor = Color.Red;
            heart.Fill.FillType = ShapeFillType.Gradient;

            //デフォルトの色で塗りつぶす矢印を追加する
            IPrstGeomShape arrow = sheet.PrstGeomShapes.AddPrstGeomShape(10, 2, 100, 100, PrstGeomShapeType.CurvedRightArrow);

            //クラウドを追加し、その位置、幅、高さを指定する
            IPrstGeomShape cloud = sheet.PrstGeomShapes.AddPrstGeomShape(10, 5, 100, 100, PrstGeomShapeType.Cloud);
            //カスタム画像で雲を埋める
            cloud.Fill.CustomPicture(Image.FromFile("ピクチャ.jpg"), "ピクチャ.jpg");
            cloud.Fill.FillType = ShapeFillType.Picture;

            //ドキュメントを保存                       
            workbook.SaveToFile("シェイプを追加.xlsx", ExcelVersion.Version2013);
        }
    }
}

デバッグしてコードを実行すると、生成されたドキュメントが以下に表示されます:
1.jpg

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1