你需要爲 Excel 工作表添加水印嗎?使用Aspose在C#中輕鬆實現

水印是保護 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),我們很高興爲您提供查詢和諮詢

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章