0
엑셀 파일을 업로드하고 읽으려고합니다. 내가 absulute 경로를 줄 때 나는 파일을 읽을 수 있지만 그것을 업로드 할 때 일하고 싶지 않다. 나는 다른 것을 시도했지만 해결책을 찾을 수 없습니다. 나는 JSP와 서블릿을 사용하지만, HTML/컨트롤러/서비스업로드 된 파일의 디렉토리를 얻으십시오. java
HTML 페이지
<h:form enctype="multipart/form-data">
<h:panelGrid>
<h:outputLabel value="Upload file: " style="font-weight:bold" />
<h:inputFile value="#{uploadController.path}" />
<h:commandButton value="Excel lezen en opslaan" action="# {uploadController.excelOpslaan}"/>
</h:panelGrid>
</h:form>
컨트롤러
private String path;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public void excelOpslaan(){
PuntenService.getExcel(path);
}
서비스
public List<String> getExcel(String path);
서비스 IMPL 아니에요
private List<String> list = new ArrayList<String>();
@Override
public List<String> getExcel(String path){
FileInputStream file = null;
OPCPackage pkg = null;
try {
file = new FileInputStream(path);
// create a new OPC Package to obtain a workbook
pkg = OPCPackage.open(file);
//Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(pkg);
//Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
//For each row, iterate through all the columns
if (row.getRowNum() <= 5) {
continue;// skip to read the first 7 row of file
}
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType())
{
case Cell.CELL_TYPE_STRING:
list.add(cell.getStringCellValue());
break;
}
}
//System.out.println("");
}
file.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}