MS Excel transformation into XML
405161Sep 8 2004 — edited Nov 17 2004I've got a Excel workbook from which I reading from using the code below(read using Apache POI API), at the moment it just echoes back the cell information
to the screen. Next step is to take this information and transform it into a XML format based on the workbook. The final step will be store this XML in a Oracle table.
Anyone done this before and know how to do the transformation without hard-coding the process, and as the workbook can be a few Kb to a few Mb so I'd prefer a method that doesn't store the XML in memory.
Also storing the XML in the table, any suggestions for doing this [factors I need to take into account, etc].
---------------------------------------------------------
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.logging.*;
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;
public class DemoPOI_1
{
static DemoPOI_1 demoPOI;
private static Log logger = LogFactory.getLog(DemoPOI_1.class);
InputStream spreadsheet ;
private static HSSFWorkbook workbook ;
private static HSSFSheet sheet ;
private static HSSFRow row ;
private static HSSFCell cell ;
static final String strFNFE = "FileNotFoundException";
static final String strIOE = "IOException";
static final String strWk = "Excel Workbook cannot be opened";
public DemoPOI_1()
{
}
boolean IsExcelWorkBookOpen()
{
boolean bIsExcelWorkBookOpen = true;
try
{
spreadsheet = new FileInputStream("F:\\data 2002.xls");
workbook = new HSSFWorkbook(spreadsheet);
}
catch (FileNotFoundException fnfe)
{
errorMessage(strFNFE);
bIsExcelWorkBookOpen = false;
}
catch (IOException ioe)
{
errorMessage(strIOE);
bIsExcelWorkBookOpen = false;
}
return bIsExcelWorkBookOpen;
}
void errorMessage(String strMessage)
{
logger.error("ERROR MESSAGE" + strMessage);
}
String ProcessRow(int nNoOfRows)
{
for (int nLoop=0 ; nLoop < nNoOfRows ; nLoop++ )
{
row = sheet.getRow(nLoop);
if (row != null)
{
demoPOI.ProcessCell(row.getLastCellNum());
System.out.println();
}
}
return null;
}
String ProcessCell(int nNoOfCells)
{
for (int nLoop=0 ; nLoop < nNoOfCells ; nLoop++ )
{
cell = row.getCell((short)nLoop);
if (cell != null)
{
switch (cell.getCellType())
{
case cell.CELL_TYPE_BLANK :
{
break;
}
case cell.CELL_TYPE_BOOLEAN :
{
System.out.print(cell.getBooleanCellValue() + " ");
break;
}
case cell.CELL_TYPE_ERROR :
{
System.out.print(cell.getErrorCellValue() + " ");
break;
}
case cell.CELL_TYPE_FORMULA :
{
//System.out.print(cell. + " ");
break;
}
case cell.CELL_TYPE_NUMERIC :
{
System.out.print(cell.getNumericCellValue() + " ");
break;
}
case cell.CELL_TYPE_STRING :
{
System.out.print(cell.getStringCellValue() + " ");
break;
}
default :
{
}
}
}
}
return null;
}