Thursday, January 16, 2014

Read content from XML File and write into EXCEL

package com.DH.Explorations;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

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 com.eibus.xml.nom.Document;
import com.eibus.xml.nom.Node;
import com.eibus.xml.nom.XMLException;
import com.eibus.xml.xpath.XPath;

public class WriteExcel {

public static void main(String[] args) throws IOException, XMLException {
// TODO Auto-generated method stub
HSSFWorkbook workbook=new HSSFWorkbook();
Document doc = new Document();
int configXML=0;
configXML = doc.load("D:\\bFOIntegrations\\WorkOrderInfo.xml");
System.out.println(Node.writeToString(configXML, true));
HSSFSheet Sheet=workbook.createSheet("WorkOrderRecords") ;
int []workOrderItems = XPath.getMatchingNodes(".//WorkItemRecord", null, configXML);
for(int i=0;i<workOrderItems.length;i++)
{
Row row = Sheet.createRow(i);
String bFO = Node.getAttribute(XPath.getFirstMatch(".//Keys", null, workOrderItems[i]), "bFO");
String BO = Node.getAttribute(XPath.getFirstMatch(".//Keys", null, workOrderItems[i]), "BO");
String Status = Node.getDataElement(workOrderItems[i], "Status", "");
String statusMsg =Node.getDataElement(workOrderItems[i], "StatusMsg", "");
Cell cell_0 = row.createCell(0);
Cell cell_1 = row.createCell(1);
Cell cell_2 = row.createCell(2);
Cell cell_3 = row.createCell(3);
cell_0.setCellValue(bFO);
cell_1.setCellValue(BO);
cell_2.setCellValue(Status);
cell_3.setCellValue(statusMsg);
}
File fileDirectory;
String FilePath="D:\\temp";
fileDirectory = new File(FilePath);
if(!fileDirectory.exists())
fileDirectory.mkdirs();
FilePath=FilePath+"\\"+(new Date().getTime())+".xls";
FileOutputStream out = new FileOutputStream(new File(FilePath));
workbook.write(out);
}
}

No comments:

Post a Comment