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 1 year has passed since last update.

C#を使用してExcelに透かしを追加する方法

Last updated at Posted at 2022-04-02

Microsoft Excelには、Excelテーブルに透かしを直接追加する組み込み関数がないことはわかっていますが、実際には、ヘッダー画像やワードアートを追加して透かしの外観を模倣するなど、他の回避策を使用してこの問題を解決できます。 したがって、この投稿では、Excelにヘッダー画像を作成して挿入することにより、Excelに透かしを追加する方法を紹介します。
ここでは、Excel用の無料コンポーネントであるFree Spire.XLSをダウンロードする必要があります。これにより、時間を節約し、コードを簡素化できます。コンポーネントがインストールされたら、プロジェクトを作成し、インストールディレクトリにプロジェクトへの参照としてdllファイルを追加し、次の名前空間を追加します。

using System;
using System.Drawing;
using System.Windows.Forms;
using Spire.Xls;

元のExcelシートのスクリーンショットは次のとおりです。
01.png

詳細な手順とサンプルコード

ステップ1:最初にDrawText()メソッドを定義し、文字列の内容に基づいて画像を作成します。文字列は、「機密」、「ドラフト」、「サンプル」、または透かしとして表示する任意のテキストにすることができます。

private static System.Drawing.Image DrawText(String text, System.Drawing.Font font, Color textColor, Color backColor, double height, double width)
{
    //指定された幅と高さのビットマップ画像を作成する
    Image img = new Bitmap((int)width, (int)height);
    Graphics drawing = Graphics.FromImage(img);
    //テキストサイズを取得する
    SizeF textSize = drawing.MeasureString(text, font);
    //画像を回転する
    drawing.TranslateTransform(((int)width - textSize.Width) / 2, ((int)height - textSize.Height) / 2);
    drawing.RotateTransform(-45);
    drawing.TranslateTransform(-((int)width - textSize.Width) / 2, -((int)height - textSize.Height) / 2);
    //背景を描く
    drawing.Clear(backColor);
    //テキストブラシを作成する
    Brush textBrush = new SolidBrush(textColor);
    drawing.DrawString(text, font, textBrush, ((int)width - textSize.Width) / 2, ((int)height - textSize.Height) / 2);
    drawing.Save();
    return img;
}

ステップ2:新しいブックを初期化し、透かしを入れたファイルをロードします。

Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.xlsx");

ステップ3:DrawText()メソッドを呼び出して新しい画像を作成し、ヘッダー画像を左揃えに設定します。次に、ヘッダー画像は表示モードがレイアウトの場合にのみ表示されるため、表示モードをレイアウトに変更することを忘れないでください。

Font font = new System.Drawing.Font("arial", 40);
String watermark = "内部用資料";
foreach (Worksheet sheet in workbook.Worksheets)
{
    //DrawText()メソッドを呼び出して、新しい画像を作成する
    System.Drawing.Image imgWtrmrk = DrawText(watermark, font, System.Drawing.Color.LightCoral, System.Drawing.Color.White, sheet.PageSetup.PageHeight, sheet.PageSetup.PageWidth);

    //ヘッダー画像を左揃えにする
    sheet.PageSetup.LeftHeaderImage = imgWtrmrk;
    sheet.PageSetup.LeftHeader = "&G";

    //透かしはこのモードでのみ表示されます
    sheet.ViewMode = ViewMode.Layout;
}

ステップ4:ファイルを保存して開きます。

workbook.SaveToFile("透かし.xlsx", ExcelVersion.Version2010);
System.Diagnostics.Process.Start("透かし.xlsx");

追加した結果は以下のように:
02.png

完全なるコード

using System;
using System.Drawing;
using System.Windows.Forms;
using Spire.Xls;

namespace Add_Watermark_To_Excel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //新しいブックを初期化し、透かしを入れるファイルをロードする
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.xlsx");
            //ヘッダーに画像を挿入する
            Font font = new System.Drawing.Font("arial", 40);
            String watermark = "内部用資料";
            foreach (Worksheet sheet in workbook.Worksheets)
            {
                //DrawText()メソッドを呼び出して、新しい画像を作成する
                System.Drawing.Image imgWtrmrk = DrawText(watermark, font, System.Drawing.Color.LightCoral, System.Drawing.Color.White, sheet.PageSetup.PageHeight, sheet.PageSetup.PageWidth);
                //ヘッダー画像を左揃えにする
                sheet.PageSetup.LeftHeaderImage = imgWtrmrk;
                sheet.PageSetup.LeftHeader = "&G";
                //透かしはこのモードでのみ表示される
                sheet.ViewMode = ViewMode.Layout;
            }
            workbook.SaveToFile("透かし.xlsx", ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("透かし.xlsx");
        }
        private static System.Drawing.Image DrawText(String text, System.Drawing.Font font, Color textColor, Color backColor, double height, double width)
        {
            //指定された幅と高さのビットマップ画像を作成する
            Image img = new Bitmap((int)width, (int)height);
            Graphics drawing = Graphics.FromImage(img);
            //テキストサイズを取得する
            SizeF textSize = drawing.MeasureString(text, font);
            //画像を回転する
            drawing.TranslateTransform(((int)width - textSize.Width) / 2, ((int)height - textSize.Height) / 2);
            drawing.RotateTransform(-45);
            drawing.TranslateTransform(-((int)width - textSize.Width) / 2, -((int)height - textSize.Height) / 2);
            //背景を描く
            drawing.Clear(backColor);
            //テキストブラシを作成する
            Brush textBrush = new SolidBrush(textColor);
            drawing.DrawString(text, font, textBrush, ((int)width - textSize.Width) / 2, ((int)height - textSize.Height) / 2);
            drawing.Save();
            return img;
        }
    }
}

今回の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?