public void handleFileUpload(FileUploadEvent fileUpload) throws IOException, InvalidFormatException {
RequestContext context = RequestContext.getCurrentInstance();
List<Excel> excels = new ArrayList<Excel>();
try {
String fileName = fileUpload
.getFile()
.getFileName()
.substring(
fileUpload.getFile().getFileName()
.indexOf("."));
// Store file in user home
copyFile(fileName, fileUpload.getFile().getInputstream());
// Get stored file
File file = new File(System.getProperty("user.home") + "/"
+ fileName);
// Get the workbook instance for XLS file
Workbook workbook = WorkbookFactory
.create(new FileInputStream(file));
// Get first sheet from the workbook
Sheet sheet = workbook.getSheetAt(0);
// Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Excel exc = new Excel();
System.out.println("A1 :" + " ");
System.out.println("b1 :" + " ");
System.out.println("c1 :" + " ");
excels.add(exc);
}
} catch (IOException e) {
System.out.println(e);
}
}
/**
* Save uploaded File into user home dir
*
* @param fileName
* @param in
*/
private void copyFile(String fileName, InputStream in) {
try {
OutputStream out = new FileOutputStream(new File(
System.getProperty("user.home") + "/" + fileName));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = in.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
in.close();
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Saludos
