LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

OfficeIMO - 为 .NET 开发者打造的轻量级 Word 文档操作库

admin
2025年6月24日 17:33 本文热度 50

简介

OfficeIMO 是一个用于创建和操作 Microsoft Word (.docx) 和 Excel (.xlsx) 文档的 .NET 库。它基于 OpenXML SDK,提供了更简单直观的 API 接口。

OfficeIMO 的设计理念是简单高效。专注于基本的 Word 处理需求,对于需要直接处理 Word 文档而不需要功能丰富的复杂库的项目来说,它是一个理想的选择。

该项目最初是为了简化 PowerShell 模块 PSWriteOffice 中的文档生成流程而开发,现已成为适用于整个 .NET 社区的通用库。

平台支持与兼容性

平台
支持版本
Windows
.NET Framework 4.7.2+、.NET Core 3.1+、.NET 5/6/7/8/9
Linux
.NET Core 3.1+、.NET 5/6/7/8/9
macOS
.NET Core 3.1+、.NET 5/6/7/8/9

✅ 测试覆盖率Codecov 链接
✅ 持续集成状态GitHub Actions CI

核心功能列表

Word 功能

  • • ☑️ 创建、加载、保存文档
  • • ☑️ 设置文档属性(标题、作者、关键词等)
  • • ☑️ 添加段落并设置样式(加粗、颜色、对齐方式)
  • • ☑️ 表格操作(添加行/列、合并单元格、设置边框)
  • • ☑️ 插入图片(文件、Base64)
  • • ☑️ 超链接、书签、分页符、注释
  • • ☑️ 内容控件(StructuredDocumentTag)
  • • ☑️ 图表、形状、水印、封面页
  • • ☑️ 嵌入 HTML、RTF、TXT 文件
  • • ☑️ 自动生成目录(TOC)

使用示例

Nuget安装

dotnet add package OfficeIMO.Word

基础文档创建

string filePath = Path.Combine("Support""GitHub""PSWriteOffice""Examples""Documents""BasicDocument.docx");

using (WordDocument document = WordDocument.Create(filePath)) {
    document.Title = "This is my title";
    document.Creator = "Przemysław Kłys";
    document.Keywords = "word, docx, test";

    var paragraph = document.AddParagraph("Basic paragraph");
    paragraph.ParagraphAlignment = JustificationValues.Center;
    paragraph.Color = SixLabors.ImageSharp.Color.Red;

    document.Save(true);
}

流式文档操作

using var stream = new MemoryStream();
using (var document = WordDocument.Create(stream)) {
    document.AddParagraph("Stream based document");
    document.Save(stream);
}

stream.Position = 0;
using (var loaded = WordDocument.Load(stream)) {
    Console.WriteLine(loaded.Paragraphs[0].Text);
}

保存为新文档

using (WordDocument document = WordDocument.Create()) {
    document.AddParagraph("Some text");
    using var copy = document.SaveAs(filePath);
    // document.FilePath 仍然是 null
    // copy.FilePath 等于 filePath
}

页眉页脚设置

using (WordDocument document = WordDocument.Create(filePath)) {
    document.Sections[0].PageOrientation = PageOrientationValues.Landscape;
    document.AddParagraph("Test Section0");
    document.AddHeadersAndFooters();
    document.DifferentFirstPage = true;
    document.DifferentOddAndEvenPages = true;

    document.Sections[0].Header.First.AddParagraph().SetText("Test Section 0 - First Header");
    document.Sections[0].Header.Default.AddParagraph().SetText("Test Section 0 - Header");
    document.Sections[0].Header.Even.AddParagraph().SetText("Test Section 0 - Even");

    document.AddPageBreak();
    // ... 其他节配置 ...
    document.Save(true);
}

内容控件操作

using (WordDocument document = WordDocument.Create(filePath)) {
    var sdt = document.AddStructuredDocumentTag("Hello""MyAlias""MyTag");
    sdt.Text = "Changed";
    document.Save(true);
}

using (WordDocument document = WordDocument.Load(filePath)) {
    var tag = document.GetStructuredDocumentTagByTag("MyTag");
    Console.WriteLine(tag.Text);
}

该文章在 2025/6/26 22:14:26 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved