📜 ⬆️ ⬇️

We are friends with C # and OpenOffice.org

Do not judge strictly, this is my first article

We set the task at work - to write a budget version for my program (previously MS Office was used for reports). He was very puzzled due to the lack of materials on this topic, since all the materials are scattered across different blogs, and they are mostly in English. Today I decided to share my, albeit a little experience in this issue.

The first thing we need to “reunite” C # and OOo3 are the “CLI _ *. Dll” libraries for our program to work with OpenOffice.org. In OpenOffice.org 2, they are shipped and installed together by the office, in the 3rd distribution, they are not installed. So we take and pull them out (all CLI _ *. Dll libraries) with pens from the [openofficeorg1.cab] OOo3 archive (during installation, all the archives are extracted to the location you choose), add them to your project's References. When adding it is important to note the values ​​for each Copy Local and Specific Version library to false!
Also on this “torment” with OOo3 does not end. Next, some OOo3-specific code:
List of namespaces:

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 .


The code above performs the so-called “initialization” of OOo3 (sets the environment variables for OOo3). “Initialization” should be performed every time the program is started, since the PATH variable is not saved.

Now it is possible to work with OOo (for version 2 and higher [Versions haven’t been tried before simply]):
// ?
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 ;
string url = newDoc? "Private: factory / swriter" : @ "file: ///" + filePath.Replace ( @ "\" , @ "/" );
try
{
aLoader = (XComponentLoader)
mxMSFactory.createInstance ( "com.sun.star.frame.Desktop" );

xComponent = aLoader.loadComponentFromURL (
url, "_blank" , 0,
new unoidl.com.sun.star.beans.PropertyValue [0]);
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
}
return (OOo.text.XTextDocument) xComponent;
}


* This source code was highlighted with Source Code Highlighter .

That's all!

Source: https://habr.com/ru/post/49373/


All Articles