[Uno, C#] Problem on the release of resource

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
Guiguis
Posts: 5
Joined: Thu Oct 30, 2008 10:37 am

[Uno, C#] Problem on the release of resource

Post by Guiguis »

Problem on the release of resource C # :

The prog must convert a file calc in a file Excel 2007 (ods -> xls).
At the end of the file xls, it remains locked for a period of 10 seconds.
What will not meet my needs .. I need my speed over available

Ex (code, reflexion and object) :

public void ConvertToExcel()
{
// textBox1.Text : name of file.ods
if(textBox1.Text == null || textBox1.Text.Equals(string.Empty) || (!System.IO.File.Exists(textBox1.Text)))
return;

string fileName = textBox1.Text;


//Create a new ServiceManager Type object
Type tServiceManager = Type.GetTypeFromProgID("com.sun.star.ServiceManager",true);
//Create a new ServiceManager Com object using our
//ServiceManager type object
object oServiceManager = System.Activator.CreateInstance(tServiceManager);
//Create our Desktop Com object
object[] args1 = new object[] {"com.sun.star.frame.Desktop" };

object oDesktop = oServiceManager.GetType().InvokeMember("createinstance",
BindingFlags.InvokeMethod, null,
oServiceManager, args1) as object;



XComponent oComponent = getXComponent(fileName);


// Output :
string fileExcelName = @"C:\test.xls";


unoidl.com.sun.star.beans.PropertyValue[] outProps = new unoidl.com.sun.star.beans.PropertyValue[3];
outProps[0] = new unoidl.com.sun.star.beans.PropertyValue();
outProps[0].Name = "FilterName";
outProps[0].Value = new uno.Any("MS Excel 97");

outProps[1] = new unoidl.com.sun.star.beans.PropertyValue();
outProps[1].Name = "Overwrite";
outProps[1].Value = new uno.Any(true);

outProps[2] = new unoidl.com.sun.star.beans.PropertyValue();
outProps[2].Name = "OutputStream";

outProps[2].Value = new uno.Any(
typeof(unoidl.com.sun.star.io.XOutputStream),
new XOutputStreamWrapper(System.IO.File.Create(fileExcelName)));


((unoidl.com.sun.star.frame.XStorable)oComponent).storeToURL("private:stream", outProps);


oComponent.dispose();
oComponent = null;


//oDesktop.terminate();



GC.Collect();
GC.WaitForPendingFinalizers();


}// End Methode
(...)



private XComponent getXComponent(string inputFile)
{
XComponentContext localContext = uno.util.Bootstrap.bootstrap();
XMultiServiceFactory multiServiceFactory = (XMultiServiceFactory)localContext.getServiceManager();
XComponentLoader componentLoader = (XComponentLoader)multiServiceFactory.createInstance("com.sun.star.frame.Desktop");

PropertyValue[] propertyValue = new PropertyValue[1];
PropertyValue aProperty = new PropertyValue();
aProperty.Name = "Hidden";
aProperty.Value = new uno.Any(true);
propertyValue[0] = aProperty;

string pathFile = PathConverter(inputFile);

XComponent xComponent = componentLoader.loadComponentFromURL(pathFile, "_blank", 0, propertyValue);
return xComponent;
}



Please, who can help me ?
thank you in advance
OOo 2.3.X on Ms Windows XP + ms server 2003
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Uno, C#] Problem on the release of resource

Post by Villeroy »

OOo does not save the Excel 2007 crap (OOXML, *.xlsx). You may try the Novel suite instead.
 Edit: I see, you mean binary xls. 
http://www.oooninja.com/2008/02/batch-c ... -with.html is a script which is well known to convert "anything-to-anything" in kind of "server mode".
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Guiguis
Posts: 5
Joined: Thu Oct 30, 2008 10:37 am

Re: [Uno, C#] Problem on the release of resource

Post by Guiguis »

Solution :
(OUPS, file open and not closed)



unoidl.com.sun.star.beans.PropertyValue[] outProps = new unoidl.com.sun.star.beans.PropertyValue[3];
outProps[0] = new unoidl.com.sun.star.beans.PropertyValue();
outProps[0].Name = "FilterName";
outProps[0].Value = new uno.Any("MS Excel 97");

outProps[1] = new unoidl.com.sun.star.beans.PropertyValue();
outProps[1].Name = "Overwrite";
outProps[1].Value = new uno.Any(true);

outProps[2] = new unoidl.com.sun.star.beans.PropertyValue();
outProps[2].Name = "OutputStream";


using (System.IO.FileStream newFileExcel = System.IO.File.Create(fileExcelName))
{
XOutputStreamWrapper outputWapperFile = new XOutputStreamWrapper(newFileExcel);
outProps[2].Value = new uno.Any(
typeof(unoidl.com.sun.star.io.XOutputStream), outputWapperFile);


((unoidl.com.sun.star.frame.XStorable)oComponent).storeToURL("private:stream", outProps);


oComponent.dispose();
oComponent = null;


//oDesktop.terminate();

outputWapperFile.closeOutput();
newFileExcel.Close();
// This is my nervous ticks ;), it's not.. with le class of framwork.....
}
GC.Collect();
GC.WaitForPendingFinalizers();
OOo 2.3.X on Ms Windows XP + ms server 2003
Guiguis
Posts: 5
Joined: Thu Oct 30, 2008 10:37 am

Re: [Uno, C#] Problem on the release of resource

Post by Guiguis »

Thank you for your help!

Excel ... :mrgreen: I made sharepoint :twisted:

Good day ;).
OOo 2.3.X on Ms Windows XP + ms server 2003
Guiguis
Posts: 5
Joined: Thu Oct 30, 2008 10:37 am

Re: [Uno, C#] Problem on the release of resource

Post by Guiguis »

public void ConvertToExcel(string inputFile, string outputFile)
{
string fileName = string.Empty;

if (inputFile == null || inputFile.Equals(string.Empty) || (!System.IO.File.Exists(inputFile)))
{
// Log,
// throw new Excep.....
return;
}

fileName = inputFile;

#region Open file calc

XComponent oComponent = null;
oComponent = getXComponent(fileName);

#endregion Open file calc


#region Save Excel

string fileExcelName = outputFile;

unoidl.com.sun.star.beans.PropertyValue[] outProps = new unoidl.com.sun.star.beans.PropertyValue[3];
outProps[0] = new unoidl.com.sun.star.beans.PropertyValue();
outProps[0].Name = "FilterName";
outProps[0].Value = new uno.Any("MS Excel 97");

outProps[1] = new unoidl.com.sun.star.beans.PropertyValue();
outProps[1].Name = "Overwrite";
outProps[1].Value = new uno.Any(true);

outProps[2] = new unoidl.com.sun.star.beans.PropertyValue();
outProps[2].Name = "OutputStream";


using (System.IO.FileStream newFileExcel = System.IO.File.Create(fileExcelName))
{
XOutputStreamWrapper outputWapperFile = new XOutputStreamWrapper(newFileExcel);
outProps[2].Value = new uno.Any(
typeof(unoidl.com.sun.star.io.XOutputStream), outputWapperFile);


((unoidl.com.sun.star.frame.XStorable)oComponent).storeToURL("private:stream", outProps);


oComponent.dispose();
oComponent = null;


//oDesktop.terminate();

outputWapperFile.closeOutput();
newFileExcel.Close();
}
GC.Collect();
GC.WaitForPendingFinalizers();

#endregion Save Excel
}



/// <summary>
///
/// </summary>
/// <param name="inputFile"></param>
/// <returns></returns>
private XComponent getXComponent(string inputFile)
{
XComponentContext localContext = uno.util.Bootstrap.bootstrap();
XMultiServiceFactory multiServiceFactory = (XMultiServiceFactory)localContext.getServiceManager();
XComponentLoader componentLoader = (XComponentLoader)multiServiceFactory.createInstance("com.sun.star.frame.Desktop");

PropertyValue[] propertyValue = new PropertyValue[1];
PropertyValue aProperty = new PropertyValue();
aProperty.Name = "Hidden";
aProperty.Value = new uno.Any(true);
propertyValue[0] = aProperty;

string pathFile = PathConverter(inputFile);

XComponent xComponent = componentLoader.loadComponentFromURL(pathFile, "_blank", 0, propertyValue);
return xComponent;
}

/// <summary>
///
/// "File///C:/pathFilmeName"
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
private static string PathConverter(string file)
{
try
{
file = file.Replace(@"\", "/");
return "file:///" + file;
}

catch (System.Exception ex)
{
throw ex;
}

}
OOo 2.3.X on Ms Windows XP + ms server 2003
Post Reply