C#給Excel添加水印實(shí)例詳解
C#中如何給Excel添加水印
我們知道Microsoft Excel并沒有內(nèi)置的功能直接給Excel表添加水印,但是其實(shí)我們可以用其他變通的方式來解決此問題,如通過添加頁眉圖片或藝術(shù)字的方法來模仿水印的外觀。所以在這篇文章中,我將向您演示來如何通過在Excel中創(chuàng)建和插入頁眉圖片來為excel添加水印。之前我也分享了如何給word文檔添加水印和pdf文件添加水印的方法,有需要也可以參考。
這里我下載了一個E-iceblue公司開發(fā)的免費(fèi)版的Excel組件- Free Spire.XLS,這樣既節(jié)省時間,又簡化了代碼。
控件安裝后,創(chuàng)建項(xiàng)目,添加安裝目錄下的dll文件作為項(xiàng)目的引用,并添加如下命名空間:
using System; using System.Drawing; using System.Windows.Forms; using Spire.Xls;
這是原excel表的截圖:
以下是詳細(xì)步驟和代碼片段:
步驟1:首先定義一個DrawText()方法,并在字符串的內(nèi)容基礎(chǔ)上創(chuàng)建一個圖片。字符串可以是“機(jī)密”、“草稿”、“樣品”或任何你想要顯示為水印的文本。
private static System.Drawing.Image DrawText(String text, System.Drawing.Font font, Color textColor, Color backColor, double height, double width) <br>{ //創(chuàng)建一個指定寬度和高度的位圖圖像 Image img = new Bitmap((int)width, (int)height); Graphics drawing = Graphics.FromImage(img); //獲取文本大小 SizeF textSize = drawing.MeasureString(text, font); //旋轉(zhuǎn)圖片 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); //創(chuàng)建文本刷 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:\Users\Administrator\Desktop\sample.xlsx");
步驟3:調(diào)用DrawText()方法新建一個圖片,并將頁眉圖片設(shè)置為左對齊。其次,因?yàn)樵谝晥D模式是布局的狀態(tài)下頁眉圖片才會顯示,所以一定要記得將視圖模式改為布局。
Font font = new System.Drawing.Font("arial", 40); String watermark = "內(nèi)部資料"; foreach (Worksheet sheet in workbook.Worksheets) { //調(diào)用DrawText()方法新建圖片 System.Drawing.Image imgWtrmrk = DrawText(watermark, font, System.Drawing.Color.LightCoral, System.Drawing.Color.White, sheet.PageSetup.PageHeight, sheet.PageSetup.PageWidth); //將頁眉圖片設(shè)置為左對齊 sheet.PageSetup.LeftHeaderImage = imgWtrmrk; sheet.PageSetup.LeftHeader = "&G"; //水印只會在此種模式下顯現(xiàn) sheet.ViewMode = ViewMode.Layout; }
步驟4:保存并打開文件。
workbook.SaveToFile("水印.xlsx", ExcelVersion.Version2010);
System.Diagnostics.Process.Start("水印.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 = "內(nèi)部資料"; foreach (Worksheet sheet in workbook.Worksheets) { //調(diào)用DrawText()方法新建圖片 System.Drawing.Image imgWtrmrk = DrawText(watermark, font, System.Drawing.Color.LightCoral, System.Drawing.Color.White, sheet.PageSetup.PageHeight, sheet.PageSetup.PageWidth); //將頁眉圖片設(shè)置為左對齊 sheet.PageSetup.LeftHeaderImage = imgWtrmrk; sheet.PageSetup.LeftHeader = "&G"; //水印只會在此種模式下顯現(xiàn) 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) { //創(chuàng)建一個指定寬度和高度的位圖圖像 Image img = new Bitmap((int)width, (int)height); Graphics drawing = Graphics.FromImage(img); //獲取文本大小 SizeF textSize = drawing.MeasureString(text, font); //旋轉(zhuǎn)圖片 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); //創(chuàng)建文本刷 Brush textBrush = new SolidBrush(textColor); drawing.DrawString(text, font, textBrush, ((int)width - textSize.Width) / 2, ((int)height - textSize.Height) / 2); drawing.Save(); return img; } } }
感謝您的瀏覽,希望本文能給您帶來一定的幫助,謝謝大家對本站的 支持!
- C#實(shí)現(xiàn)DataSet內(nèi)數(shù)據(jù)轉(zhuǎn)化為Excel和Word文件的通用類完整實(shí)例
- C#使用winform簡單導(dǎo)出Excel的方法
- C#使用oledb導(dǎo)出數(shù)據(jù)到excel的方法
- C#使用Aspose.Cells控件讀取Excel
- C#定制Excel界面并實(shí)現(xiàn)與數(shù)據(jù)庫交互的方法
- C#讀取Excel的三種方式以及比較分析
- C#實(shí)現(xiàn)將DataTable內(nèi)容輸出到Excel表格的方法
- C#實(shí)現(xiàn)將數(shù)據(jù)導(dǎo)出到word或者Excel中的方法
- C#實(shí)現(xiàn)導(dǎo)入CSV文件到Excel工作簿的方法
相關(guān)文章
C#使用StreamReader和StreamWriter類讀寫操作文件
這篇文章介紹了C#使用StreamReader和StreamWriter類讀寫操作文件的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05