Controller.java
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public ResponseEntity<Object> upload(@RequestParam(value = "file") MultipartFile multipartFile) throws Exception{
ResponseObject responseObject = new ResponseObject();
// call service class method
service.excelUpload(multipartFile);
responseObject.setSuccess(true);
responseObject.setMessage("Embedded upload successful");
return new ResponseEntity(responseObject, HttpStatus.OK);
}
Service.java
@Override
public void excelUpload(MultipartFile multipartFile) throws IOException, OpenXML4JException {
InputStream inputStream = multipartFile.getInputStream();
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
List<DAOClass> daoClassList = new ArrayList<>();
for(Sheet sheet : workbook){
for (Row row : sheet){
// Set excel values to DAO class in order to save in DB
DAOClass daoClass = new DAOClass();
for (Cell cell : row){
if(cell.getColumnIndex() == 0){
daoClass.setID(cell.getNumericValue());
}else if(cell.getColumnIndex() == 1){
daoClass.setName(cell.getStringCellValue());
}else if(cell.getColumnIndex() == 2){
daoClass.setAddress(cell.getStringCellValue());
}else if(cell.getColumnIndex() == 3){
// ***Here I need help!!!***
PackagePart pPart = (PackagePart) workbook.getAllEmbedds();
String contentType = pPart.getContentType();
// Excel Workbook - either binary or OpenXML
if (contentType.equals("application/vnd.ms-excel")) {
HSSFWorkbook binaryEmbeddedWorkbook = new HSSFWorkbook(pPart.getInputStream());
System.out.println("binaryEmbeddedWorkbook: "+binaryEmbeddedWorkbook);
}
// Excel Workbook - OpenXML file format
else if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
OPCPackage docPackage = OPCPackage.open(pPart.getInputStream());
XSSFWorkbook xmlEmbeddedWorkbook = new XSSFWorkbook(docPackage);
System.out.println("xmlEmbeddedWorkbook: "+xmlEmbeddedWorkbook);
}
// Word Document - binary (OLE2CDF) file format
else if (contentType.equals("application/msword")) {
HWPFDocument hwpfDocument = new HWPFDocument(pPart.getInputStream());
System.out.println("hwpfDocument: "+hwpfDocument);
}
// Word Document - OpenXML file format
else if (contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
OPCPackage docPackage = OPCPackage.open(pPart.getInputStream());
XWPFDocument xwpfDocument = new XWPFDocument(docPackage);
System.out.println("xwpfDocument: "+xwpfDocument);
}
}else{
continue;
}
}
daoClassList.add(daoClass);
}
}
}