如何在Spring MVC中使用Java导入xls和xlsx文件 [英] How to import both xls and xlsx files using java in spring mvc

查看:238
本文介绍了如何在Spring MVC中使用Java导入xls和xlsx文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种方法中,我使用了xssf类,该类​​用于读取xlsx文件,但我们无法对xls文件进行处理.对于xls,我们需要Hssf类.用户可以在那里导入任何格式.我的要求是,是否有任何可以用来代替xssf和hssf来读取两种文件.在我的示例中,我使用了xssf.

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");
    }

我有另一种方法,我使用Hssf读取xls文件.但是只有一个导入按钮用户的iam可以上载任何类型的文件xls,xlsx,但是对于导入按钮,我可以执行一个操作,否则请转到xssf或hssf方法.因此,我想知道是否有可能在单个方法中拥有底部,或者具有Xssf和Hssf类的属性的任何其他超类.

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.

推荐答案

为同时支持HSSFXSSF来读取和重写*.xls*.xlsx,您将使用

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);

然后,您将尽可能地使用打包org.apache.poi.ss.usermodel 而不是特殊的HSSFXSSF类.

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.

由于apache poi到目前为止仍在开发中,因此这并不总是可能的.但是,如果不可能,您可以通过instanceof检测到您真正在使用哪个对象(HSSFXSSF).

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.

为了进行编写,您将使用依赖于instanceofWorkbook的适当方法.

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();

最多apache poi 3.17 Workbook.write已关闭OutputStream.现在,在apache poi 4.0.*版本中,它不再关闭OutputStream.所以我们需要使用

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();

这篇关于如何在Spring MVC中使用Java导入xls和xlsx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆