如您所知,Excel 没有水印的概念。将形状或图像添加到工作表的问题在于它会浮在单元格上。如果您希望单元格后面出现某些内容,最好的方法是在页眉或页脚中放置一个大图像,如本 MS 文章中所述:http: //office.microsoft.com/en-us/excel-help/模仿-a-watermark-in-excel-HP010103239.aspx
要使用 ExcelWriter API 以编程方式执行此操作,您可以使用HeaderFooterSection 对象的SetContent 方法。您还可以使用HeaderFooterPicture 对象调整图像的尺寸。
例子:
ExcelApplication xla = new ExcelApplication();
Workbook wb = xla.Create(ExcelApplication.FileFormat.Xlsx);
Worksheet sheet1 = wb.Worksheets[0];
PageSetup pgsetup = sheet1.PageSetup;
HeaderFooterSection centerFooter = pgsetup.GetFooter(HeaderFooterSection.Section.Center);
string watermarkImagePath = @"C:\images\watermark.jpg";
using (FileStream fs = File.Open(watermarkImagePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
//Note: Excel requires including the "&G" code when inserting
//an image in a header or footer
centerFooter.SetContent("&G",fs);
}
xla.Save(wb, Page.Response, "watermarktest.xlsx", false);
有关格式化页眉和页脚的更多信息:http ://wiki.softartisans.com/display/EW8/Formatting+Headers+and+Footers