This article will teach you on how to open and read Microsoft Excel 2010 file using Apache POI.
download Apache POI jar file here:
Below are the following code:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class TestExcel {
private static String localPath;
private static String fileName;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
InputStream inputStream = null;
try {
System.out.println("Reading Excel File: " +
localPath + "/" + fileName);
InputStream = new FileInputStream(localPath + "/" + fileName);
XSSFWorkbook workBook = new XSSFWorkbook (inputStream);
XSSFSheet sheet = workBook.getSheetAt(0);
System.out.println("Getting sheet... ");
int cellCount = 0;
System.out.println("Cell counting... ");
for(int i=0; i < 1; i++){
XSSFRow rowHeader = sheet.getRow(i);
cellCount = rowHeader.getLastCellNum();
}
System.out.println("cellcount: " + cellCount);
for(int i=1; i < sheet.getLastRowNum() + 1; i++){
XSSFRow row = sheet.getRow(i);
System.out.println(row.getCell((short) 2));
}
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
download Apache POI jar file here:
Below are the following code:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class TestExcel {
private static String localPath;
private static String fileName;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
InputStream inputStream = null;
try {
System.out.println("Reading Excel File: " +
localPath + "/" + fileName);
InputStream = new FileInputStream(localPath + "/" + fileName);
XSSFWorkbook workBook = new XSSFWorkbook (inputStream);
XSSFSheet sheet = workBook.getSheetAt(0);
System.out.println("Getting sheet... ");
int cellCount = 0;
System.out.println("Cell counting... ");
for(int i=0; i < 1; i++){
XSSFRow rowHeader = sheet.getRow(i);
cellCount = rowHeader.getLastCellNum();
}
System.out.println("cellcount: " + cellCount);
for(int i=1; i < sheet.getLastRowNum() + 1; i++){
XSSFRow row = sheet.getRow(i);
System.out.println(row.getCell((short) 2));
}
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
No comments:
Post a Comment