当前位置:网站首页>Easypoi之excel多sheet导入
Easypoi之excel多sheet导入
2022-07-17 05:08:00 【qq_42042158】
多sheet导入跟简单导入差不多,只是controller层略微有点差别
其他代码点击Easypoi之excel简单导入
controller层代码
/** * 多sheet导入Excel数据 */
@RequestMapping(value = "multImports",method = RequestMethod.POST)
@ResponseBody
public String multImports(MultipartFile file) throws IOException {
if (null==file){
return GetResult.getResultObj("文件上传失败");
}
//根据file得到Workbook,主要是要根据这个对象获取,传过来的excel有几个sheet页
Workbook hssfWorkbook = getWorkBook(file);
//获取sheet数量
int sheetNum = hssfWorkbook.getNumberOfSheets();
ImportParams params = new ImportParams();
//表头在第几行
params.setTitleRows(5);
try {
List<Applys> list = new ArrayList<>();
for (int sheetIndex = 0; sheetIndex < sheetNum; sheetIndex++) {
//第几个sheet页
params.setStartSheetIndex(sheetIndex);
List<Applys> result = ExcelImportUtil.importExcel(file.getInputStream(), Applys.class, params);
list.addAll(result);
}
for (int i = 0; i < list.size(); i++) {
if (null==list.get(i).getRealName()){
list.remove(i);
i--;
}
}
if (list.size()==0){
return GetResult.getResultObj("数据读取失败,检查Excel数据格式");
}
if (ApplyService.saveBatch(list)){
return GetResult.getResultObj("ok");
}
} catch (Exception e) {
e.printStackTrace();
}
return GetResult.getResultObj("文件读取失败");
}
/** * 获取Excel中的sheet * @param file * @return * @throws IOException */
public static Workbook getWorkBook(MultipartFile file) throws IOException {
//这样写excel能兼容03和07
InputStream is = file.getInputStream();
Workbook hssfWorkbook = null;
try {
hssfWorkbook = new HSSFWorkbook(is);
} catch (Exception ex) {
is =file.getInputStream();
hssfWorkbook = new XSSFWorkbook(is);
}
return hssfWorkbook;
}
边栏推荐
- 交换机用户模式、特权模式、全局模式、端口模式
- H5页面使用js生成二维码
- Es6 真实案例解构(多维数组对象)全新案例:
- 学习C语言的第6天
- (elaborate) ES6 remaining parameters, ES6 built-in objects, template string content (detailed example Dictionary) and practical cases of flexible use of the project
- 【C语言—零基础第八课】循环结构与break continue
- 哨兵二号轨道数据下载
- UML(用例图,类图,对象图,包图)
- STL容器——map的基本操作
- es6新增-数组部分
猜你喜欢

Cesium bind mouse events and remove mouse events

Applet cloud development upload pictures to cloud storage

uni-app 条件编译#ifdef #endif 兼容多个终端

uniapp 使用uview实现折叠面板

Uniapp uses uview to realize folding panel

基于RTX30显卡的ArcGIS Pro2.8深度学习环境配置

父组件加scoped有时也会影响子组件

es6新增-数组部分

循环赛制日程表问题

使用Echars实现水滴状、环形图、分割图、堆叠、组织架构图、地图轮廓等图表
随机推荐
【C语言—零基础第九课】函数中的爱恨情仇
【C语言—零基础第七课】顺序结构与选择结构
2020-11-10
学习C语言的第四天
实习项目2-主页配置-我的数据模块
Cesium bind mouse events and remove mouse events
Wechat applet status bar
Pat class B 1017: a divided by B
基于RTX30显卡的ArcGIS Pro2.8深度学习环境配置
Excel导入长数据末尾变000
Nacos配置管理
Markdown notes and related shortcut keys of typora
Interface parameters return encapsulated class result
Pat class B 1002: write this number
B域,M域,O域具体是指什么
微信小程序wx.setClipboardData复制文本
小程序云开发表单提交并在页面中获取数据
【C语言—零基础第十三课】字符串的奥秘
手把手教你复现Log4j2核弹级漏洞
computed和watch的区别