[英]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. 在这种方法中,我使用了xssf类来读取xlsx文件,但我们无法为xls文件做。对于xls,我们需要Hssf类。用户可以在那里导入任何格式。我的要求是,是否有可以使用的任何类而不是xssf和hssf来读取两种文件。 in my example i used xssf.
在我的示例中,我使用了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. 我有另一种方法,我使用Hssf读取xls文件。但是只有一个导入按钮用户的iam可以上传任何类型的文件xls,xlsx,但是对于导入按钮,我可以有一个动作,例如转到xssf或hssf方法。所以我喜欢要知道有没有可能在单个方法中拥有底部,或者拥有Xssf和Hssf类的属性的任何其他超类。
For supporting both HSSF
as well as XSSF
for reading and rewriting *.xls
and *.xlsx
, you will using WorkbookFactory for creating Workbook . 为了同时支持
HSSF
和XSSF
来读取和重写*.xls
和*.xlsx
,您将使用WorkbookFactory来创建Workbook 。 This is able creating Workbook
from InputStream
of *.xls
as well as of *.xlsx
files. 这能够从
*.xls
以及*.xlsx
文件的InputStream
创建Workbook
。
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. 然后,您将尽可能地使用Package org.apache.poi.ss.usermodel的接口,而不是特殊的
HSSF
或XSSF
类。
This is not always possible since apache poi
is in development until now. 由于
apache poi
一直在开发中,因此这并不总是可能的。 But if not possible you can detect via instanceof
what object ( HSSF
or XSSF
) you really are working with. 但是,如果不可能的话,您可以通过
instanceof
检测到您真正在使用哪个对象( HSSF
或XSSF
)。
And for writing you will using the appropriate methods dependent of the instanceof
the Workbook
. 在编写时,您将使用依赖于
Workbook
instanceof
的适当方法。
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
. 直到
apache poi 3.17
Workbook.write
关闭了OutputStream
。 Now in apache poi 4.0.*
versions it not more closes the OutputStream
. 现在在
apache poi 4.0.*
版本中,它不再关闭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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.