Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Thursday, 24 May 2018

Handaling XLS File In Java

This code will help you to handle xls file in java using apache poi.

package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;

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;

public class test {
public static void main(String[] args) {
String file="C:\\Users\\Documents\\file.xls";
File excelfile=new File(file);
FileInputStream excleFileInputStream;
try {
excleFileInputStream = new FileInputStream(excelfile);
HSSFWorkbook wb = new HSSFWorkbook(excleFileInputStream);
int noOfsheet =wb.getNumberOfSheets();
for(int i=0;i<noOfsheet;i++){
HSSFSheet sheet = wb.getSheetAt(i);
String wbName =sheet.getSheetName();
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext()){
    Row row = rowIterator.next();
    Iterator<Cell> cellIterator = row.iterator();
    String rowString="";
    while(cellIterator.hasNext()){
    Cell cell = cellIterator.next();
    rowString=rowString+cell.toString()+" ; ";
    System.out.println(rowString);
    }
   
   
    }
}

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

Sunday, 4 February 2018

Get root element of a xml/document.getDocumentElement() function in xml

your xml:

<employee>
<name>swati</name>
<id>1209202</id>
<department>audit</department>
</employee>

program:

import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;

public class Util {

public static void main(String[] args) {

String xml="<employee><name>swati</name><id>1209202</id><department>audit</department></employee>"

System.out.println("get root element of xml");
root(xml) ;

}

public static void root(String xmlSource)
throws SAXException, ParserConfigurationException, IOException {
// Parse the given input
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xmlSource)));

System.out.println("Root element in xml is:"+doc.getDocumentElement());

}
}

doc.getDocumentElement() will give the root element of your xml document.

working with xml,convert a xml string to xml document

import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;

public class Util {

public static void main(String[] args) {

String xml="<employee><name>swati</name><id>1209202</id><department>audit</department></employee>";

System.out.println("get xml document from string xml);
stringToDom(xml) ;
System.out.println("document is ready for use");

}

public static void stringToDom(String xmlSource)
throws SAXException, ParserConfigurationException, IOException {
// Parse the given input
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xmlSource)));
}

After document(doc) is created one can work on nodes,elements etc.

can save xml to files or can convert back to string and enjoy results.

I will post for more operations on xml documents.

Handaling XLS File In Java

This code will help you to handle xls file in java using apache poi. package test; import java.io.File; import java.io.FileInputStream...