}
//Dispatch.put(mExcelApplication, "Visible", new Variant(true));
try {
// Some constants needed later on
Variant what = new Variant("*");
Variant lookIn = new Variant(-4163);
Variant lookAt = new Variant(XlLookAt.xlWhole);
Variant searchOrderByRows = new Variant(XlSearchOrder.xlByRows);
Variant searchOrderByCols = new Variant(XlSearchOrder.xlByColumns);
int searchDirectionUp = XlSearchDirection.xlNext;
int searchDirectionDown = XlSearchDirection.xlPrevious;
Variant xlFalse = new Variant(false);
// Get all workbooks
Workbooks wbs = mExcelApplication.getWorkbooks();
// Open the file
java.io.File file = rawDocument.getContentAsFile();
// RB 20051106: Need to add second argument to open call, otherwise Excel
// can hang here waiting for an answer to the dialogue box asking to
// update external links
Workbook wb = wbs.open(file.getAbsolutePath(), new Variant(0));
// Collect the content of all sheets
Sheets sheets = wb.getWorksheets();
int sheetCount = sheets.getCount();
//System.out.println("Sheetcount = "+sheetCount);
StringBuffer contentBuf = new StringBuffer(DEFAULT_BUFFER_SIZE);
for (int sheetIdx = 1; sheetIdx <= sheetCount; sheetIdx++) {
Variant sheetVariant = (Variant) sheets.getItem(new Variant(sheetIdx));
Worksheet sheet = new Worksheet(sheetVariant.toDispatch());
Variant after = new Variant(sheet.getRange(new Variant("IV65536")));
Range extractRange;
// Find the top left and bottom right corner enclosing the data in the sheet
try {
// The following calls may fail because the find operation returns
// a null pointer. This exception is caught by reverting to the
// UsedRange call which also returns the used area in a spreadsheet,
// even though it is often bigger than necessary.
int firstRow = sheet.getCells().find(what, after, lookIn, lookAt, searchOrderByRows,
searchDirectionUp, xlFalse, xlFalse).getRow();
int firstCol = sheet.getCells().find(what, after, lookIn, lookAt, searchOrderByCols,
searchDirectionUp, xlFalse, xlFalse).getColumn();
int lastRow = sheet.getCells().find(what, after, lookIn, lookAt, searchOrderByRows,
searchDirectionDown, xlFalse, xlFalse).getRow();
int lastCol = sheet.getCells().find(what, after, lookIn, lookAt, searchOrderByCols,
searchDirectionDown, xlFalse, xlFalse).getColumn();
extractRange = sheet.getRange(new Variant(getCells(sheet, firstRow, firstCol)),
new Variant(getCells(sheet, lastRow, lastCol)));
//System.out.println("Sheet "+sheetIdx+" ProperUsedRange="+extractRange.getAddress());
} catch(NullPointerException e) {
extractRange = sheet.getUsedRange();
//System.out.println("Sheet "+sheetIdx+" UsedRange="+extractRange.getAddress());
}
//RB 20051106: If the sheet is empty, getUsedRange returns $A$1 and
// toSafeArray fails. Therefore we have to check that there is some text
// to extract by comparing the address of the range to the string "$A$1".
if (!extractRange.getAddress().equals("$A$1")) {
SafeArray cellArray = extractRange.getValue().toSafeArray();
int startRow = cellArray.getLBound(1);
int startCol = cellArray.getLBound(2);
int endRow = cellArray.getUBound(1);
int endCol = cellArray.getUBound(2);
for (int row = startRow; row <= endRow; row++) {
for (int col = startCol; col <= endCol; col++) {
String cellValue = cellArray.getString(row, col);
if ((cellValue != null) && (cellValue.length() != 0)) {
contentBuf.append(cellValue);
contentBuf.append(" ");
}
}
contentBuf.append("\n");
}
}
}
// Read the document properties
readProperties(wb);
// Set the content
setCleanedContent(contentBuf.toString());
// Close the workbook without saving
wb.close(new Variant(false));
}
catch (ComFailException exc) {
throw new RegainException("Using COM failed.", exc);
}
}