ユーザーがさまざまなページでExcelからデータをインポートして、ユーザー作成などと言うようにしたいJavaでExcelからデータを動的にインポートする方法は?
私は以下のコード(Apache POI)を試しましたが、それを動的に読み取ることはできませんでした。
ファイルの場所のURLを問い合わせています。私はそれがローカルから読んでいるならそれを指定することができます。しかし、私はこれを動的にしたい。 URLを指定する方法は?
これを処理する方法やライブラリはありますか?
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;
}
}
http://stackoverflow.com/questions/494869/file-changed-listener-in-javaを見てみましょう –