This is a code using for read excel sheet data as it is in java, also it display in Java console.
Before you use this code you need download and insert to this program.
Please use this URL for free download : http://poi.apache.org/download.html
import java.io.*;
import java.util.*;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class excelRead {
public static void main(String[] args) {
String fileName = "C://xls//auto_reg1.xls"; // give your actual excel sheet file path
Vector cellVectorHolder = new Vector();
try {
FileInputStream FIS = new FileInputStream(fileName);
POIFSFileSystem fileSys = new POIFSFileSystem(FIS);
HSSFWorkbook workBook = new HSSFWorkbook(fileSys);
HSSFSheet mySheet = workBook.getSheetAt(0); // reading sheet name
Iterator rowIter = mySheet.rowIterator();
/* Read values from Excel shhet*/
while (rowIter.hasNext()) {
HSSFRow myRow = (HSSFRow) rowIter.next(); //Reading Row
Iterator cellIter = myRow.cellIterator();
Vector cellStoreVector = new Vector();
while (cellIter.hasNext()) {
HSSFCell myCell = (HSSFCell) cellIter.next();
cellStoreVector.addElement(myCell);
}
cellVectorHolder.addElement(cellStoreVector);
}
/* Write a values in console*/
for (int i = 0; i < cellVectorHolder.size(); i++) {
Vector cellStoreVector = (Vector) cellVectorHolder.elementAt(i);
for (int j = 0; j < cellStoreVector.size(); j++) {
HSSFCell myCell = (HSSFCell) cellStoreVector.elementAt(j);
String stringCellValue = myCell.toString();
System.out.print(stringCellValue + "\t");
}
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.util.*;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class excelRead {
public static void main(String[] args) {
String fileName = "C://xls//auto_reg1.xls"; // give your actual excel sheet file path
Vector cellVectorHolder = new Vector();
try {
FileInputStream FIS = new FileInputStream(fileName);
POIFSFileSystem fileSys = new POIFSFileSystem(FIS);
HSSFWorkbook workBook = new HSSFWorkbook(fileSys);
HSSFSheet mySheet = workBook.getSheetAt(0); // reading sheet name
Iterator rowIter = mySheet.rowIterator();
/* Read values from Excel shhet*/
while (rowIter.hasNext()) {
HSSFRow myRow = (HSSFRow) rowIter.next(); //Reading Row
Iterator cellIter = myRow.cellIterator();
Vector cellStoreVector = new Vector();
while (cellIter.hasNext()) {
HSSFCell myCell = (HSSFCell) cellIter.next();
cellStoreVector.addElement(myCell);
}
cellVectorHolder.addElement(cellStoreVector);
}
/* Write a values in console*/
for (int i = 0; i < cellVectorHolder.size(); i++) {
Vector cellStoreVector = (Vector) cellVectorHolder.elementAt(i);
for (int j = 0; j < cellStoreVector.size(); j++) {
HSSFCell myCell = (HSSFCell) cellStoreVector.elementAt(j);
String stringCellValue = myCell.toString();
System.out.print(stringCellValue + "\t");
}
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
No comments:
Post a Comment