1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

LibreOffice CalcをJScriptなどから操作してみた

Last updated at Posted at 2020-11-24

#はじめに

を参考にさせて頂きました。圧倒的感謝……!

#環境

  • Windows 10 Home (64bit)
  • Visual Studio Community 2019

#サンプルソース

##JScript

libreoffice.js
{
	var factory = WScript.CreateObject("com.sun.star.ServiceManager");
	var loader = factory.createInstance("com.sun.star.frame.Desktop");
	var doc = loader.loadComponentFromURL("file:///C:/data/test.ods", "_blank", 0, []);

	var sheets = doc.getSheets();
	var sheet = sheets.getByName("Sheet1");
	var cell = sheet.getCellByPosition(0, 0);
	cell.setFormula("hello, world");
}

##VB.NET

Module1.vb
Module Module1

    Sub Main()
        Dim factory, loader, doc As Object
        Dim d(0) As Object

        factory = CreateObject("com.sun.star.ServiceManager")
        loader = factory.createInstance("com.sun.star.frame.Desktop")
        doc = loader.loadComponentFromURL("private:factory/scalc", "_blank", 0, d)

        Dim sheets, sheet, cell As Object
        sheets = doc.getSheets()
        sheet = sheets.getByName("Sheet1")
        cell = sheet.getCellByPosition(1, 1)
        cell.setFormula("hello, world")
    End Sub

End Module

##C#
LibreOffice SDKを使う方法もあるが、使わない方法もある。

Program.cs
using System;
using System.Reflection;
using System.Runtime.InteropServices;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var t = Type.GetTypeFromProgID("com.sun.star.ServiceManager");
            var factory = Activator.CreateInstance(t);

            object[] a = { "com.sun.star.frame.Desktop" };
            var loader = factory.GetType().InvokeMember("createInstance",
                BindingFlags.InvokeMethod, null, factory, a);

            object[] b = { };
            object[] c = { "private:factory/scalc", "_blank", 0, b };
            var doc = loader.GetType().InvokeMember("loadComponentFromURL",
                BindingFlags.InvokeMethod, null, loader, c);

            Marshal.ReleaseComObject(factory);
        }
    }
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?