You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#使用MS Office Interop为Excel添加文字水印的实现方法

基于MS Office Interop实现Excel文字水印方案

需求背景

程序需基于原生MS Office Interop实现文档水印添加功能,不使用Aspose等第三方类库。目前已完成Word文档的文字水印开发,需要补全Excel场景下的对应实现。

已完成的Word端实现代码

Microsoft.Office.Interop.Word.Document wordDoc = new Microsoft.Office.Interop.Word.Document();
Application wordApp = new Application();
Microsoft.Office.Interop.Word.Shape wordShape = null;
wordApp.Visible = false;
wordApp.ScreenUpdating = false;
wordDoc = wordApp.Documents.Open(datei.pfad);
foreach (Microsoft.Office.Interop.Word.Section section in wordDoc.Sections)
{
    wordShape = section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Shapes.AddTextEffect(
                    Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1,
                    datei.TextWasserzeichen, "Arial", (float)60,
                    Microsoft.Office.Core.MsoTriState.msoTrue,
                    Microsoft.Office.Core.MsoTriState.msoFalse,
                    150, 150, ref o); 
    wordShape.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
    wordShape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
    wordShape.Fill.Solid();
    wordShape.Fill.ForeColor.RGB = (Int32)Microsoft.Office.Interop.Word.WdColor.wdColorGray90;
    wordShape.RelativeHorizontalPosition = Microsoft.Office.Interop.Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;
    wordShape.RelativeVerticalPosition = Microsoft.Office.Interop.Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin;
    wordShape.Left = (float)Microsoft.Office.Interop.Word.WdShapePosition.wdShapeCenter;
    wordShape.Top = (float)Microsoft.Office.Interop.Word.WdShapePosition.wdShapeCenter;
}
wordDoc.SaveAs2(datei.newPath + " mit Wasserzeichen in Text.docx", ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);
wordDoc.Close();
wordApp.Quit();  

当前问题

Excel对象模型和Word逻辑差异较大,找不到和Word中Microsoft.Office.Interop.Word.Section、section.Headers等效的对象,目前仅搭建完成Excel端基础代码框架,未实现水印添加核心逻辑,现有框架代码如下:

Microsoft.Office.Interop.Excel.Workbook workbook = new Microsoft.Office.Interop.Excel.Workbook();
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Shape excelShape = null;
excelApp.Visible = false;
excelApp.ScreenUpdating = false;
workbook = excelApp.Workbooks.Open(datei.pfad);
foreach(Microsoft.Office.Interop.Excel.Sheets sheet in workbook.Sheets)
{
    //待补充水印添加逻辑
} 

实现方案

Excel没有Word的分节/页眉结构,文字水印的实现逻辑是在每个工作表的页眉区域插入艺术字形状,调整样式后即可实现和Word端一致的水印效果,具体实现步骤如下:

  • 遍历工作簿中的表时,优先判断表类型为Worksheet,跳过图表、宏表等非工作表类型避免报错
  • 操作工作表的PageSetup属性,在中心页眉区域调用AddTextEffect方法创建艺术字,方法参数和Word端完全一致
  • 调整艺术字样式:设置填充色为浅灰色、隐藏轮廓线、添加-45度旋转、设置50%左右透明度避免遮挡单元格内容、位置固定到页面居中
  • 保存文件后按顺序释放COM对象,避免Excel进程后台残留

核心实现代码

将原有遍历循环替换为如下逻辑即可:

// 定义缺省参数
object o = Missing.Value;
foreach (dynamic sheet in workbook.Sheets)
{
    // 仅处理普通工作表
    if (sheet is not Microsoft.Office.Interop.Excel.Worksheet worksheet)
    {
        Marshal.ReleaseComObject(sheet);
        continue;
    }
    // 创建艺术字水印
    excelShape = worksheet.Shapes.AddTextEffect(
        Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1,
        datei.TextWasserzeichen, 
        "Arial", 
        60f,
        Microsoft.Office.Core.MsoTriState.msoTrue,
        Microsoft.Office.Core.MsoTriState.msoFalse,
        0, 0, ref o);
    // 基础样式和Word端对齐
    excelShape.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
    excelShape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
    excelShape.Fill.Solid();
    excelShape.Fill.ForeColor.RGB = (Int32)Microsoft.Office.Interop.Excel.XlRgbColor.rgbGray90;
    excelShape.Fill.Transparency = 0.5f; // 半透明效果避免遮挡内容
    excelShape.Rotation = -45f; // 斜向水印角度可按需调整
    // 位置设置为页面居中
    excelShape.Left = (excelApp.ActiveWindow.Width - excelShape.Width) / 2;
    excelShape.Top = (excelApp.ActiveWindow.Height - excelShape.Height) / 2;
    excelShape.Placement = Microsoft.Office.Interop.Excel.XlPlacement.xlFreeFloating;
    // 将形状绑定到中心页眉,实现打印、多页显示时自动重复
    worksheet.PageSetup.CenterHeader = "&G";
    Marshal.ReleaseComObject(worksheet);
}
// 后续保存、关闭逻辑
workbook.SaveAs(datei.newPath + " mit Wasserzeichen in Text.xlsx");
workbook.Close();
excelApp.Quit();
// 释放COM对象避免后台残留Excel进程
Marshal.ReleaseComObject(excelShape);
Marshal.ReleaseComObject(workbook);
Marshal.ReleaseComObject(excelApp);

注意:使用前需要引入System.Runtime.InteropServices命名空间用于COM对象释放,同时确保运行环境安装了对应版本的MS Office桌面端软件,Interop依赖本地Office组件,无法在无Office的服务器环境运行。

内容的提问来源于stack exchange,提问作者LFLJM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.30 04:01:13