欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

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

C#教程

當前位置:主頁 > 軟件編程 > C#教程 >

C#中給Excel添加水印的具體方法

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

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

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

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

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

這是原excel表的截圖:

以下是詳細步驟和代碼片段:

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

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è)置為左對齊。其次,因為在視圖模式是布局的狀態(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;
  }
 
 } 
}

 感謝您的瀏覽,希望本文能給您帶來一定的幫助。

上一篇:Visual Studio 未能加載各種Package包的解決方案

欄    目:C#教程

下一篇:C#實現(xiàn)win10 uwp 右擊浮出窗在點擊位置

本文標題:C#中給Excel添加水印的具體方法

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6234.html

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

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

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

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