雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁(yè) > 軟件編程 > C#教程 >

C#給Excel添加水印實(shí)例詳解

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊:

C#中如何給Excel添加水印

我們知道Microsoft Excel并沒有內(nèi)置的功能直接給Excel表添加水印,但是其實(shí)我們可以用其他變通的方式來解決此問題,如通過添加頁(yè)眉圖片或藝術(shù)字的方法來模仿水印的外觀。所以在這篇文章中,我將向您演示來如何通過在Excel中創(chuàng)建和插入頁(yè)眉圖片來為excel添加水印。之前我也分享了如何給word文檔添加水印和pdf文件添加水印的方法,有需要也可以參考。

這里我下載了一個(gè)E-iceblue公司開發(fā)的免費(fèi)版的Excel組件- Free Spire.XLS,這樣既節(jié)省時(shí)間,又簡(jiǎn)化了代碼。

控件安裝后,創(chuàng)建項(xiàng)目,添加安裝目錄下的dll文件作為項(xiàng)目的引用,并添加如下命名空間:

using System;
using System.Drawing;
using System.Windows.Forms;
using Spire.Xls;

這是原excel表的截圖:

以下是詳細(xì)步驟和代碼片段:

步驟1:首先定義一個(gè)DrawText()方法,并在字符串的內(nèi)容基礎(chǔ)上創(chuàng)建一個(gè)圖片。字符串可以是“機(jī)密”、“草稿”、“樣品”或任何你想要顯示為水印的文本。

private static System.Drawing.Image DrawText(String text, System.Drawing.Font font, Color textColor, Color backColor, double height, double width) <br>{
 //創(chuàng)建一個(gè)指定寬度和高度的位圖圖像
 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:初始化一個(gè)新的工作簿并加載添加水印的文件。

Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.xlsx");

步驟3:調(diào)用DrawText()方法新建一個(gè)圖片,并將頁(yè)眉圖片設(shè)置為左對(duì)齊。其次,因?yàn)樵谝晥D模式是布局的狀態(tài)下頁(yè)眉圖片才會(huì)顯示,所以一定要記得將視圖模式改為布局。

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);
 //將頁(yè)眉圖片設(shè)置為左對(duì)齊
 sheet.PageSetup.LeftHeaderImage = imgWtrmrk;
 sheet.PageSetup.LeftHeader = "&G";
 //水印只會(huì)在此種模式下顯現(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)
    {
      //初始化一個(gè)新工作簿并加載要添加水印的文件
      Workbook workbook = new Workbook();
      workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.xlsx");
      //在頁(yè)眉插入圖片
      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);
        //將頁(yè)眉圖片設(shè)置為左對(duì)齊
        sheet.PageSetup.LeftHeaderImage = imgWtrmrk;
        sheet.PageSetup.LeftHeader = "&G";
        //水印只會(huì)在此種模式下顯現(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)建一個(gè)指定寬度和高度的位圖圖像
      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;
    }
 
  } 
}

 感謝您的瀏覽,希望本文能給您帶來一定的幫助,謝謝大家對(duì)本站的 支持!

上一篇:C#自定義日志記錄

欄    目:C#教程

下一篇:C#向圖片添加水印的兩種不同場(chǎng)景與解決方法

本文標(biāo)題:C#給Excel添加水印實(shí)例詳解

本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6245.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有