Read excel file and convert it to XML format in Java




//ReadExcelFile.java

package com.excel;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.w3c.dom.Document;

class ReadExcel {

public List readExcel(String filePath, String extention) {
List contractList = new ArrayList();
Iterator rows = null;
ArrayList heading = new ArrayList();
int i = 0;
if (extention !=null && "xls".equalsIgnoreCase(extention)) {
rows = readXlsFile(filePath);
} else {
rows = readXlsxFile(filePath);
}
if (rows != null) {
while (rows.hasNext()) {
Iterator cellIterator = rows.next().cellIterator();//cellIterator();
int j = 0;
if (cellIterator != null) {
boolean isAdded = false;
Contract contract = new Contract();
while (cellIterator.hasNext()) {
String cell = cellIterator.next().getStringCellValue();

if (i == 0) {
heading.add(cell.toUpperCase());
} else {
String headingName = heading.get(j);
if (headingName.equalsIgnoreCase("NAME")) {
contract.setName(cell);
//contractList.add(contract);
isAdded = true;
}
if (headingName.equalsIgnoreCase("CITY")) {
contract.setCity(cell);
isAdded = true;
//contractList.add(contract);
}
}
j++;
}
if (isAdded) {
contractList.add(contract);
}
}
i++;
}

}
return contractList;
}
//////////////////
public Iterator readXlsFile(String filePath) {
// FileInputStream file = new FileInputStream(new File("C:\\test.xls"));
FileInputStream file;
Iterator rowIterator = null;
try {
file = new FileInputStream(new File(filePath));


//Get the workbook instance for XLS file
HSSFWorkbook workbook;

workbook = new HSSFWorkbook(file);

//Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(0);

//Get iterator to all the rows in current sheet
rowIterator = sheet.iterator();
//Get iterator to all cells of current row
//Iterator cellIterator = row.cellIterator();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rowIterator;
}

public Iterator readXlsxFile(String filePath) {
Iterator rowIterator = null;
try {
FileInputStream file = new FileInputStream(new File(filePath));
//Get the workbook instance for XLS file
XSSFWorkbook workbook = new XSSFWorkbook (file);

//Get first sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);

//Get iterator to all the rows in current sheet
rowIterator = sheet.iterator();

//Get iterator to all cells of current row
//Iterator cellIterator = row.cellIterator();
} catch (Exception e) {
e.printStackTrace();
}
return rowIterator;
}

}



public class ReadExcelFile {

/**
* @param args
* @throws JAXBException
* @throws ParserConfigurationException
* @throws TransformerException
*/
public static void main(String[] args) throws JAXBException, ParserConfigurationException, TransformerException {
ReadExcel re = new ReadExcel();
List list = re.readExcel("C:\\Test\\test.xls", "xls");
System.out.println(list.toString());
Contract c = new Contract();
c.setList(list);
// Create the JAXBContext
JAXBContext jc = JAXBContext.newInstance(Contract.class);

// Create the Document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.newDocument();

// Marshal the Object to a Document
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(c, document);

// Output the Document
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(System.out);
t.transform(source, result);
}

}
package com.excel;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.w3c.dom.Document;

class ReadExcel {

public List readExcel(String filePath, String extention) {
List contractList = new ArrayList();
Iterator rows = null;
ArrayList heading = new ArrayList();
int i = 0;
if (extention !=null && "xls".equalsIgnoreCase(extention)) {
rows = readXlsFile(filePath);
} else {
rows = readXlsxFile(filePath);
}
if (rows != null) {
while (rows.hasNext()) {
Iterator cellIterator = rows.next().cellIterator();//cellIterator();
int j = 0;
if (cellIterator != null) {
boolean isAdded = false;
Contract contract = new Contract();
while (cellIterator.hasNext()) {
String cell = cellIterator.next().getStringCellValue();

if (i == 0) {
heading.add(cell.toUpperCase());
} else {
String headingName = heading.get(j);
if (headingName.equalsIgnoreCase("NAME")) {
contract.setName(cell);
//contractList.add(contract);
isAdded = true;
}
if (headingName.equalsIgnoreCase("CITY")) {
contract.setCity(cell);
isAdded = true;
//contractList.add(contract);
}
}
j++;
}
if (isAdded) {
contractList.add(contract);
}
}
i++;
}

}
return contractList;
}
//////////////////
public Iterator readXlsFile(String filePath) {
// FileInputStream file = new FileInputStream(new File("C:\\test.xls"));
FileInputStream file;
Iterator rowIterator = null;
try {
file = new FileInputStream(new File(filePath));


//Get the workbook instance for XLS file
HSSFWorkbook workbook;

workbook = new HSSFWorkbook(file);

//Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(0);

//Get iterator to all the rows in current sheet
rowIterator = sheet.iterator();
//Get iterator to all cells of current row
//Iterator cellIterator = row.cellIterator();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rowIterator;
}

public Iterator readXlsxFile(String filePath) {
Iterator rowIterator = null;
try {
FileInputStream file = new FileInputStream(new File(filePath));
//Get the workbook instance for XLS file
XSSFWorkbook workbook = new XSSFWorkbook (file);

//Get first sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);

//Get iterator to all the rows in current sheet
rowIterator = sheet.iterator();

//Get iterator to all cells of current row
//Iterator cellIterator = row.cellIterator();
} catch (Exception e) {
e.printStackTrace();
}
return rowIterator;
}

}



public class ReadExcelFile {

/**
* @param args
* @throws JAXBException
* @throws ParserConfigurationException
* @throws TransformerException
*/
public static void main(String[] args) throws JAXBException, ParserConfigurationException, TransformerException {
ReadExcel re = new ReadExcel();
List list = re.readExcel("C:\\Test\\test.xls", "xls");
System.out.println(list.toString());
Contract c = new Contract();
c.setList(list);
// Create the JAXBContext
JAXBContext jc = JAXBContext.newInstance(Contract.class);

// Create the Document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.newDocument();

// Marshal the Object to a Document
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(c, document);

// Output the Document
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
DOMSource source = new DOMSource(document);
// StreamResult result = new StreamResult(System.out);
//t.transform(source, result);
ByteArrayOutputStream f = new ByteArrayOutputStream();
StreamResult result = new StreamResult(f);
t.transform(source, result);
String aString = new String(f.toByteArray(),"UTF-8");
System.out.println(aString);
}

}



//Contract.java
Package come. excel;

import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Contract {
private String name;
private String city;
List list;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Contract [name=" + name + ", city=" + city + "]";
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}

}

Comments

Popular posts from this blog

XML Parsing in IBM BPM

Type conversion in IBM BPM

Parse the JSON in IBM BPM