Ich möchte, dass meine Benutzer Daten aus Excel in verschiedenen Seiten importieren Benutzererstellung, etc. importierenWie können Daten aus Excel dynamisch in Java importiert werden?
Ich habe den folgenden Code (Apache POI) versucht, aber nicht in der Lage, es dynamisch zu lesen.
Es fragt mich nach der URL des Dateispeicherorts. Ich kann es angeben, wenn es von lokal liest. Aber ich möchte das dynamisch. Wie spezifiziert man die URL?
Gibt es einen anderen Weg oder eine Bibliothek, um damit umzugehen?
public class ApachePOIExcelRead {
//private static final String FILE_NAME = "/Users/apple/Desktop/Import.xlsx";
public List<User> readExcel(Import imports) {
System.out.println(imports.getFile().toString());
String FILE_NAME = "data:" + imports.getFileContentType()+";base64,"+imports.getData().toString();
List<User> userList = new ArrayList<User>();
try {
System.out.println("Import file content type is " + imports.getFileContentType());
if("application/vnd.ms-excel".equalsIgnoreCase(imports.getFileContentType()) || "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".equalsIgnoreCase(imports.getFileContentType())){
System.out.println("Content type is as exepcted");
byte[] file = imports.getFile();
FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
excelFile.read(file);
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
List<TestPOJO> pojoList = new ArrayList<TestPOJO>();
while (iterator.hasNext()) {
System.out.println("Iterator has next row");
Row currentRow = iterator.next();
if(currentRow.getRowNum() != 0) {
//Exit the first row since its not needed.
Iterator<Cell> cellIterator = currentRow.iterator();
TestPOJO testPOJO = new TestPOJO();
while (cellIterator.hasNext()) {
System.out.println("cell iterator has next value");
Cell currentCell = cellIterator.next();
switch (currentCell.getColumnIndex())
{
case 0 :
String idData = currentCell.getStringCellValue();
if (DataValidator.isDataNumber(idData)) {
testPOJO.setId(Integer.parseInt(idData));
System.out.println("Id is" +testPOJO.getId());
}
else {
throw new Exception("Data not correct");
}
break;
case 1:
String passwordData = currentCell.getStringCellValue();
if(!DataValidator.isDataInvalid(passwordData)) {
testPOJO.setPassword(currentCell.getStringCellValue());
System.out.println("password is" +testPOJO.getPassword());
}
else {
throw new Exception("Data not correct");
}
break;
case 2 :
String firstName = currentCell.getStringCellValue();
if(DataValidator.isDataContainingOnlyString(firstName)) {
testPOJO.setFirstName(firstName);
System.out.println("First name is" +testPOJO.getFirstName());
}
else {
throw new Exception("Data not correct");
}
break;
case 3:
String lastName = currentCell.getStringCellValue();
if(DataValidator.isDataContainingOnlyString(lastName)) {
testPOJO.setLastName(lastName);
System.out.println("Last Name is" +testPOJO.getLastName());
}
else {
throw new Exception("Data not correct");
}
break;
case 4 :
String emailId = currentCell.getStringCellValue();
if(DataValidator.isDataInCorrectEmailFormat(emailId)) {
testPOJO.setEmailId(currentCell.getStringCellValue());
System.out.println("Email Id is" +testPOJO.getEmailId());
}
else {
throw new Exception("Data not correct");
}
break;
}
}
pojoList.add(testPOJO);
}
}
System.out.println("LIST SIZE" +pojoList.size());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return userList;
}
}
versuchen Sie einen Blick auf http://StackOverflow.com/Questions/494869/File-change-Listener-in-Java –