You are on page 1of 4

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.quan.

excel; /** * * @author sanjeev.das */ import org.apache.poi.hssf.usermodel.*; import org.w3c.dom.*; import java.io.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; public class ExcelToXML { public void generateXML(File excelFile,String locInfo) { try { InputStream input = new FileInputStream(excelFile); HSSFWorkbook workbook = new HSSFWorkbook(input); HSSFSheet spreadsheet = workbook.getSheetAt(0); HSSFSheet spreadsheet1 = workbook.getSheetAt(1); HSSFRow row0 = spreadsheet.getRow(0);//This will point the CI Sprea dsheet HSSFRow row2 = spreadsheet1.getRow(0);//This will point the Rel ation Spreadsheet //Initializing the XML document DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.newDocument(); Element rootElement = document.createElement("GRLoader"); document.appendChild(rootElement); //Creating top-level elements Element element = null; for (int i = 1; i <= spreadsheet.getLastRowNum(); i++){ element = document.createElement("ci"); rootElement.appendChild(element); //Adding first subelements Element subelement = null; for (int j = 0; j < row0.getLastCellNum(); j++){ if(row0.getCell((short)j).getCellType() == HSSFCell.CELL_TYPE_NUMERIC){ subelement = document.cr eateElement(String.valueOf(row0.getCell((short)j).getNumericCellValue())); }else{ subelement = document.cr eateElement(row0.getCell((short)j).getStringCellValue()); } if(subelement==null) { subelement = document.createElement("NA");

} if(row0.getCell ((short)j).getCellType() == HSSFCell.CELL_TYPE_NUMERIC){ if((String.valueOf(row0.getCell((short)j).getNumericCellValue()).equals("resourc e_contact"))) { subelement.setAttribute("lookup","userid"); } }else{ if((row0.getCell((short)j).getStringCellValue()).equals("resource_contact")) { subelement.setAttribute("lookup","userid"); } } element.appendChild(subelement); HSSFRow row1 = spreadsheet.getRow(i); if(row1.getCell((short)j).getCellType() == HSSFCell.CELL_TYPE_NUMERIC){ subelement.appendChild(d ocument.createTextNode(String.valueOf(row1.getCell((short)j).getNumericCellValue ()))); }else{ subelement.appendChild(d ocument.createTextNode(row1.getCell((short)j).getStringCellValue())); } } } //Below section is Relation XML Element subRootElement = null;//this is for relation under root Element GRLoader for (int i = 1; i <= spreadsheet1.getLastRowNum(); i++){ subRootElement = document.createElement("relation"); rootElement.appendChild(subRootElement); //Adding first subelements Element subelement = null; // This is for simple element or Tex t Element Element subelement1 = null;//This is for Complex Elemen t i.e. provider under subRootElement i.e. relation Element subelement2 = null;//This is for Complex Elemen t i.e. dependent under subRootElement i.e. relation Element tempelem = null;// For storing temporary Sub Ro ot Element value i.e. relation for (int j = 0; j < row2.getLastCellNum(); j++){ if(j==1){ subelement1 = document.createElement("provider") ; subRootElement.appendChild(subelement1); tempelem = subRootElement;

subRootElement = subelement1; } if (j==6){ subelement2 = document.createElement("dependen t"); subRootElement = tempelem; subRootElement.appendChild(subelement2); subRootElement = subelement2; } if(row2.getCell((short)j ).getCellType() == HSSFCell.CELL_TYPE_NUMERIC){ subeleme nt = document.createElement(String.valueOf(row2.getCell((short)j).getNumericCell Value())); }else{ subeleme nt = document.createElement(row2.getCell((short)j).getStringCellValue()); } if(subelement==null) { subelement = document.createElement("NA"); } subRootElement.appendChild(subelement); HSSFRow row3 = spreadsheet1.getRow(i); if(row3.getCell((short)j ).getCellType() == HSSFCell.CELL_TYPE_NUMERIC){ subeleme nt.appendChild(document.createTextNode(String.valueOf(row3.getCell((short)j).get NumericCellValue()))); }else{ subeleme nt.appendChild(document.createTextNode(row3.getCell((short)j).getStringCellValue ())); } } } TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); //Add indentation to output transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-a mount", "2"); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(new File(locInfo)); transformer.transform(source, result); } catch (IOException e) { System.out.println("IOException " + e.getMessage()); } catch (ParserConfigurationException e) { System.out .println("ParserConfigurationException " + e.getMessage()); } catch (TransformerConfigurationException e) { System.out.println("TransformerConfigurationException "+ e.getMessa

ge()); } catch (TransformerException e) { System.out.println("TransformerException " + e.getMessage()); } } public static void main(String[] argv) { ExcelToXML excel = new ExcelToXML(); File input = new File(argv[0]); excel.generateXML(input,argv[1]); } }

You might also like