{"id":11830,"date":"2025-12-12T12:23:25","date_gmt":"2025-12-12T03:23:25","guid":{"rendered":"https:\/\/www.moonmile.net\/blog\/?p=11830"},"modified":"2025-12-30T15:28:16","modified_gmt":"2025-12-30T06:28:16","slug":"libreoffice-calc-%e3%82%92-c-%e3%81%8b%e3%82%89%e3%82%a2%e3%82%af%e3%82%bb%e3%82%b9%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"http:\/\/www.moonmile.net\/blog\/archives\/11830","title":{"rendered":"LibreOffice Calc \u3092 C# \u304b\u3089\u30a2\u30af\u30bb\u30b9\u3059\u308b&#8230;\u304c\u3001\u3067\u304d\u307e\u305b\u3093"},"content":{"rendered":"\n<p>\u6700\u7d42\u7684\u306b\u306f Python \u304b VB \u3042\u305f\u308a\u3067\u52d5\u304b\u3057\u305f\u3044\u306e\u3067\u3059\u304c\u3001\u3061\u3087\u3063\u3068\u5bc4\u308a\u9053\u3002<\/p>\n\n\n\n<p>Python \u30b3\u30fc\u30c9\u3092\u66f8\u304f\u3068\u304d\u306b\u578b\u30c1\u30a7\u30c3\u30af\u304c\u51fa\u6765\u306a\u304f\u3066\u56f0\u3063\u3066\u3044\u307e\u3059&#8230;\u3068\u8a00\u3044\u307e\u3059\u304b\u3001Python \u306e\u5834\u5834\u5408\u306b\u306f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3092\u3059\u308b\u3068\u304d\u306b\u30d7\u30ed\u30d1\u30c6\u30a3\u540d\u3068\u304b\u30e1\u30bd\u30c3\u30c9\u540d\u3068\u304b\u3069\u3046\u3084\u3063\u3066\u88dc\u5b8c\u3057\u3066\u3044\u308b\u306e\u3067\u3057\u3087\u3046\u304b\uff1f<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">*.pyi \u3067\u30b9\u30bf\u30d6\u3092\u4f5c\u308b<\/h2>\n\n\n\n<p>LibreOffice \u306e UNO API \u306f\u623b\u308a\u5024\u304c Any \u306a\u306e\u3067\u30b3\u30fc\u30c9\u88dc\u5b8c\u304c\u52b9\u304d\u307e\u305b\u3093\u3002\u591a\u5206\u3001\u623b\u308a\u5024\u304c object \u306a\u306e\u3067\u3001\u305d\u3046\u3044\u3046\u4ed5\u69d8\u306b\u306a\u3063\u3066\u3044\u308b\u306e\u3067\u3057\u3087\u3046\u304c\u3001\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u3068\u304d\u306b\u9762\u5012\u3067\u3059\u3002\u3068\u3044\u3046\u304b\u3001\u3069\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u305b\u308b\u306e\u304b\u5b9f\u884c\u6642\u306b\u3057\u304b\u308f\u304b\u308a\u307e\u305b\u3093\u3002<br>\u3072\u3087\u3063\u3068\u3057\u3066\u30e1\u30bd\u30c3\u30c9\u540d\u3092\u5168\u90e8\u899a\u3048\u3066\u3044\u308b\u306e\u304b\uff1f\u3000\u3068\u3082\u601d\u308f\u306a\u304f\u3082\u306a\u3044\u306e\u3067\u3059\u304c\u3001\u3082\u3063\u3068\u6577\u5c45\u3092\u4e0b\u3052\u3066\u304a\u304d\u305f\u3044\u3067\u3059\u3002\u81ea\u5206\u306e\u305f\u3081\u306b\u3082\u3002<\/p>\n\n\n\n<p>\u304a\u305d\u3089\u304f\u3001C++\u3001Java \u3067 UNO API \u3092\u4f7f\u3046\u305f\u3081\u306b\u306f\u5927\u91cf\u306e\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3092\u5b9a\u7fa9\u3057\u3066\u3042\u308b\u306f\u305a\u3067\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom typing import TYPE_CHECKING, Protocol, cast, Any\n\nclass XCell(Protocol):\n    def getString(self) -&gt; str: ...\n    def setString(self, value: str) -&gt; None: ...\n    def getFormula(self) -&gt; str: ...\n    def setFormula(self, formula: str) -&gt; None: ...\n    def getValue(self) -&gt; float: ...\n    def setValue(self, value: float) -&gt; None: ...\n\nclass XCellRange(Protocol):\n    def getCellByPosition(self, column: int, row: int) -&gt; XCell: ...\n    def getCellRangeByPosition(self, startColumn: int, startRow: int, endColumn: int, endRow: int) -&gt; &#039;XCellRange&#039;: ...\n    def getCellRangesByName (self, aRangeName: str) -&gt; tuple&#x5B;&#039;XCellRange&#039;, ...]: ...\n    def getRangeAddress(self) -&gt; CellRangeAddress: ...\n\nclass XNameAccess(Protocol):\n    def getByName(self, name: str) -&gt; XComponent: ...\n    def hasByName(self, name: str) -&gt; bool: ...\n    def getElementNames(self) -&gt; tuple&#x5B;str, ...]: ...\n    def getEmbeddedObject(self) -&gt; XChartDocument: ...\n\nclass XChartDocument(Protocol):\n    def setDiagram(self, diagram: XDiagram) -&gt; None: ...\n    def createInstance(self, serviceName: str) -&gt; Any: ...\n\nclass XDiagram (Protocol):\n    pass\n\nclass XTableCharts(Protocol):\n    def addNewByName(self, name: str, aRect: Any, aRanges: tuple&#x5B;str, ...], bColumnHeaders: bool, bRowHeaders: bool) -&gt; None: ...\n    def hasByName(self, name: str) -&gt; bool: ...\n    def removeByName(self, name: str) -&gt; None: ...\n    def getByName(self, name: str) -&gt; XNameAccess: ...\n...\n<\/pre><\/div>\n\n\n<p>\u3053\u3093\u306a\u611f\u3058\u3067 calc.pyi \u3068\u3044\u3046\u30b9\u30bf\u30d6\u3092\u4f5c\u3063\u3066\u304a\u3044\u3066\u3001\u672c\u4f53\u306e\u30b3\u30fc\u30c9\u3067\u53c2\u7167\u3055\u305b\u307e\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport uno\nimport re\nfrom typing import Any, TYPE_CHECKING, cast\n\n# typings\/calc.pyi \u306f\u578b\u30c1\u30a7\u30c3\u30af\u5c02\u7528\u306b\u8aad\u307f\u8fbc\u3080\uff08\u5b9f\u884c\u6642\u306f\u30d5\u30a9\u30fc\u30eb\u30d0\u30c3\u30af\uff09\nif TYPE_CHECKING:\n\tfrom calc import XDesktop, XComponent, XSpreadsheet, XCellRange, Rectangle  # type: ignore\nelse:\n\tXDesktop = XComponent = XSpreadsheet = XCellRange = Rectangle = Any\n\n&quot;&quot;&quot;\n\u68d2\u30b0\u30e9\u30d5\u3092\u4f5c\u6210\u3059\u308b\u30b9\u30af\u30ea\u30d7\u30c8\n&quot;&quot;&quot;\n\n\ndef connect_to_libreoffice() -&gt; tuple&#x5B;XDesktop, XComponent, XSpreadsheet]:\n\t&quot;&quot;&quot;LibreOffice \u306b\u63a5\u7d9a\u3057\u3066\u30a2\u30af\u30c6\u30a3\u30d6\u306a Calc \u30b7\u30fc\u30c8\u3092\u53d6\u5f97\u3059\u308b&quot;&quot;&quot;\n\tlocal_ctx = uno.getComponentContext()\n\tresolver = local_ctx.ServiceManager.createInstanceWithContext(\n\t\t&quot;com.sun.star.bridge.UnoUrlResolver&quot;, local_ctx)\n\tctx = resolver.resolve(\n\t\t&quot;uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext&quot;)\n\tsmgr = ctx.ServiceManager\n\tdesktop : XDesktop = smgr.createInstanceWithContext(&quot;com.sun.star.frame.Desktop&quot;, ctx)\n\n\tmodel = desktop.getCurrentComponent()\n\tif model is None:\n\t\traise RuntimeError(&quot;LibreOffice Calc \u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u304c\u958b\u304b\u308c\u3066\u3044\u307e\u305b\u3093&quot;)\n\n\tsheet = model.getCurrentController().getActiveSheet()\n\treturn desktop, model, sheet\n\n<\/pre><\/div>\n\n\n<p>if TYPE_CHECKING \u306e\u3068\u3053\u308d\u3067\u3001\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u6642\u3068\u5b9f\u884c\u6642\u306e\u5207\u308a\u66ff\u3048\u3092\u3057\u306a\u3068\u3044\u3051\u306a\u3044\u306e\u304c\u9762\u5012\u306a\u306e\u3067\u3059\u304c\u3001\u3053\u308c\u3067 Any \u3068\u306a\u3063\u3066\u3044\u308b\u578b\u3092\u7121\u7406\u77e2\u7406  XCellRange \u306a\u3069\u306e\u578b\u306b\u5909\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br>\u672c\u6765\u306f\u3001cast \u95a2\u6570\u3092\u4f7f\u3063\u3066\u6b63\u5f0f\u306b\u30ad\u30e3\u30b9\u30c8\u3092\u3059\u308c\u3070\u3044\u3044\u306e\u3067\u3059\u304c\u3001\u3069\u3046\u305b Any \u304b\u3089\u306e\u30ad\u30e3\u30b9\u30c8\u3067\u3057\u304b\u306a\u3044\u306e\u3067\u3001\u5909\u6570\u306e\u578b\u3060\u3051\u6c7a\u3081\u3066\u3001\u305d\u3053\u306b\u7121\u7406\u3084\u308a\u62bc\u3057\u3053\u307f\u307e\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndesktop : XDesktop = smgr.createInstanceWithContext(&quot;com.sun.star.frame.Desktop&quot;, ctx)\n<\/pre><\/div>\n\n\n<p>\u5b9f\u884c\u6642\u306e\u578b\u30c1\u30a7\u30c3\u30af\u3067\u306f\u306a\u304f\u3066\u3001\u3042\u304f\u307e\u3067\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u6642\u306e\u30b3\u30fc\u30c9\u88dc\u5b8c\u306e\u305f\u3081\u306a\u306e\u3067\u3053\u308c\u3067\u5341\u5206\u3067\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">pyi \u306e\u30b9\u30bf\u30d6\u306f\u3069\u3046\u4f5c\u308b\u306e\u304b\uff1f<\/h2>\n\n\n\n<p>UNO API \u306e\u30af\u30e9\u30b9\u306f\u81a8\u5927\u306b\u3042\u3063\u3066\u3001\u3061\u307e\u3061\u307e\u624b\u4f5c\u696d\u3067\u5909\u63db\u3059\u308b\u306e\u306f\u5927\u5909\u3067\u3059\u3002\u3044\u3084\u3001\u305d\u3082\u305d\u3082\u4f7f\u3044\u305f\u3044\u306e\u306f Calc \u95a2\u4fc2\u3060\u3051\u306a\u306e\u3067\u3001\u5168\u90e8\u3092\u5909\u63db\u3059\u308b\u306e\u306f\u5927\u8888\u88df\u3060\u3057\u3001\u624b\u9593\u304c\u304b\u304b\u308a\u307e\u3059\u3002<br>\u57fa\u672c\u306f AI \u30a8\u30fc\u30b8\u30a7\u30f3\u30c8\u306b\u30b3\u30fc\u30c9\u3092\u4f5c\u3063\u3066\u8cb0\u3046\u3064\u3082\u308a\u306a\u306e\u3067\u3001\u3059\u3079\u3066\u306e\u578b\u304c\u5fc5\u8981\u306a\u308f\u3051\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u3061\u3087\u3063\u3068\u624b\u76f4\u3057\u3092\u3059\u308b\u3068\u304b\u3001\u6570\u884c\u306e\u30de\u30af\u30ed\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u3068\u304d\u306b\u30b3\u30fc\u30c9\u88dc\u5b8c\u304c\u3067\u304d\u308c\u3070\u3088\u3044\u306e\u3067\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">C++ \u3084 Java \u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u5229\u7528\u3059\u308c\u3070\u3044\u3044\u306e\u3067\u306f\uff1f<\/h2>\n\n\n\n<p>UNO API \u306e\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u306f\u3001Python \u3060\u3051\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002C++ \u3084 Java \u304b\u3089\u3082\u4f7f\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306f\u975e\u5e38\u306b\u5c11\u306a\u3044\u3067\u3059\u304c\u3001C# \u304b\u3089\u3001\u3064\u307e\u308a .NET \u304b\u3089\u306e\u30a2\u30af\u30bb\u30b9\u3082\u53ef\u80fd\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>Copilot \u306b\u805e\u304f\u3068 unoidl.dll \u3068\u3044\u3046\u306e\u304c\u3042\u308b\u305d\u3046\u3067\u3059\uff08\u5b9f\u969b\u306b\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u5f8c\u8ff0\u3059\u308b\u3088\u3046\u306b\u3001cli_oootypes.dll \u7b49\u306e\u30d5\u30a1\u30a4\u30eb\u540d\u306b\u5909\u308f\u3063\u3066\u3044\u307e\u3059\uff09\u3002\u3053\u308c\u3092\u3046\u307e\u304f\u4f7f\u3048\u3070\u81ea\u52d5\u751f\u6210\u304c\u3067\u304d\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u306d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">SDK \u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b<\/h3>\n\n\n\n<p>\u3053\u3053\u3067\u306f\u3001\u4ed6\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u898b\u308b\u305f\u3081\u306b SDK \u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u5b9f\u884c\u6642\u306b\u306f\u5fc5\u8981\u3042\u308a\u307e\u305b\u3093\u3002C# \u306e\u5834\u5408\u306f\u3001\u9069\u5f53\u306a *.dll \u3092\u5b9f\u884c\u30d5\u30a1\u30a4\u30eb\u3068\u4e00\u7dd2\u306e\u30d5\u30a9\u30eb\u30c0\u30fc\u306b\u5165\u308c\u308b\u3053\u3068\u306b\u306a\u308b\u306e\u3067\u3001\u5ba2\u5148\u306e PC \u306b SDK \u3092\u5165\u308c\u306a\u304f\u3066\u3082\u3088\u3044\u3067\u3059\u3002<\/p>\n\n\n\n<p>SDK and Sourcecode<br><a href=\"https:\/\/www.libreoffice.org\/download\/download-libreoffice\">https:\/\/www.libreoffice.org\/download\/download-libreoffice<\/a><\/p>\n\n\n\n<p>\u3053\u308c\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3068 LibreOffice \u306e\u30de\u30af\u30ed\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306a\u3069\u304c\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u307e\u3059\u3002<br>\u30d5\u30a9\u30eb\u30c0\u30fc\u540d\u306f C:\\Program Files\\LibreOffice\\sdk\\ \u3067\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/12\/image-34.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1011\" height=\"713\" src=\"https:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/12\/image-34.png\" alt=\"\" class=\"wp-image-11831\" srcset=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/12\/image-34.png 1011w, http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/12\/image-34-300x212.png 300w, http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/12\/image-34-768x542.png 768w\" sizes=\"auto, (max-width: 1011px) 100vw, 1011px\" \/><\/a><\/figure>\n\n\n\n<p>IDL \u30ea\u30d5\u30a1\u30ec\u30f3\u30b9\u306f\u672c\u5bb6 <a href=\"https:\/\/api.libreoffice.org\/\">https:\/\/api.libreoffice.org\/<\/a> \u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u91cd\u305f\u304f\u3066\u4ed5\u65b9\u304c\u306a\u3044\u306e\u3067\u3001\u30ed\u30fc\u30ab\u30eb PC \u306b\u5165\u308c\u3066\u691c\u7d22\u3059\u308b\u3068\u826f\u3044\u3067\u3059\u3002\u61d0\u304b\u3057\u306e doxygen \u306b\u3088\u308b\u81ea\u52d5\u751f\u6210\u3067\u3059\u3002 <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/12\/image-35.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1011\" height=\"713\" src=\"https:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/12\/image-35.png\" alt=\"\" class=\"wp-image-11832\" srcset=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/12\/image-35.png 1011w, http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/12\/image-35-300x212.png 300w, http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/12\/image-35-768x542.png 768w\" sizes=\"auto, (max-width: 1011px) 100vw, 1011px\" \/><\/a><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">C# \u3067\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u53c2\u7167\u3059\u308b<\/h3>\n\n\n\n<p>\u9069\u5f53\u306a\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u3063\u3066\u3001C:\\Program Files\\LibreOffice\\sdk\\cli\\ \u306b\u3042\u308b *.dll \u3092\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u304b\u3089\u53c2\u7167\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/12\/image-36.png\"><img loading=\"lazy\" decoding=\"async\" width=\"721\" height=\"517\" src=\"https:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/12\/image-36.png\" alt=\"\" class=\"wp-image-11833\" srcset=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/12\/image-36.png 721w, http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/12\/image-36-300x215.png 300w\" sizes=\"auto, (max-width: 721px) 100vw, 721px\" \/><\/a><\/figure>\n\n\n\n<p>\u4e3b\u306b cli_oootypes \u306b\u5404\u7a2e\u306e\u30af\u30e9\u30b9\u5b9a\u7fa9\u304c\u5165\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/12\/image-37.png\"><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"547\" src=\"https:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/12\/image-37.png\" alt=\"\" class=\"wp-image-11834\" srcset=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/12\/image-37.png 750w, http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2025\/12\/image-37-300x219.png 300w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/a><\/figure>\n\n\n\n<p>\u3053\u308c\u3092\u3046\u307e\u304f\u30d4\u30c3\u30af\u30a2\u30c3\u30d7\u3057\u306a\u304c\u3089 *.pyi \u306e\u30b9\u30bf\u30d6\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u3063\u3066\u3044\u3051\u3070\u3088\u3044\u308f\u3051\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u3053\u308c\u306f\u4eca\u5f8c\u306e\u8ab2\u984c\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u304a\u307e\u3051\u3068\u3057\u3066\u3001C# \u3067\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u306e\u304b\uff1f<\/h2>\n\n\n\n<p>\u7d50\u8ad6\u304b\u3089\u8a00\u3046\u3068\u73fe\u72b6\u306e 25.8.3 \u3067\u306f\u52d5\u4f5c\u3057\u307e\u305b\u3093\u3002\u3053\u308c\u306f .NET \u95a2\u4fc2\u3067\u4f5c\u3089\u308c\u3066\u3044\u308b\u30e9\u30c3\u30d1\u30fc\u304c\u304a\u304b\u3057\u3044\u306e\u304b\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u304a\u304b\u3057\u3044\u306e\u304b\u4e0d\u660e\u3067\u3059\u304c\u3001UNO API \u30b5\u30fc\u30d0\u30fc\u306b\u7e4b\u304c\u308a\u307e\u305b\u3093\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing System;\nusing System.Diagnostics;\nusing uno;\nusing uno.util;\nusing unoidl.com.sun.star.bridge;\nusing unoidl.com.sun.star.container;\nusing unoidl.com.sun.star.frame;\nusing unoidl.com.sun.star.lang;\nusing unoidl.com.sun.star.sheet;\nusing unoidl.com.sun.star.table;\nusing unoidl.com.sun.star.text;\nusing unoidl.com.sun.star.uno;\nusing unoidl.com.sun.star.beans;\n\nclass Program\n{\n    static void Main()\n    {\n        try\n        {\n            Console.WriteLine(&quot;Starting...&quot;);\n            string&#x5B;] programPathCandidates =\n            {\n                @&quot;C:\\\\Program Files\\\\LibreOffice\\\\program&quot;,\n                @&quot;C:\\\\Program Files (x86)\\\\LibreOffice\\\\program&quot;\n            };\n\n            string? libreOfficeProgramPath = null;\n            foreach (var p in programPathCandidates)\n            {\n                if (System.IO.Directory.Exists(p))\n                {\n                    libreOfficeProgramPath = p;\n                    break;\n                }\n            }\n\n            if (libreOfficeProgramPath == null)\n            {\n                Console.WriteLine(&quot;LibreOffice program folder not found. Please adjust path.&quot;);\n                foreach (var p in programPathCandidates)\n                {\n                    Console.WriteLine($&quot;Tried: {p}&quot;);\n                }\n                return;\n            }\n\n            Console.WriteLine($&quot;UNO_PATH target: {libreOfficeProgramPath}&quot;);\n\n            \/\/ Ensure UNO can locate LibreOffice binaries.\n            Environment.SetEnvironmentVariable(&quot;UNO_PATH&quot;, libreOfficeProgramPath, EnvironmentVariableTarget.Process);\n            var bootstrapIni = System.IO.Path.Combine(libreOfficeProgramPath, &quot;fundamental.ini&quot;);\n            Environment.SetEnvironmentVariable(&quot;URE_BOOTSTRAP&quot;, $&quot;vnd.sun.star.pathname:{bootstrapIni}&quot;, EnvironmentVariableTarget.Process);\n\n            string ureBin = System.IO.Path.Combine(libreOfficeProgramPath, &quot;..\\\\URE\\\\bin&quot;);\n            string newPathSegment = libreOfficeProgramPath + &quot;;&quot; + ureBin;\n\n            string? currentPath = Environment.GetEnvironmentVariable(&quot;PATH&quot;) ?? string.Empty;\n            if (!currentPath.Contains(libreOfficeProgramPath, StringComparison.OrdinalIgnoreCase) || !currentPath.Contains(ureBin, StringComparison.OrdinalIgnoreCase))\n            {\n                Environment.SetEnvironmentVariable(&quot;PATH&quot;, newPathSegment + &quot;;&quot; + currentPath, EnvironmentVariableTarget.Process);\n            }\n\n            Environment.SetEnvironmentVariable(&quot;UNO_SERVICES&quot;, System.IO.Path.Combine(libreOfficeProgramPath, &quot;uno_services.rdb&quot;), EnvironmentVariableTarget.Process);\n\n            \/\/ Some environments need the working directory inside the LibreOffice program folder.\n            Environment.CurrentDirectory = libreOfficeProgramPath;\n\n            \/\/ Start (or reuse) LibreOffice headless with a socket connector.\n            var sofficePath = System.IO.Path.Combine(libreOfficeProgramPath, &quot;soffice.exe&quot;);\n            var acceptArg = &quot;--accept=socket,host=localhost,port=2002;urp;StarOffice.ServiceManager&quot;;\n\n            if (!System.IO.File.Exists(sofficePath))\n            {\n                Console.WriteLine($&quot;soffice.exe not found at {sofficePath}&quot;);\n                return;\n            }\n\n            bool sofficeRunning = Process.GetProcessesByName(&quot;soffice.bin&quot;).Length &gt; 0;\n            if (!sofficeRunning)\n            {\n                Console.WriteLine(&quot;Starting soffice headless...&quot;);\n                var psi = new ProcessStartInfo\n                {\n                    FileName = sofficePath,\n                    Arguments = $&quot;--headless --nologo --norestore --nodefault {acceptArg}&quot;,\n                    UseShellExecute = false,\n                    CreateNoWindow = true,\n                    WorkingDirectory = libreOfficeProgramPath\n                };\n                Process.Start(psi);\n                \/\/ Give soffice time to open the socket.\n                System.Threading.Thread.Sleep(TimeSpan.FromSeconds(5));\n            }\n\n            Console.WriteLine(&quot;Creating initial UNO context...&quot;);\n            XComponentContext localContext = Bootstrap.defaultBootstrap_InitialComponentContext();\n            XMultiComponentFactory localSmgr = localContext.getServiceManager();\n            XUnoUrlResolver resolver = (XUnoUrlResolver)localSmgr.createInstanceWithContext(&quot;com.sun.star.bridge.UnoUrlResolver&quot;, localContext);\n\n            Console.WriteLine(&quot;Resolving remote context via socket...&quot;);\n            XComponentContext remoteContext = (XComponentContext)resolver.resolve(&quot;uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext&quot;);\n\n            XMultiComponentFactory serviceManager = remoteContext.getServiceManager();\n            XDesktop? desktop = serviceManager.createInstanceWithContext(&quot;com.sun.star.frame.Desktop&quot;, remoteContext) as XDesktop;\n            if (desktop == null)\n            {\n                Console.WriteLine(&quot;Failed to acquire LibreOffice desktop.&quot;);\n                return;\n            }\n\n            Console.WriteLine(&quot;Desktop acquired&quot;);\n\n            XComponentLoader? loader = desktop as XComponentLoader;\n            if (loader == null)\n            {\n                Console.WriteLine(&quot;Failed to acquire component loader.&quot;);\n                return;\n            }\n\n            Console.WriteLine(&quot;Component loader OK&quot;);\n\n            \/\/ Reuse the active Calc document, or create a new one if none is open.\n            Console.WriteLine(&quot;Checking current component...&quot;);\n            XSpreadsheetDocument? calcDocument = desktop.getCurrentComponent() as XSpreadsheetDocument;\n            if (calcDocument == null)\n            {\n                Console.WriteLine(&quot;No active Calc. Creating new...&quot;);\n                var args = Array.Empty&lt;PropertyValue&gt;();\n                var component = loader.loadComponentFromURL(&quot;private:factory\/scalc&quot;, &quot;_blank&quot;, 0, args);\n                calcDocument = component as XSpreadsheetDocument;\n            }\n\n            if (calcDocument == null)\n            {\n                Console.WriteLine(&quot;No Calc document available.&quot;);\n                return;\n            }\n\n            Console.WriteLine(&quot;Calc document ready&quot;);\n\n            \/\/ Access the first sheet and write text to cell A1.\n            XSpreadsheets sheets = calcDocument.getSheets();\n            XIndexAccess sheetAccess = (XIndexAccess)sheets;\n            Any sheetAny = (Any)sheetAccess.getByIndex(0);\n            XSpreadsheet? sheet = sheetAny.Value as XSpreadsheet;\n            if (sheet == null)\n            {\n                Console.WriteLine(&quot;Failed to access the first sheet.&quot;);\n                return;\n            }\n\n            Console.WriteLine(&quot;Sheet acquired&quot;);\n\n            XCell cell = sheet.getCellByPosition(0, 0);\n            cell.setFormula(&quot;Hello by C#&quot;);\n\n            Console.WriteLine(&quot;Value written to A1.&quot;);\n        }\n        catch (System.Exception ex)\n        {\n            Console.WriteLine($&quot;Failed to write to Calc: {ex.Message}&quot;);\n            Console.WriteLine(ex);\n        }\n    }\n}\n<\/pre><\/div>\n\n\n<p>\u7d50\u679c<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nPS H:\\LibreOffice\\net-ref&gt; dotnet run\nStarting...\nUNO_PATH target: C:\\\\Program Files\\\\LibreOffice\\\\program\nCreating initial UNO context...\nFailed to write to Calc: External component has thrown an exception.\nSystem.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.\n   at cppu.defaultBootstrap_InitialComponentContext(Reference&lt;com::sun::star::uno::XComponentContext&gt;*)\n   at uno.util.Bootstrap.defaultBootstrap_InitialComponentContext(String ini_file, IDictionaryEnumerator bootstrap_parameters)\n   at uno.util.Bootstrap.defaultBootstrap_InitialComponentContext()\n   at Program.Main() in H:\\LibreOffice\\net-ref\\Program.cs:line 97\nPS H:\\LibreOffice\\net-ref&gt; \n<\/pre><\/div>\n\n\n<p>\u5b9f\u884c\u3059\u308b\u3068\u3001XComponentContext localContext = Bootstrap.defaultBootstrap_InitialComponentContext(); \u306e\u4f4d\u7f6e\u3067\u4f8b\u5916\u304c\u767a\u751f\u3057\u3066\u3044\u3066\u81f4\u547d\u7684\u3067\u3059\u3002\u3044\u308f\u3086\u308b LibreOffice \u304c\u30db\u30b9\u30c8\u3057\u3066\u3044\u308b\u30b5\u30fc\u30d0\u30fc\u306b\u7e4b\u304c\u3089\u305a\u6700\u521d\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u304c\u751f\u6210\u3067\u304d\u3066\u3044\u307e\u305b\u3093\u3002Python \u304b\u3089\u306f\u7e4b\u3052\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u306e\u3067\u3001<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlocal_ctx = uno.getComponentContext()\n<\/pre><\/div>\n\n\n<p>\u307e\u3042\u3001 .NET \u95a2\u4fc2\u304c\u304a\u304b\u3057\u3044\u306e\u3067\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<p>\u3072\u3068\u307e\u305a\u3001.NET \u95a2\u4fc2\u3067\u52d5\u304b\u3059\u306e\u306f\u8ae6\u3081\u3066\u3001\u7d20\u76f4\u306b Python \u3067\u306e\u5b9f\u88c5\u3092\u8003\u3048\u307e\u3059\u3002\u3064\u307e\u308a\u306f *.pyi \u306e\u30b9\u30bf\u30d6\u3092\u3082\u3046\u5c11\u3057\u5145\u5b9f\u3055\u305b\u307e\u3059\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u7d42\u7684\u306b\u306f Python \u304b VB \u3042\u305f\u308a\u3067\u52d5\u304b\u3057\u305f\u3044\u306e\u3067\u3059\u304c\u3001\u3061\u3087\u3063\u3068\u5bc4\u308a\u9053\u3002 Python \u30b3\u30fc\u30c9\u3092\u66f8\u304f\u3068\u304d\u306b\u578b\u30c1\u30a7\u30c3\u30af\u304c\u51fa\u6765\u306a\u304f\u3066\u56f0\u3063\u3066\u3044\u307e\u3059&#8230;\u3068\u8a00\u3044\u307e\u3059\u304b\u3001Python \u306e\u5834\u5834\u5408\u306b\u306f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3092\u3059 &hellip; <a href=\"http:\/\/www.moonmile.net\/blog\/archives\/11830\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,109],"tags":[],"class_list":["post-11830","post","type-post","status-publish","format-standard","hentry","category-dev","category-libreoffice"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/11830","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/comments?post=11830"}],"version-history":[{"count":4,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/11830\/revisions"}],"predecessor-version":[{"id":11839,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/11830\/revisions\/11839"}],"wp:attachment":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/media?parent=11830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/categories?post=11830"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/tags?post=11830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}