I have the code below which reads excel files and displays it in java. I'd like to implement the code after reading the data from the excel file to java, it will to convert in XML format and save it on XML file.
Any code sample or reference will be thankful;
public class POIExcelReader {
public POIExcelReader (){
}
public void displayFromExcel (String xlsPath)
{
InputStream inputStream = null;
try
{
inputStream = new FileInputStream (xlsPath);
}
catch (FileNotFoundException e)
{
System.out.println ("File not found in the specified path.");
e.printStackTrace ();
}
POIFSFileSystem fileSystem = null;
try {
fileSystem = new POIFSFileSystem (inputStream);
HSSFWorkbook workBook = new HSSFWorkbook (fileSystem);
HSSFSheet sheet = workBook.getSheetAt (0);
Iterator<?> rows = sheet.rowIterator ();
while (rows.hasNext ())
{
HSSFRow row = (HSSFRow) rows.next();
// display row number
System.out.println ("Row No.: " + row.getRowNum ());
// get a row, iterate through cells.
Iterator<?> cells = row.cellIterator ();
while (cells.hasNext ())
{
HSSFCell cell = (HSSFCell) cells.next ();
//System.out.println ("Cell : " + cell.getCellNum ());
switch (cell.getCellType ())
{
case HSSFCell.CELL_TYPE_NUMERIC :
{
// NUMERIC CELL TYPE
System.out.println ("Numeric: " + cell.getNumericCellValue ());
break;
}
case HSSFCell.CELL_TYPE_STRING :
{
// STRING CELL TYPE
HSSFRichTextString richTextString = cell.getRichStringCellValue ();
System.out.println ("String: " + richTextString.getString ());
break;
}
default:
{
// types other than String and Numeric.
System.out.println ("Type not supported.");
break;
}
}
}
}
}
catch(IOException e)
{
e.printStackTrace ();
}
}
public static void main (String[] args)
{
POIExcelReader poiExample = new POIExcelReader ();
String xlsPath ="c://Users//Secured//Desktop//artikli.xls";
poiExample.displayFromExcel (xlsPath);
}
}
You can use the classes in the package: javax.xml.parsers. The package provides classes allowing the processing of XML documents. e.g. DocumentBuilder, DocumentBuilderFactory, to mention a few.
Using the java code (extended your original code) below and the file located in this location: http://base.google.com/base/products.xls
}