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 java.math.BigDecimal; 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 s) { 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 Spread sheet HSSFRow row2 = spreadsheet1.getRow(0);//This will point the Relation 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_T YPE_NUMERIC) { subelement = document.createElement(String.valueOf(row0. getCell((short) j).getNumericCellValue())); } else { subelement = document.createElement(row0.getCell((short) j).getStringCellValue()); } if (subelement == null) { subelement = document.createElement("NA"); }

if (row0.getCell((short) j).getCellType() == HSSFCell.CELL_T YPE_NUMERIC) { if ((String.valueOf(row0.getCell((short) j).getNumericCe llValue()).equals("resource_contact"))) { subelement.setAttribute("lookup", "userid"); } } else { if ((row0.getCell((short) j).getStringCellValue()).equal s("resource_contact")) { subelement.setAttribute("lookup", "userid"); } } element.appendChild(subelement); HSSFRow row1 = spreadsheet.getRow(i); if (row1.getCell((short) j).getCellType() == HSSFCell.CELL_T YPE_NUMERIC) { String s1 = String.valueOf(row1.getCell((short) j).getNu mericCellValue()); if(s1.equals("NA")) { s1=""; subelement.appendChild(document.createTextNode(s1)); } //subelement.appendChild(document.createTextNode(String. valueOf(row1.getCell((short) j).getNumericCellValue()))); else { BigDecimal decimal = new BigDecimal(row1.getCell((short) j).getNumericCellValue()); String str = String.valueOf(decimal); subelement.appendChild(document.createTextNode(str)); } continue; } String s2 = String.valueOf(row1.getCell((short) j).getString CellValue()); if (s2.equals("NA")) { s2 = ""; } subelement.appendChild(document.createTextNode(s2)); } } //Below section is Relation XML Element subRootElement = null;//this is for relation under rootEleme nt 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 Text Element Element subelement1 = null;//This is for Complex Element i.e. pr ovider under subRootElement i.e. relation Element subelement2 = null;//This is for Complex Element i.e. de pendent under subRootElement i.e. relation

Element tempelem = null;// For storing temporary Sub Root Elemen t 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("dependent"); subRootElement = tempelem; subRootElement.appendChild(subelement2); subRootElement = subelement2; } if (row2.getCell((short) j).getCellType() == HSSFCell.CELL_T YPE_NUMERIC) { subelement = document.createElement(String.valueOf(row2. getCell((short) j).getNumericCellValue())); } else { subelement = 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_T YPE_NUMERIC) { String s1 = String.valueOf(row3.getCell((short) j).getNu mericCellValue()); if(s1.equals("NA")) { s1=""; subelement.appendChild(document.createTextNode(s1)); } //subelement.appendChild(document.createTextNode(String. valueOf(row3.getCell((short) j).getNumericCellValue()))); else { BigDecimal decimal = new BigDecimal(row3.getCell((short ) j).getNumericCellValue()); String str = String.valueOf(decimal); subelement.appendChild(document.createTextNode(str)); //subelement.appendChild(document.createTextNode(row3.ge tCell((short) j).getStringCellValue())); } continue; } String s2= String.valueOf(row3.getCell((short) j).getStringC ellValue()); if(s2.equals("NA")) { s2=""; }

subelement.appendChild(document.createTextNode(s2)); } } 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-am ount", "2"); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(new File(s)); 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[] args) { ExcelToXML excel = new ExcelToXML(); File input = new File(args[0]); excel.generateXML(input, args[1]); } }

You might also like