你需要为 Excel 工作表添加水印吗?使用Aspose在C#中轻松实现

582 阅读1分钟

水印是保护 Excel 文件不被非法使用的方法之一。通常,水印定义受版权保护的内容的所有权。在本文中,您将了解如何以编程方式向 Excel 文件添加水印。特别是,本文将介绍如何使用 C# 向 Excel 工作表添加水印。

  • 保护Excel VBA宏

为了给 Excel 工作表添加水印,我们将使用Aspose.Cells for .NET。它是一个功能强大且易于使用的 API,可让您创建、修改或转换 Excel 文件。.NET 文档开发管理都可以使用Aspose。(下载Aspose.Cells

在 C# 中为 Excel 工作表添加水印

以下是使用 C# 在 Excel 工作表中添加水印的步骤。

  • 通过提供其路径,使用Workbook类加载 Excel 文件。
  • 获取要添加水印的工作表的参考。
  • 通过向工作表添加新形状并设置其文本和属性来创建水印
  • 将Shape.IsLocked属性设置为 true 以锁定水印。
  • 使用Shape.SetLockedProperty(ShapeLockType, bool)方法设置其他锁类型。
  • 使用Workbook.Save(String)方法保存更新的 Excel 文件。

以下代码示例展示了如何向 Excel 工作表添加水印。

// Instantiate a new Workbook
Workbook workbook = new Workbook("Excel.xlsx");

// Get the first default sheet
Worksheet sheet = workbook.Worksheets[0];

// Add watermark
Aspose.Cells.Drawing.Shape wordart = sheet.Shapes.AddTextEffect(MsoPresetTextEffect.TextEffect1,
"CONFIDENTIAL", "Arial Black", 50, false, true
, 18, 8, 1, 1, 130, 800);

// Lock shape aspects
wordart.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 art
FillFormat wordArtFormat = wordart.Fill;

// Set the transparency
wordArtFormat.Transparency = 0.9; 

// Save the watermarked Excel file
workbook.Save("Watermarked-locked.xlsx");

以下是添加水印后的Excel工作表截图。

.NET Excel处理开发工具Aspose.Cells功能演示:在C#中 为 Excel 工作表添加水印

如果您有任何疑问或需求,请随时加入Aspose技术交流群(761297826),我们很高兴为您提供查询和咨询。