|
From: <de...@us...> - 2004-01-06 14:10:02
|
Update of /cvsroot/babeldoc/babeldoc/modules/conversion/src/com/babeldoc/conversion/excel
In directory sc8-pr-cvs1:/tmp/cvs-serv942/modules/conversion/src/com/babeldoc/conversion/excel
Modified Files:
Tag: V1-2
ExcelConverter.java
Log Message:
- Excel converter now can read cells with formulas
- Numbers are parsed using format specified in Excel cells and given locale. Locale can be specified as config option in XlsToXml pipeline stage
Index: ExcelConverter.java
===================================================================
RCS file: /cvsroot/babeldoc/babeldoc/modules/conversion/src/com/babeldoc/conversion/excel/ExcelConverter.java,v
retrieving revision 1.1
retrieving revision 1.1.4.1
diff -C2 -d -r1.1 -r1.1.4.1
*** ExcelConverter.java 19 Jul 2003 13:16:47 -0000 1.1
--- ExcelConverter.java 6 Jan 2004 14:09:58 -0000 1.1.4.1
***************
*** 1,187 ****
! /* ====================================================================
! * The Apache Software License, Version 1.1
! *
! * Copyright (c) 2000 The Apache Software Foundation. All rights
! * reserved.
! *
! * Redistribution and use in source and binary forms, with or without
! * modification, are permitted provided that the following conditions
! * are met:
! *
! * 1. Redistributions of source code must retain the above copyright
! * notice, this list of conditions and the following disclaimer.
! *
! * 2. Redistributions in binary form must reproduce the above copyright
! * notice, this list of conditions and the following disclaimer in
! * the documentation and/or other materials provided with the
! * distribution.
! *
! * 3. The end-user documentation included with the redistribution,
! * if any, must include the following acknowledgment:
! * "This product includes software developed by the
! * Apache Software Foundation (http://www.apache.org/)."
! * Alternately, this acknowledgment may appear in the software itself,
! * if and wherever such third-party acknowledgments normally appear.
! *
! * 4. The names "Apache" and "Apache Software Foundation" must
! * not be used to endorse or promote products derived from this
! * software without prior written permission. For written
! * permission, please contact ap...@ap....
! *
! * 5. Products derived from this software may not be called "Apache",
! * nor may "Apache" appear in their name, without prior written
! * permission of the Apache Software Foundation.
! *
! * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
! * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
! * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
! * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
! * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
! * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
! * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
! * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
! * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
! * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
! * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! * SUCH DAMAGE.
! * ====================================================================
! *
! * This software consists of voluntary contributions made by many
! * individuals on behalf of the Apache Software Foundation. For more
! * information on the Apache Software Foundation, please see
! * <http://www.apache.org/>.
! *
! * Portions of this software are based upon public domain software
! * originally written at the National Center for Supercomputing Applications,
! * University of Illinois, Urbana-Champaign.
! * ====================================================================
! *
! * Babeldoc: The Universal Document Processor
! *
! * $Header$
! * $DateTime$
! * $Author$
! *
! */
! package com.babeldoc.conversion.excel;
!
! import com.babeldoc.conversion.ConversionException;
! import org.apache.poi.hssf.usermodel.HSSFCell;
! import org.apache.poi.hssf.usermodel.HSSFRow;
! import org.apache.poi.hssf.usermodel.HSSFSheet;
! import org.apache.poi.hssf.usermodel.HSSFWorkbook;
! import org.apache.poi.poifs.filesystem.POIFSFileSystem;
! import org.dom4j.Document;
! import org.dom4j.DocumentHelper;
! import org.dom4j.Element;
!
! import java.io.IOException;
! import java.io.InputStream;
!
! /**
! * Simple class to toXml an input stream of an excel file to an
! * xml output stream
! *
! * @author dejank
! */
! public class ExcelConverter {
!
! /**
! * Convert a input stream of excel file as input to an xml document.
! *
! * @param in excel file stream
! * @return the document
! * @throws com.babeldoc.conversion.ConversionException
! */
! public Document toXml(InputStream in)
! throws ConversionException {
!
! POIFSFileSystem fs;
! try {
! fs = new POIFSFileSystem(in);
! HSSFWorkbook wb = new HSSFWorkbook(fs);
!
! HSSFSheet sheet = null;
! HSSFRow row = null;
! HSSFCell cell = null;
!
! Document document = DocumentHelper.createDocument();
! //
! Element root = document.addElement("workbook");
!
! int numberOfSheets = wb.getNumberOfSheets();
! root.addAttribute("number-of-sheets",
! String.valueOf(numberOfSheets));
!
! for (int i = 0; i < numberOfSheets; i++) {
! Element sheetElement = root.addElement("sheet");
! sheet = wb.getSheetAt(i);
! String sheetName = wb.getSheetName(i);
! sheetElement.addAttribute("sheet-name", sheetName);
! sheetElement.addAttribute("sheet-number",
! (new Integer(i)).toString());
!
! int rowCount = 0;
! int firstRowNum = sheet.getFirstRowNum();
! int lastRowNum = sheet.getLastRowNum();
!
! for (int m = firstRowNum; m <= lastRowNum; m++) {
! row = sheet.getRow(m);
! if (row == null)
! continue;
! Element rowElement = sheetElement.addElement("row");
! rowElement.addAttribute("row-number",
! (new Integer(m)).toString());
! rowCount++;
! int firstCellNum = row.getFirstCellNum();
! int lastCellNum = row.getLastCellNum();
! int cellCount = 0;
! for (int p = firstCellNum; p <= lastCellNum; p++) {
! cell = row.getCell((short) p);
! // if (cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) continue;
! if (cell == null)
! continue;
!
! cellCount++;
! Element cellElement = rowElement.addElement("cell");
! cellElement.addAttribute("cell-number", String.valueOf(cellCount));
! cellElement.addAttribute("cell-colnum",
! String.valueOf(cell.getCellNum()));
!
! int cellType = cell.getCellType();
! switch (cellType) {
! case HSSFCell.CELL_TYPE_NUMERIC :
! cellElement.addAttribute("type", "Numeric");
! cellElement.addText(
! String.valueOf(cell.getNumericCellValue()));
! break;
! case HSSFCell.CELL_TYPE_STRING :
! cellElement.addAttribute("type", "String");
! cellElement.addText(cell.getStringCellValue());
! break;
! case HSSFCell.CELL_TYPE_BOOLEAN :
! cellElement.addAttribute("type", "Boolean");
! cellElement.addText(
! String.valueOf(cell.getBooleanCellValue()));
! break;
! case HSSFCell.CELL_TYPE_BLANK :
! cellElement.addAttribute("type", "N/A");
! break;
! default :
! break;
! }
! }
! rowElement.addAttribute(
! "number-of-cells",
! String.valueOf(cellCount));
! }
! sheetElement.addAttribute(
! "number-of-rows",
! String.valueOf(rowCount));
! }
! return document;
! } catch(IOException iox) {
! throw new ConversionException("", iox);
! }
! }
! }
--- 1,254 ----
! /* ====================================================================
! * The Apache Software License, Version 1.1
! *
! * Copyright (c) 2000 The Apache Software Foundation. All rights
! * reserved.
! *
! * Redistribution and use in source and binary forms, with or without
! * modification, are permitted provided that the following conditions
! * are met:
! *
! * 1. Redistributions of source code must retain the above copyright
! * notice, this list of conditions and the following disclaimer.
! *
! * 2. Redistributions in binary form must reproduce the above copyright
! * notice, this list of conditions and the following disclaimer in
! * the documentation and/or other materials provided with the
! * distribution.
! *
! * 3. The end-user documentation included with the redistribution,
! * if any, must include the following acknowledgment:
! * "This product includes software developed by the
! * Apache Software Foundation (http://www.apache.org/)."
! * Alternately, this acknowledgment may appear in the software itself,
! * if and wherever such third-party acknowledgments normally appear.
! *
! * 4. The names "Apache" and "Apache Software Foundation" must
! * not be used to endorse or promote products derived from this
! * software without prior written permission. For written
! * permission, please contact ap...@ap....
! *
! * 5. Products derived from this software may not be called "Apache",
! * nor may "Apache" appear in their name, without prior written
! * permission of the Apache Software Foundation.
! *
! * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
! * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
! * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
! * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
! * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
! * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
! * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
! * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
! * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
! * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
! * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! * SUCH DAMAGE.
! * ====================================================================
! *
! * This software consists of voluntary contributions made by many
! * individuals on behalf of the Apache Software Foundation. For more
! * information on the Apache Software Foundation, please see
! * <http://www.apache.org/>.
! *
! * Portions of this software are based upon public domain software
! * originally written at the National Center for Supercomputing Applications,
! * University of Illinois, Urbana-Champaign.
! * ====================================================================
! *
! * Babeldoc: The Universal Document Processor
! *
! * $Header$
! * $DateTime$
! * $Author$
! *
! */
! package com.babeldoc.conversion.excel;
!
! import com.babeldoc.conversion.ConversionException;
! import com.babeldoc.core.LogService;
!
! import org.apache.poi.hssf.model.Workbook;
! import org.apache.poi.hssf.usermodel.HSSFCell;
! import org.apache.poi.hssf.usermodel.HSSFDataFormat;
! import org.apache.poi.hssf.usermodel.HSSFRow;
! import org.apache.poi.hssf.usermodel.HSSFSheet;
! import org.apache.poi.hssf.usermodel.HSSFWorkbook;
! import org.apache.poi.poifs.filesystem.POIFSFileSystem;
! import org.dom4j.Document;
! import org.dom4j.DocumentHelper;
! import org.dom4j.Element;
!
! import java.io.IOException;
! import java.io.InputStream;
! import java.text.DecimalFormat;
! import java.text.DecimalFormatSymbols;
! import java.text.NumberFormat;
! import java.text.SimpleDateFormat;
! import java.util.Date;
! import java.util.HashMap;
! import java.util.Iterator;
! import java.util.Locale;
!
! /**
! * Simple class to toXml an input stream of an excel file to an
! * xml output stream
! *
! * @author dejank
! */
! public class ExcelConverter {
!
! /**
! * Convert a input stream of excel file as input to an xml document.
! *
! * @param in excel file stream
! * @return the document
! * @throws com.babeldoc.conversion.ConversionException
! */
! private HashMap workBookAttributes = new HashMap();
! private String locale = null;
!
! public Document toXml(InputStream in)
! throws ConversionException {
!
! POIFSFileSystem fs;
! try {
! fs = new POIFSFileSystem(in);
! HSSFWorkbook wb = new HSSFWorkbook(fs);
!
! HSSFSheet sheet = null;
! HSSFRow row = null;
! HSSFCell cell = null;
! HSSFDataFormat dataFormat = wb.createDataFormat();
! Document document = DocumentHelper.createDocument();
! DecimalFormat formatter;
! if (locale == null) {
! formatter = (DecimalFormat) NumberFormat.getInstance();
! } else {
! formatter = (DecimalFormat) NumberFormat.getInstance(new Locale(locale));
! }
! Element root = document.addElement("workbook");
!
! int numberOfSheets = wb.getNumberOfSheets();
! root.addAttribute("number-of-sheets",
! String.valueOf(numberOfSheets));
!
! //this code is used for specifying additional attributes to
! //resulting XML document.
! if (workBookAttributes != null) {
! Iterator iter = workBookAttributes.keySet().iterator();
! while (iter.hasNext()) {
! String name = (String) iter.next();
! String value = (String) workBookAttributes.get(name);
! root.addAttribute(name, value);
! }
! }
!
! for (int i = 0; i < numberOfSheets; i++) {
! Element sheetElement = root.addElement("sheet");
! sheet = wb.getSheetAt(i);
! String sheetName = wb.getSheetName(i);
! sheetElement.addAttribute("sheet-name", sheetName);
! sheetElement.addAttribute("sheet-number",
! (new Integer(i)).toString());
!
! int rowCount = 0;
! int firstRowNum = sheet.getFirstRowNum();
! int lastRowNum = sheet.getLastRowNum();
!
! for (int m = firstRowNum; m <= lastRowNum; m++) {
! row = sheet.getRow(m);
! if (row == null)
! continue;
! Element rowElement = sheetElement.addElement("row");
! rowElement.addAttribute("row-number",
! (new Integer(m)).toString());
! rowCount++;
! int firstCellNum = row.getFirstCellNum();
! int lastCellNum = row.getLastCellNum();
! int cellCount = 0;
! for (int p = firstCellNum; p <= lastCellNum; p++) {
! cell = row.getCell((short) p);
! // if (cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) continue;
! if (cell == null)
! continue;
!
! cellCount++;
! Element cellElement = rowElement.addElement("cell");
! cellElement.addAttribute("cell-number", String.valueOf(cellCount));
! cellElement.addAttribute("cell-colnum",
! String.valueOf(cell.getCellNum()));
!
! int cellType = cell.getCellType();
! switch (cellType) {
! case HSSFCell.CELL_TYPE_NUMERIC:
! case HSSFCell.CELL_TYPE_FORMULA :
!
! double value = cell.getNumericCellValue();
!
! try {
!
! String format = dataFormat.getFormat(cell.getCellStyle().getDataFormat());
! formatter.applyPattern(format);
! cellElement.addText(formatter.format(value));
! } catch (Exception e) {
! //set the value without formatting
! LogService.getInstance(this.getClass().getName()).logWarn("Error trying to format number" + e);
! cellElement.addText(String.valueOf(value));
! }
! cellElement.addAttribute("type", "Numeric");
! //cellElement.addText(String.valueOf(value));
! break;
! case HSSFCell.CELL_TYPE_STRING :
! cellElement.addAttribute("type", "String");
! cellElement.addText(cell.getStringCellValue());
! break;
! case HSSFCell.CELL_TYPE_BOOLEAN :
! cellElement.addAttribute("type", "Boolean");
! cellElement.addText(
! String.valueOf(cell.getBooleanCellValue()));
! break;
! case HSSFCell.CELL_TYPE_BLANK :
! cellElement.addAttribute("type", "N/A");
! break;
! default :
! break;
! }
! }
! rowElement.addAttribute(
! "number-of-cells",
! String.valueOf(cellCount));
! }
! sheetElement.addAttribute(
! "number-of-rows",
! String.valueOf(rowCount));
! }
! return document;
! } catch(IOException iox) {
! throw new ConversionException("", iox);
! }
! }
!
!
! /**
! * @return Returns the workBookAttributes.
! */
! public HashMap getWorkBookAttributes() {
! return workBookAttributes;
! }
! /**
! * @param workBookAttributes The workBookAttributes to set.
! */
! public void setWorkBookAttributes(HashMap workBookAttributes) {
! this.workBookAttributes = workBookAttributes;
! }
!
!
!
! /**
! * @param locale The locale to set.
! */
! public void setLocale(String locale) {
! this.locale = locale;
! }
! }
|