C#為Excel添加浮水印實例詳解
C#中如何為Excel添加浮水印
我們知道Microsoft Excel並沒有內建的功能直接為Excel表添加浮水印,但是其實我們可以用其他變通的方式來解決此問題,如透過添加頁眉圖片或藝術字的方法來模仿水印的外觀。所以在這篇文章中,我將向您示範如何透過在Excel中建立和插入頁眉圖片來為excel添加浮水印。之前我也分享如何為word文件添加浮水印和pdf文件添加浮水印的方法,有需要也可以參考。
這裡我下載了一個E-iceblue公司開發的免費版的Excel組件- Free Spire.XLS,這樣既節省時間,又簡化了程式碼。
控制安裝後,建立項目,新增安裝目錄下的dll檔案作為項目的引用,並加入以下命名空間:
using System; using System.Drawing; using System.Windows.Forms; using Spire.Xls;
這是原始excel表的截圖:
步驟與程式碼片段:
步驟1:先定義一個DrawText()方法,並在字串的內容基礎上建立一個圖片。字串可以是「機密」、「草稿」、「樣品」或任何你想要顯示為浮水印的文字。
private static System.Drawing.Image DrawText(String text, System.Drawing.Font font, Color textColor, Color backColor, double height, double width) <br>{ //创建一个指定宽度和高度的位图图像 Image img = new Bitmap((int)width, (int)height); Graphics drawing = Graphics.FromImage(img); //获取文本大小 SizeF textSize = drawing.MeasureString(text, font); //旋转图片 drawing.TranslateTransform(((int)width - textSize.Width) / 2, ((int)height - textSize.Height) / 2); drawing.RotateTransform(-45); drawing.TranslateTransform(-((int)width - textSize.Width) / 2, -((int)height - textSize.Height) / 2); //绘制背景 drawing.Clear(backColor); //创建文本刷 Brush textBrush = new SolidBrush(textColor); drawing.DrawString(text, font, textBrush, ((int)width - textSize.Width) / 2, ((int)height - textSize.Height) / 2); drawing.Save(); return img; }
步驟2:初始化一個新的工作簿並載入新增浮水印的檔案。
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:UsersAdministratorDesktopsample.xlsx");
步驟3:調用DrawText()方法新建一個圖片,並將頁眉圖片設定為左眉形步驟3:調用DrawText()方法新建一個圖片,並對齊頁眉圖片,並將調用DrawText()方法新建一個圖片,並對齊頁眉圖片3:並將調用DrawText()方法新建一個圖片,並將頁眉設為左眉。其次,因為在視圖模式是佈局的狀態下頁首圖片才會顯示,所以一定要記得將視圖模式改為佈局。
Font font = new System.Drawing.Font("arial", 40); String watermark = "内部资料"; foreach (Worksheet sheet in workbook.Worksheets) { //调用DrawText()方法新建图片 System.Drawing.Image imgWtrmrk = DrawText(watermark, font, System.Drawing.Color.LightCoral, System.Drawing.Color.White, sheet.PageSetup.PageHeight, sheet.PageSetup.PageWidth); //将页眉图片设置为左对齐 sheet.PageSetup.LeftHeaderImage = imgWtrmrk; sheet.PageSetup.LeftHeader = "&G"; //水印只会在此种模式下显现 sheet.ViewMode = ViewMode.Layout; }
步驟4:儲存並開啟檔案。
workbook.SaveToFile("浮水印.xlsx", ExcelVersion.Version2010);
System.Diagnostics.Process.Start("水印.xlsx");
效果圖:水印.xlsx");
using System; using System.Drawing; using System.Windows.Forms; using Spire.Xls; namespace Add_Watermark_To_Excel { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //初始化一个新工作簿并加载要添加水印的文件 Workbook workbook = new Workbook(); workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.xlsx"); //在页眉插入图片 Font font = new System.Drawing.Font("arial", 40); String watermark = "内部资料"; foreach (Worksheet sheet in workbook.Worksheets) { //调用DrawText()方法新建图片 System.Drawing.Image imgWtrmrk = DrawText(watermark, font, System.Drawing.Color.LightCoral, System.Drawing.Color.White, sheet.PageSetup.PageHeight, sheet.PageSetup.PageWidth); //将页眉图片设置为左对齐 sheet.PageSetup.LeftHeaderImage = imgWtrmrk; sheet.PageSetup.LeftHeader = "&G"; //水印只会在此种模式下显现 sheet.ViewMode = ViewMode.Layout; } workbook.SaveToFile("水印.xlsx", ExcelVersion.Version2010); System.Diagnostics.Process.Start("水印.xlsx"); } <br> private static System.Drawing.Image DrawText(String text, System.Drawing.Font font, Color textColor, Color backColor, double height, double width) { //创建一个指定宽度和高度的位图图像 Image img = new Bitmap((int)width, (int)height); Graphics drawing = Graphics.FromImage(img); //获取文本大小 SizeF textSize = drawing.MeasureString(text, font); //旋转图片 drawing.TranslateTransform(((int)width - textSize.Width) / 2, ((int)height - textSize.Height) / 2); drawing.RotateTransform(-45); drawing.TranslateTransform(-((int)width - textSize.Width) / 2, -((int)height - textSize.Height) / 2); //绘制背景 drawing.Clear(backColor); //创建文本刷 Brush textBrush = new SolidBrush(textColor); drawing.DrawString(text, font, textBrush, ((int)width - textSize.Width) / 2, ((int)height - textSize.Height) / 2); drawing.Save(); return img; } } }

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

C#是一種現代、面向對象的編程語言,由微軟開發並作為.NET框架的一部分。 1.C#支持面向對象編程(OOP),包括封裝、繼承和多態。 2.C#中的異步編程通過async和await關鍵字實現,提高應用的響應性。 3.使用LINQ可以簡潔地處理數據集合。 4.常見錯誤包括空引用異常和索引超出範圍異常,調試技巧包括使用調試器和異常處理。 5.性能優化包括使用StringBuilder和避免不必要的裝箱和拆箱。

C#.NET應用的測試策略包括單元測試、集成測試和端到端測試。 1.單元測試確保代碼的最小單元獨立工作,使用MSTest、NUnit或xUnit框架。 2.集成測試驗證多個單元組合的功能,常用模擬數據和外部服務。 3.端到端測試模擬用戶完整操作流程,通常使用Selenium進行自動化測試。

C#.NET依然重要,因為它提供了強大的工具和庫,支持多種應用開發。 1)C#結合.NET框架,使開發高效便捷。 2)C#的類型安全和垃圾回收機制增強了其優勢。 3).NET提供跨平台運行環境和豐富的API,提升了開發靈活性。

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C#高級開發者面試需要掌握異步編程、LINQ、.NET框架內部工作原理等核心知識。 1.異步編程通過async和await簡化操作,提升應用響應性。 2.LINQ以SQL風格操作數據,需注意性能。 3..NET框架的CLR管理內存,垃圾回收需謹慎使用。

c#.netissutableforenterprise-levelapplications withemofrosoftecosystemdueToItsStrongTyping,richlibraries,androbustperraries,androbustperformance.however,itmaynotbeidealfoross-platement forment forment forment forvepentment offependment dovelopment toveloperment toveloperment whenrawspeedsportor whenrawspeedseedpolitical politionalitable,

C#在企業級應用、遊戲開發、移動應用和Web開發中均有廣泛應用。 1)在企業級應用中,C#常用於ASP.NETCore開發WebAPI。 2)在遊戲開發中,C#與Unity引擎結合,實現角色控制等功能。 3)C#支持多態性和異步編程,提高代碼靈活性和應用性能。

C#在.NET中的編程過程包括以下步驟:1)編寫C#代碼,2)編譯為中間語言(IL),3)由.NET運行時(CLR)執行。 C#在.NET中的優勢在於其現代化語法、強大的類型系統和與.NET框架的緊密集成,適用於從桌面應用到Web服務的各種開發場景。
