有时我们需要在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