0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C#を使用してExcelシートに背景画像を挿入する方法

Last updated at Posted at 2022-04-06

日常生活で、人々は常にMS Excelを使用して仕事を完成させます。通常、MS Excelを開くと、その中のワークシートはすべて空白な背景でけっこう単調なものです。もちろん、MS Excelは画像をワークブックに挿入してその背景として設置することができます。しかし、Microsoft Officeがインストールされていない場合、Excelファイルに素敵な背景画像を設定したいならどうすればいいでしょうか?この機能を実現するため、次はC#で無料のコントロールであるFree Spire.XLS for .NETを使用する方法を紹介します。

追加する必要のある名前空間:

using System.Drawing;
using Spire.Xls;

元のファイルのスクリーンショット:
01.png

詳細な手順とコード

ステップ1:新しいブックオブジェクトを作成します。

Workbook workbook = new Workbook();

ステップ2:システムからExcelファイルをロードします。

workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx");

ステップ3:例としてExcelファイルの一番目のテーブルを選択します。

Worksheet sheet = workbook.Worksheets[0];

ステップ4:画像をロードします。

Bitmap bm = new Bitmap(System.Drawing.Image.FromFile("C:\\Users\\Administrator\\Desktop\\image.jpg"));

ステップ5:この画像を背景として設定します。

sheet.PageSetup.BackgoundImage = bm;

ステップ6:Excelファイルを保存し、そして開きます。

workbook.SaveToFile("result.xlsx");
System.Diagnostics.Process.Start("result.xlsx");

挿入した結果は以下のようになります:
02.png

完全なるコード一覧

using System.Drawing;
using Spire.Xls;

namespace add background image
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx");
            Worksheet sheet = workbook.Worksheets[0];
            Bitmap bm = new Bitmap(System.Drawing.Image.FromFile("C:\\Users\\Administrator\\Desktop\\image.jpg"));
            sheet.PageSetup.BackgoundImage = bm;
            workbook.SaveToFile("result.xlsx");
            System.Diagnostics.Process.Start("result.xlsx");
        }
    }
}

今回のExcelシートに背景画像を挿入する方法は以上で終わります、最後まで読んでいただきありがとうございました。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?