我们知道Microsoft Excel并没有内置的功能直接给Excel表添加水印,但是其实我们可以用其他变通的方式来解决此问题,如通过添加页眉图片或艺术字的方法来模仿水印的外观。所以在这篇文章中,我将向您演示来如何通过在Excel中创建和插入页眉图片来为excel添加水印。之前我也分享了如何给word文档添加水印和pdf文件添加水印的方法,有需要也可以参考。
这里我下载了一个E-iceblue公司开发的免费版的Excel组件- Free Spire.XLS,这样既节省时间,又简化了代码。
控件安装后,创建项目,添加安装目录下的dll文件作为项目的引用,并添加如下命名空间:
1
2
3
4
|
using
System;
using
System.Drawing;
using
System.Windows.Forms;
using
Spire.Xls;
|
这是原excel表的截图:
以下是详细步骤和代码片段:
步骤1:首先定义一个DrawText()方法,并在字符串的内容基础上创建一个图片。字符串可以是“机密”、“草稿”、“样品”或任何你想要显示为水印的文本。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
private
static
System.Drawing.Image DrawText(String text, System.Drawing.Font font, Color textColor, Color backColor,
double
height,
double
width) <br>{
//创建一个指定宽度和高度的位图图像
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:初始化一个新的工作簿并加载添加水印的文件。
1
2
|
Workbook workbook =
new
Workbook();
workbook.LoadFromFile(
@"C:\Users\Administrator\Desktop\sample.xlsx"
);
|
步骤3:调用DrawText()方法新建一个图片,并将页眉图片设置为左对齐。其次,因为在视图模式是布局的状态下页眉图片才会显示,所以一定要记得将视图模式改为布局。
1
2
3
4
5
6
7
8
9
10
11
12
|
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:保存并打开文件。
1
2
|
workbook.SaveToFile(
"水印.xlsx"
, ExcelVersion.Version2010);
System.Diagnostics.Process.Start(
"水印.xlsx"
);
|
效果图:
全部代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
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"
);
}
<br>
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;
}
}
}
|
感谢您的浏览,希望本文能给您带来一定的帮助。