如何使用softartisans ExcelWriter模仿excel中的水印?如果是这样,我们有任何示例代码吗?

时间:2014-02-04 12:29:35

标签: officewriter

示例代码:

ExcelApplication xla = new ExcelApplication();
Workbook wb = xla.Create();
Worksheet ws = wb.Worksheets[0];
Shapes shps = ws.Shapes;
Anchor anch = ws.CreateAnchor(0, 7, 0, 0);
Shape shpHeart = shps.CreateShape(ShapeType.TextBox, anch);
shpHeart.FillTransparency=0.5;

1 个答案:

答案 0 :(得分:1)

如您所知,Excel没有水印的概念。向工作表添加形状或图像的问题是它将浮动在单元格上。如果您希望在单元格后面显示某些内容,最好的方法是在页眉或页脚中放置一个大图像,如本文MS文章中所述:http://office.microsoft.com/en-us/excel-help/mimic-a-watermark-in-excel-HP010103239.aspx

要使用ExcelWriter API以编程方式执行此操作,您可以使用SetContent methodHeaderFooterSection object。您还可以使用HeaderFooterPicture object调整图像的尺寸。

示例:

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