|
From: <de...@us...> - 2003-07-17 14:45:29
|
Update of /cvsroot/babeldoc/babeldoc/modules/conversion/src/com/babeldoc/conversion/pipeline/stage
In directory sc8-pr-cvs1:/tmp/cvs-serv17735/modules/conversion/src/com/babeldoc/conversion/pipeline/stage
Added Files:
XlsToXmlPipelineStage.java
Log Message:
Excel files to XML conversion pipeline stage
--- NEW FILE: XlsToXmlPipelineStage.java ---
/*
* Created on Jul 16, 2003
*
* To change this generated comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.babeldoc.conversion.pipeline.stage;
import java.io.IOException;
import java.util.Collection;
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 com.babeldoc.core.option.IConfigInfo;
import com.babeldoc.core.pipeline.PipelineDocument;
import com.babeldoc.core.pipeline.PipelineException;
import com.babeldoc.core.pipeline.PipelineStage;
import com.babeldoc.core.pipeline.PipelineStageInfo;
import com.babeldoc.core.pipeline.PipelineStageResult;
/**
* @author dejank
*
* To change this generated comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class XlsToXmlPipelineStage extends PipelineStage {
public static String DATE_FORMAT = "dd.mm.yyyy";
public XlsToXmlPipelineStage(IConfigInfo info) {
super(new PipelineStageInfo() {
public String getName() {
return "XlsToXml";
}
public String getDescription() {
return "Converts Microsoft Excel files to XML format.";
}
public Collection getTypeSpecificOptions() {
return null;
}
});
}
/* (non-Javadoc)
* @see com.babeldoc.core.pipeline.PipelineStage#process()
*/
public PipelineStageResult[] process() throws PipelineException {
POIFSFileSystem fs;
try {
fs = new POIFSFileSystem(this.getDocument().getInputStream());
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("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 = document.addElement("row");
rowElement.addAttribute(
"rowNum",
(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("colNum", String.valueOf(p));
cellElement.addAttribute(
"col",
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));
}
PipelineDocument doc =
new PipelineDocument(
this.getDocument(),
document.toString().getBytes());
return processHelper(doc);
} catch (IOException e) {
// TODO Auto-generated catch block
throw new PipelineException("Error converting XLS file to XML");
}
}
}
|