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 :

     private void removeFirstRow(XComponent xDoc) throws Exception {

         //get the speadsheet object
         XSpreadsheetDocument sDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, xDoc);
         Object o = sDoc.getSheets().getByName("Sheet1");
         XSpreadsheet xSheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, o);

         //get the rows
         XColumnRowRange xRange = (XColumnRowRange) UnoRuntime.queryInterface(XColumnRowRange.class, xSheet);
         XTableRows rows = xRange.getRows();

         //delete the first row
         rows.removeByIndex(0, 1);

     }

As usual, I welcome your comments and feedbacks.

Enjoy !

  1. No comments yet.
(will not be published)