One of my clients want to “migrate” their software infrastructure from Microsoft Office to OpenOffice. But, the main problem for them is they have used Office library in their applications. Yes, they have used “Excel.Application” and “Word.Application” ActiveX library to instantiate office documents. The term “documents” here means Word document and Excel spreadsheet.
The first solution across my mind is we have to “migrate” their ActiveX implementation to other library that can create Microsoft Office documents, for example: NPOI. But wait a minute, their "document”-generator is scripted in JavaScript! Geez……they can compose a document using JavaScript, it’s generated on-the-fly at client side, definitely not at server side!
After googling and googling, I found UNO. In simple term, UNO is a component object model for OpenOffice. UNO can be used in various programming language, such as Java, Python, C#, and of course, JavaScript. UNO objects can be instantiated using JavaScript through COM Automation. In JavaScript, we can use new ActiveXObject(name of UNO class) to instantiate UNO objects.

For example:
var svcMgr = new ActiveXObject("com.sun.star.ServiceManager");
Code above means, we create an object svcMgr as an instantiate of com.sun.star.ServiceManager class.
We can read a complete reference of UNO in the OpenOffice SDK, which can be downloaded from this link. In the SDK, we can also read a complete documentation of UNO (or we can read it at this site), including code samples.
In this post, I show you how to create a simple OpenOffice Calc spreadsheet, and save it as example.xls (which we can open it using Microsoft Excel later). But, before we start to code, we have to download the OpenOffice (I used OpenOffice 3.3) and the OpenOffice SDK.
Now, take a look at this JavaScript code:
function generateSpreadsheet() {
var svcMgr = new ActiveXObject("com.sun.star.ServiceManager");
var coreRef = svcMgr.createInstance("com.sun.star.reflection.CoreReflection");
var desktop = svcMgr.createInstance("com.sun.star.frame.Desktop");
var args = [];
var doc = desktop.loadComponentFromURL("private:factory/scalc", "_blank", 0, args);
var sheets = doc.Sheets
var sheet = sheets.getByIndex(0);
// fill data
sheet.getCellRangeByName("B2").String="ItemCode";
sheet.getCellRangeByName("C2").String="Qty";
sheet.getCellRangeByName("D2").String="Amount";
sheet.getCellRangeByName("E2").String="Total";
sheet.getCellRangeByName("B3").String="A01";
sheet.getCellRangeByName("C3").Value=2 ;
sheet.getCellRangeByName("D3").Value=100000;
sheet.getCellRangeByName("E3").Formula="=C3*D3";
sheet.getCellRangeByName("B4").String="A02";
sheet.getCellRangeByName("C4").Value=5 ;
sheet.getCellRangeByName("D4").Value=120000;
sheet.getCellRangeByName("E4").Formula="=C4*D4";
sheet.getCellRangeByName("B5").String="A01";
sheet.getCellRangeByName("C5").Value=3;
sheet.getCellRangeByName("D5").Value=200000;
sheet.getCellRangeByName("E5").Formula="=C5*D5";
sheet.getCellRangeByName("E6").Formula="=SUM(D3:D5)";
sheet.getCellRangeByName("E6").setPropertyValue("CharWeight", 150);
sheet.getCellRangeByName("E6").setPropertyValue("CharColor", 16711680);
sheet.getCellRangeByName("E6").HoriJustify = 2;
sheet.getCellRangeByName("B6:D6").merge(true);
var url = "file:///C:/example.xls";
var arg = new Array();
arg[0] = MakePropertyValue(svcMgr, "FilterName", "MS Excel 97");
try
{
doc.storeToURL(url, arg);
}
catch(e)
{
alert(e.toString());
}
doc.close(true);
doc = null;
coreRef = null;
svcMgr = null;
}
You can copy-paste code above to Notepad, and save it as a JavaScript file, such as createcalc.js
Then create a new HTML document like this one:
<html>
<head>
<script src="createcalc.js" type="text/javascript" language="javascript"></script>
</head>
<body>
<form>
<input type="button" value="print" onclick="generateSpreadsheet();" />
</form>
</body>
</html>
Save it as CreateCalc.htm, then open it using Internet Explorer (we can use IE 6 or a newest one). The result of the script is:

The hardest part to write the script above is that we don’t know the constant value of a constant parameter. Take a look at this line:
sheet.getCellRangeByName("E6").setPropertyValue("CharWeight", 150);
What is 150? Actually it is a bold constant value, in UNO, it is declared in class com.sun.star.awt.FontWeight, and the class consists some constants about font weight. Let’s take a look at the SDK folder: C:\Program Files\OpenOffice.org 3\Basis\sdk\docs\common\ref\com\sun\star\awt\FontWeight.html
To get the real constant value, take a look at the IDL folder: C:\Program Files\OpenOffice.org 3\Basis\sdk\idl\com\sun\star\awt\FontWeight.idl, it is clearly written that for the “bold” font style, the constant value must be set to 150.
Since I have to start this project immediately, if you have any experience developing office documents using UNO, please drop me an email or just comment this post. Thank you.