- 保护Excel VBA宏
为了给 Excel 工作表添加水印,我们将使用Aspose.Cells for .NET。它是一个功能强大且易于使用的 API,可让您创建、修改或转换 Excel 文件。
在 C# 中为 Excel 工作表添加水印
以下是使用 C# 在 Excel 工作表中添加水印的步骤。
- 通过提供其路径,使用Workbook类加载 Excel 文件。
- 获取要添加水印的工作表的参考。
- 通过向工作表添加新形状并设置其文本和属性来创建水印
- 将Shape.IsLocked属性设置为 true 以锁定水印。
- 使用Shape.SetLockedProperty(ShapeLockType, bool)方法设置其他锁类型。
- 使用Workbook.Save(String)方法保存更新的 Excel 文件。
以下代码示例展示了如何向 Excel 工作表添加水印。
// Instantiate a new WorkbookWorkbook workbook = new Workbook("Excel.xlsx");// Get the first default sheetWorksheet sheet = workbook.Worksheets[0];// Add watermarkAspose.Cells.Drawing.Shape wordart = sheet.Shapes.AddTextEffect(MsoPresetTextEffect.TextEffect1,"CONFIDENTIAL", "Arial Black", 50, false, true, 18, 8, 1, 1, 130, 800);// Lock shape aspectswordart.IsLocked = true;wordart.SetLockedProperty(ShapeLockType.Selection, true);wordart.SetLockedProperty(ShapeLockType.ShapeType, true);wordart.SetLockedProperty(ShapeLockType.Move, true);wordart.SetLockedProperty(ShapeLockType.Resize, true);wordart.SetLockedProperty(ShapeLockType.Text, true);// Get the fill format of the word artFillFormat wordArtFormat = wordart.Fill;// Set the transparencywordArtFormat.Transparency = 0.9; // Save the watermarked Excel fileworkbook.Save("Watermarked-locked.xlsx");
以下是添加水印后的Excel工作表截图。

如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。
还想要更多吗可以点击阅读【Aspose最新资源在线文库】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!