How to import both xls and xlsx files using java in spring mvc

In this method i used xssf class which is used to read xlsx file but we cant do it for xls file.for xls we need to have Hssf class .User can import any format there .My requirement,Is there any Class that can be used instead of xssf and hssf to read both kind of file. in my example i used xssf.

  @RequestMapping(value="/import",method = RequestMethod.POST)
     public ModelAndView imports(Model model, @RequestParam("excelFile") MultipartFile excelfile){
        try {
            List<DepartmentModel> lstUser = new ArrayList<>();
            int i = 0;

            XSSFWorkbook workbook = new XSSFWorkbook(excelfile.getInputStream());

            XSSFSheet worksheet = workbook.getSheetAt(0);
            while (i <= worksheet.getLastRowNum()) {

                DepartmentModel user = new DepartmentModel();

                XSSFRow row = worksheet.getRow(i++);
user.setHrName(row.getCell(0).getStringCellValue());
user.setDepartmentName(row.getCell(1).getStringCellValue());
user.setParentDepartment(row.getCell(2).getStringCellValue());


                lstUser.add(user);

            }

            departmentService.updateList(lstUser);

            model.addAttribute("lstUser", lstUser);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new ModelAndView("redirect:/listOfDepartment");
    }

Im having another method which i used Hssf to read xls file.But iam having only one import button user can upload any type of file xls,xlsx but for import button i can have one action eigther go to xssf or hssf method.So i like to know is there any possible way to have botth in single method.Or any other super class to having property of both Xssf and Hssf Class.

Answer

For supporting both HSSF as well as XSSF for reading and rewriting *.xls and *.xlsx, you will using WorkbookFactory for creating Workbook. This is able creating Workbook from InputStream of *.xls as well as of *.xlsx files.

 FileInputStream fileinputstream = new FileInputStream("pathToExcelFile.xls_or_.xlsx");
 Workbook workbook = WorkbookFactory.create(fileinputstream);

Then, as long as possible, you will working with the interfaces of Package org.apache.poi.ss.usermodel instead of the special HSSF or XSSF classes.

This is not always possible since apache poi is in development until now. But if not possible you can detect via instanceof what object (HSSF or XSSF) you really are working with.

And for writing you will using the appropriate methods dependent of the instanceof the Workbook.

  if (workbook instanceof XSSFWorkbook) {
   workbook.write(new FileOutputStream("pathToExcelFile.xlsx"));
  } else if (workbook instanceof HSSFWorkbook) {
   workbook.write(new FileOutputStream("pathToExcelFile.xls"));
  }
  workbook.close();

Up to apache poi 3.17 Workbook.write has closed the OutputStream. Now in apache poi 4.0.* versions it not more closes the OutputStream. So we need using

  FileOutputStream out = null;
  if (workbook instanceof XSSFWorkbook) out = new FileOutputStream("pathToExcelFile.xlsx");
  else if (workbook instanceof HSSFWorkbook) out = new FileOutputStream("pathToExcelFile.xls");
  if (out != null) {
   workbook.write(out);
   out.close();
  }
  workbook.close();

Leave a Reply

Your email address will not be published. Required fields are marked *