有时我们需要在Excel工作表中添加水印来达到保护文档的目的。该文将介绍如何使用C# 在Excel表格中添加自定义页眉,再添加图片或艺术字来模拟Excel水印。Excel 水印在正常模式下不可见,仅在页面布局模式或打印预览模式才可见。
效果图:
C#
using Spire.Xls;
using System.Drawing;
using System;
namespace AddWaterMarktoExcel
{
class Program
{
static void Main(string[] args)
{
//加载示例文档
Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");
//设置文本和字体大小
Font font = new System.Drawing.Font("仿宋", 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";
//将显示模式设置为Layout
sheet.ViewMode = ViewMode.Layout;
}
workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);
System.Diagnostics.Process.Start("result.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);
//获取文本size
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;
}
}
}
VB.NET
Imports Spire.Xls
Imports System.Drawing
Namespace AddWaterMarktoExcel
Class Program
Private Shared Sub Main(args As String())
'加载示例文档
Dim workbook As New Workbook()
workbook.LoadFromFile("Sample.xlsx")
'设置文本和字体大小
Dim font As Font = New System.Drawing.Font("仿宋", 40)
Dim watermark As [String] = "内部文档"
For Each sheet As Worksheet In workbook.Worksheets
'调用DrawText() 方法插入图片
Dim imgWtrmrk As System.Drawing.Image = 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"
'将显示模式设置为Layout
sheet.ViewMode = ViewMode.Layout
Next
workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010)
System.Diagnostics.Process.Start("result.xlsx")
End Sub
Private Shared Function DrawText(text As [String], font As System.Drawing.Font, textColor As Color, backColor As Color, height As Double, width As Double) As System.Drawing.Image
'定义图片宽度和告诉
Dim img As Image = New Bitmap(CInt(width), CInt(height))
Dim drawing As Graphics = Graphics.FromImage(img)
'获取文本size
Dim textSize As SizeF = drawing.MeasureString(text, font)
'文本显示样式及位置
drawing.TranslateTransform((CInt(width) - textSize.Width) / 2, (CInt(height) - textSize.Height) / 2)
drawing.RotateTransform(-45)
drawing.TranslateTransform(-(CInt(width) - textSize.Width) / 2, -(CInt(height) - textSize.Height) / 2)
drawing.Clear(backColor)
Dim textBrush As Brush = New SolidBrush(textColor)
drawing.DrawString(text, font, textBrush, (CInt(width) - textSize.Width) / 2, (CInt(height) - textSize.Height) / 2)
drawing.Save()
Return img
End Function
End Class
End Namespace