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

Debian下.NET 8使用LibreOffice UNO API保存文档及包引用问题

在Debian的.NET 8项目中引入LibreOffice UNO API依赖

问题描述

我正在Debian系统上开发.NET 8跨平台应用,需通过LibreOffice UNO API与LibreOffice交互,目标是保存已打开的LibreOffice标签页。以下是我的大致代码:

using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.uno;
using unoidl.com.sun.star.beans;
    
public void SaveLibreOfficeDocument()
{
    XComponentContext xLocalContext = uno.util.Bootstrap.bootstrap();
    XMultiComponentFactory xMCF = xLocalContext.getServiceManager();
    
    object desktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xLocalContext);
    XComponentLoader xCL = (XComponentLoader)desktop;
    XModel xModel = (XModel)xCL.loadComponentFromURL("private:factory/scalc", "_blank", 0, new PropertyValue[0]);
    
    // Now, save the document
    XStorable xStorable = (XStorable)xModel;
    xStorable.store();
}

问题在于我不知道该如何引入所有所需包,代码中的using语句均显示错误。

解决步骤

1. 安装系统层面的LibreOffice UNO依赖

首先在Debian上安装LibreOffice开发相关的系统包,提供UNO的底层支持:

sudo apt-get update && sudo apt-get install libreoffice libreoffice-dev libreoffice-uno libreoffice-sdk

2. 添加.NET项目的UNO NuGet包

使用官方维护的NuGet包可以直接引入所需的命名空间,无需手动处理IDL文件:
执行以下命令添加包引用:

dotnet add package LibreOffice.Uno

或者直接在项目的.csproj文件中添加:

<PackageReference Include="LibreOffice.Uno" Version="7.6.7" />

(注意替换为最新版本号)

3. 调整代码以保存已打开的文档

原代码是新建Calc文档,若要保存已打开的标签页,需要连接到已运行的LibreOffice实例并遍历所有打开的文档:

using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.uno;
using unoidl.com.sun.star.beans;

public void SaveOpenLibreOfficeDocuments()
{
    // 连接到本地运行的LibreOffice实例
    XComponentContext xLocalContext = uno.util.Bootstrap.bootstrap();
    XMultiComponentFactory xMCF = xLocalContext.getServiceManager();

    object desktopObj = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xLocalContext);
    XDesktop xDesktop = (XDesktop)desktopObj;
    
    // 获取所有已打开的文档框架
    XFramesSupplier framesSupplier = (XFramesSupplier)xDesktop;
    XFrames frames = framesSupplier.getFrames();
    int frameCount = frames.getCount();

    for (int i = 0; i < frameCount; i++)
    {
        object frameObj = frames.getByIndex(i);
        XFrame frame = (XFrame)frameObj;
        XController controller = frame.getController();
        
        if (controller != null)
        {
            XModel model = controller.getModel();
            if (model != null)
            {
                XStorable storable = (XStorable)model;
                // 仅保存已修改的文档
                if (storable.isModified())
                {
                    storable.store();
                    Console.WriteLine($"已保存文档: {model.getURL()}");
                }
            }
        }
    }
}

4. 运行时注意事项

  • 确保LibreOffice已提前启动,UNO接口默认启用,无需额外配置
  • 若程序无法连接到LibreOffice,可尝试启动LibreOffice时添加--accept="socket,host=localhost,port=2002;urp;"参数指定UNO连接端口
  • 确保运行程序的用户有访问LibreOffice配置目录的权限

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 22:25:09