How to add watermark to Excel in C#

In the era of big data, data is particularly important for companies and individuals. As a common tool for editing and processing data, Excel is also widely used in various occasions and office environments such as business or government affairs. When we want to declare or protect some important worksheet data, we can protect it by certain methods, such as encrypting the document or adding a watermark. We know that you can add text or image watermarks in Word, but in Excel, we can't add watermarks directly. So what to do? Although we can't add it directly in the Excel file, in C#, we can do it in code form. Here, I found a nice way to add Excel watermark using the group Spire.XLS for .NET. The article is reproduced from http://www.cnblogs.com/Yesi/p/5915251.html .

Here is the entire code:

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)
        {
            //Initialize a new workbook and load the file to be watermarked
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.xlsx");
            // insert image in header
            Font font = new System.Drawing.Font("arial", 40);
            String watermark = "Internal data";
            foreach (Worksheet sheet in workbook.Worksheets)
            {
                //Call the DrawText() method to create a new image
                System.Drawing.Image imgWtrmrk = DrawText(watermark, font, System.Drawing.Color.LightCoral, System.Drawing.Color.White, sheet.PageSetup.PageHeight, sheet.PageSetup.PageWidth);
                //Set the header image to left-align
                sheet.PageSetup.LeftHeaderImage = imgWtrmrk;
                sheet.PageSetup.LeftHeader = "&G";
                //The watermark will only appear in this mode
                sheet.ViewMode = ViewMode.Layout;
            }
            workbook.SaveToFile("水印.xlsx", ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("水印.xlsx");
        }
       <br>       private static System.Drawing.Image DrawText(String text, System.Drawing.Font font, Color textColor, Color backColor, double height, double width)
        {
            //Create a bitmap image with specified width and height
            Image img = new Bitmap((int)width, (int)height);
            Graphics drawing = Graphics.FromImage(img);
            //get text size
            SizeF textSize = drawing.MeasureString(text, font);
            //rotate the image
            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);
            // draw background
            drawing.Clear(backColor);
            //create text brush
            Brush textBrush = new SolidBrush(textColor);
            drawing.DrawString(text, font, textBrush, ((int)width - textSize.Width) / 2, ((int)height - textSize.Height) / 2);
            drawing.Save();
            return img;
        }
    }  
}

 Comparison renderings:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326545744&siteId=291194637