仕事で、彼らはタスクを設定しました-私のプログラムの予算オプションを書くため(以前は、MS Officeがレポートに使用されていました)。 すべての資料はさまざまなブログに散らばっており、ほとんどが英語で書かれているため、このトピックに関する資料が不足しているため、非常に困惑していました。 今日、私はこの問題での経験は少しではありますが共有することにしました。
C#とOOo3を「再結合」するために最初に必要なのは、プログラムがOpenOffice.orgと連携するための「CLI _ *。Dll」ライブラリです。 OpenOffice.org 2では、それらはオフィスによって一緒に提供およびインストールされますが、ディストリビューション3ではインストールされません。 そこで、[openofficeorg1.cab] OOo3アーカイブ(インストール中にすべてのアーカイブが選択した場所に抽出されます)からペンでそれらを取り出し(すべてのCLI _ *。DLLライブラリ)、プロジェクトの参照に追加します。 追加する場合、ローカルおよび特定バージョンのコピーライブラリごとにfalseのフラグを立てることが重要です!
また、OOo3でのこの「苦痛」はそこで終わりません。 以下は、OOo3固有のコードです。
名前空間のリスト:
using System;
using System.Collections. Generic ;
using System.Text;
using System.Drawing;
using System.IO;
//OOo 3
using unoidl.com.sun.star.lang;
using UNO = unoidl.com.sun.star.uno;
using unoidl.com.sun.star.bridge;
using unoidl.com.sun.star.frame;
using OOo = unoidl.com.sun.star;
using Microsoft.Win32;
using System.Runtime.InteropServices;
* This source code was highlighted with Source Code Highlighter .
private void InitOO3Env()
{
string baseKey = "" ;
if (Marshal.SizeOf( typeof ( IntPtr )) == 8) baseKey = @"SOFTWARE\Wow6432Node\OpenOffice.org\" ;
else
baseKey = @"SOFTWARE\OpenOffice.org\" ;
// Get the URE directory
string key = baseKey + @"Layers\URE\1" ;
RegistryKey reg = Registry.CurrentUser.OpenSubKey(key);
if (reg == null ) reg = Registry.LocalMachine.OpenSubKey(key);
string urePath = reg.GetValue( "UREINSTALLLOCATION" ) as string ;
reg.Close();
urePath = Path.Combine(urePath, "bin" );
// Get the UNO Path
key = baseKey + @"UNO\InstallPath" ;
reg = Registry.CurrentUser.OpenSubKey(key);
if (reg == null ) reg = Registry.LocalMachine.OpenSubKey(key);
string unoPath = reg.GetValue( null ) as string ;
reg.Close();
string path;
string sysPath = System.Environment.GetEnvironmentVariable( "PATH" );
path = string .Format( "{0};{1}" , System.Environment.GetEnvironmentVariable( "PATH" ), urePath);
System.Environment.SetEnvironmentVariable( "PATH" , path);
System.Environment.SetEnvironmentVariable( "UNO_PATH" , unoPath);
}
* This source code was highlighted with Source Code Highlighter .
上記のコードは、OOo3のいわゆる「初期化」を生成します(OOo3の環境変数を設定します)。 PATH変数は保存されないため、プログラムを起動するたびに「初期化」を実行する必要があります。
OOoを使用できるようになりました(バージョン2以降の場合[以前のバージョンを単純に試したことはありません]):
// ?
private bool isOOoInstalled()
{
string baseKey;
// 64
if (Marshal.SizeOf( typeof ( IntPtr )) == 8) baseKey = @"SOFTWARE\Wow6432Node\OpenOffice.org\" ;
else
baseKey = @"SOFTWARE\OpenOffice.org\" ;
string key = baseKey + @"Layers\URE\1" ;
RegistryKey reg = Registry.CurrentUser.OpenSubKey(key);
if (reg == null ) reg = Registry.LocalMachine.OpenSubKey(key);
string urePath = reg.GetValue( "UREINSTALLLOCATION" ) as string ;
reg.Close();
if (urePath != null ) return true ;
else
return false ;
}
// ( \ )
private OOo.lang.XMultiServiceFactory uno_connect( String [] args)
{
InitOO3Env();
unoidl.com.sun.star.uno.XComponentContext m_xContext m_xContext = uno.util.Bootstrap.bootstrap();
if (m_xContext != null )
return (OOo.lang.XMultiServiceFactory)m_xContext.getServiceManager();
else
return null ;
}
// \ Calc
private unoidl.com.sun.star.sheet.XSpreadsheetDocument OOo3_initCalcDocument( string filePath, bool newDoc)
{
XComponentLoader aLoader;
XComponent xComponent = null ;
string url = newDoc ? "private:factory/scalc" : @"file:///" + filePath.Replace( @"\" , @"/" );
try
{
aLoader = (XComponentLoader)
mxMSFactory.createInstance( "com.sun.star.frame.Desktop" );
xComponent = aLoader.loadComponentFromURL(
/*"private:factory/scalc"*/ url, "_blank" , 0,
new unoidl.com.sun.star.beans.PropertyValue[0]);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return (OOo.sheet.XSpreadsheetDocument)xComponent;
}
// \ Writer
private OOo.text.XTextDocument OOo3_initWriterDocument( string filePath、 bool newDoc)
{
XComponentLoader aLoader;
XComponent xComponent = null ;
文字列 url = newDoc? 「プライベート:ファクトリー/ライター」 : @ "ファイル:///" + filePath.Replace( @ "\" 、 @ "/" );
試してみる
{
aLoader =(XComponentLoader)
mxMSFactory.createInstance( "com.sun.star.frame.Desktop" );
xComponent = aLoader.loadComponentFromURL(
url、 「_ blank」 、0、
new unoidl.com.sun.star.beans.PropertyValue [0]);
}
catch (例外ex)
{
MessageBox.Show(ex.Message);
}
return (OOo.text.XTextDocument)xComponent;
}
*このソースコードは、 ソースコードハイライターで強調表示されました。
それだけです!