【问题标题】:Adding watermark to excel file给excel文件添加水印
【发布时间】:2014-12-22 17:45:53
【问题描述】:

我有一个应用程序从 excel 文件中的工作表中读取数据,并在同一文件的另一个工作表中打印出来。

为了我自己的满意,我想在显示输出的工作表中添加水印。 我正在使用 C# 和 .NET

我需要粘贴任何特定代码吗?我不确定你需要什么。请询问您是否需要更多详细信息

用于处理 excel 对象的库 -:

using Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;

//creating an object of Application
Excel.Application excelApp = new Excel.Application();

//creating an object of Workbook
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(path, 0,false,
                            5, "", "", false, Excel.XlPlatform.xlWindows,
                            "", true, false, 0, true, false, false);

//creating an object of Sheet
Excel.Sheets excelSheets = (Excel.Sheets)excelWorkbook.Sheets;

【问题讨论】:

  • 您应该指定您使用什么库来读取和写入 Excel 工作簿中的工作表。
  • Excel 并不真正支持水印。然而,它确实支持在表头数据后面添加一个大的胖图像,并且确实看起来像一个水印。这就是你所追求的吗?
  • @Paul-Jan 你能具体说明如何做到这一点,如果这足以很好地达到目的。

标签: c# excel


【解决方案1】:

由于 Excel 支持打开 HTML,您可以生成使用 excel 打开的 Html 表格。这段 html 代码将背景图像或水印(虽然不是 html 标准)添加到您的 excel 生成文件中。您需要将图像文件与在 excel 中打开的 HTML 文件放在同一文件夹中。这适用于您不使用任何 .NET Framework 来生成 Excel 兼容文件的情况。

Generated_Excel.html:

<html>
<body background="1.png">
    <table>
        <tr>
            <td>Column1</td> 
            <td>Column2</td>
        </tr>
        <tr>
            <td>Value Column 1</td> 
            <td>Value Column 2</td>
        </tr>
    </table>
</body>

最终结果:

【讨论】:

    【解决方案2】:

    如果您使用的是 Syncfusion XlsIO,那么您必须使用 this code

    // Set the image as sheet background
    sheet.PageSetup.BackgoundImage = new System.Drawing.Bitmap(@"image.png");
    

    【讨论】:

      【解决方案3】:

      我一直在研究这个问题,因为我在发布我的工作代码的任何地方都没有找到解决方案。此解决方案通过创建临时图像并将其插入幻灯片来工作。创建代码的步骤基于此:http://smallbusiness.chron.com/put-watermarks-photos-word-45076.html链接。

          public void AddWaterMarkToPowerPoint(string filePath)
          {
              string waterMarkText = "Top secret";
      
              PowerPoint.Application powerPointApp = new PowerPoint.Application();
      
              PowerPoint.Presentations pres = powerPointApp.Presentations;
      
              string imagePath = null;
      
              try
              {
                  PowerPoint.Presentation pptPresentation = pres.Open(filePath, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);
      
                  PowerPoint.PageSetup pageSetup = pptPresentation.PageSetup;
      
                  float pageWidth = pageSetup.SlideWidth;
                  float pageHeight = pageSetup.SlideHeight;
      
                  CreateTempWaterMarkImage(waterMarkText, ref imagePath, (int)pageHeight, (int)pageWidth);
      
                  for (int i = 1; i <= pptPresentation.Slides.Count; i++)
                  {
                      PowerPoint.Slide slide = pptPresentation.Slides[i];
      
                      PowerPoint.Shapes shapes = slide.Shapes;
      
                      PowerPoint.Shape shape = shapes.AddShape(MsoAutoShapeType.msoShapeRectangle, 0, 0, pageWidth, pageHeight);
      
                      shape.Fill.UserPicture(imagePath);
      
                      shape.Fill.Transparency = 0.7f;
      
                      shape.Line.Transparency = 1;
      
                      shape.ZOrder(MsoZOrderCmd.msoBringToFront);
                  }
      
                  pptPresentation.SaveAs(filePath);
      
                  pptPresentation.Close();
              }
              catch (Exception ex)
              {
                  //log exception
      
                  throw;
              }
              finally
              {
                  // Cleanup
                  GC.Collect();
                  GC.WaitForPendingFinalizers();
      
                  powerPointApp.Quit();
      
                  //remove temp image
                  if (imagePath != null)
                      File.Delete(imagePath);
              }
          }
      
      private void CreateTempWaterMarkImage(string waterMarkText, ref string imagePath, int pageHeight, int pageWidth)
      {
      
          float angleRotation = (float)((Math.Atan2((double)pageHeight, (double)pageWidth) * 180) / Math.PI);
      
          float fontSize = (float)Math.Sqrt(Math.Pow(pageHeight, 2) + Math.Pow(pageWidth, 2)) / 50;
      
          using (Bitmap newie = new Bitmap(pageWidth, pageHeight))
          {
              using (Graphics gr = Graphics.FromImage(newie))
              {
                  gr.SmoothingMode = SmoothingMode.AntiAlias;
      
                  gr.TranslateTransform((float)pageWidth / 2f, (float)pageHeight / 2f);
      
                  gr.RotateTransform(-angleRotation);
      
                  Font font = new Font("Arial", fontSize, FontStyle.Regular);
      
                  SizeF textSize = gr.MeasureString(companyName, font);
      
                  gr.DrawString(waterMarkText, font, SystemBrushes.GrayText, -textSize.Width, -textSize.Height);
              }
      
              string fileName = Path.GetRandomFileName();
      
                  imagePath = Path.GetTempPath() + @"\" + fileName + ".png";
      
                  newie.Save(imagePath, ImageFormat.Png);
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多