vendredi août 22, 2008

More OpenOffice API Fun : Remove the first row in a Spreadsheet

As a follow up to my previous entry about converting Excel spreadsheet into CSV files using OpenOffice API, here is a small improvement.

The files we do receive have a first row containing the title of the columns, not any actual business data.  My intent was to skip this line while encoding the CSV file into XML, using some feature of the Open ESB Custom Encoders module.  Apparently, the Custom Encoder can not be configured in such a way, i.e. there is no possible configuration telling the Custom Encoder to skip the first line of a file.

So, I returned to the OpenOffice API and decided to remove the first row in the spreadsheet itself, before converting it to CSV.

Here is the code :

 1     private void removeFirstRow(XComponent xDoc) throws Exception {
 2 
 3         //get the speadsheet object
 4         XSpreadsheetDocument sDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, xDoc);
 5         Object o = sDoc.getSheets().getByName("Sheet1");
 6         XSpreadsheet xSheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, o);
 7 
 8         //get the rows
 9         XColumnRowRange xRange = (XColumnRowRange) UnoRuntime.queryInterface(XColumnRowRange.class, xSheet);
10         XTableRows rows = xRange.getRows();
11 
12         //delete the first row
13         rows.removeByIndex(0, 1);
14 
15     }

As usual, I welcome your comments and feedbacks.

Enjoy !

About

Sébastien Stormacq is a Senior Software Architect at Sun Microsystems. He uses his 15+ years of professional experience to design large scale, secured and highly transactional architectures based on Sun's middleware solutions.

Search

Archives
« avril 2014
lun.mar.mer.jeu.ven.sam.dim.
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today