More Related Content
Similar to Read Excel File Java
Similar to Read Excel File Java (20)
Read Excel File Java
- 1. import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Iterator;
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;
public class ReadExcelDemo
{
public static void main(String[] args)
{
try
{
FileInputStream file = new FileInputStream(new
File("ValidateCVDeviceByObligationDomain.xlsx"));
//Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);
//Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
int count =0;
HashMap <String,String> hn = new HashMap<String, String>();
while (rowIterator.hasNext())
{
Row row = rowIterator.next();
count++;
int cell_count=0;
//For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
String content = null;
//Check the cell type and format accordingly
cell_count++;
if(count==1)
{
switch (cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
content=String.valueOf(cell.getNumericCellValue());
//System.out.print(cell.getNumeri
cCellValue() + "t");
break;
case Cell.CELL_TYPE_STRING:
content=cell.getStringCellValue();
//System.out.print(cell.getString
CellValue() + "t");
break;
}
/*System.out.println("cell_count