java实现Excel文件上传下载(模板)-hutool
java实现Excel文件上传下载(模板)-hutool
·
引入依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.1.0</version>
</dependency>
上传
文件上传入参为
@RequestPart MultipartFile file
具体实现:
// 1.获取上传文件输入流
try (InputStream inputStream = multipartFile.getInputStream()) {
// 调用用 hutool 方法读取数据 默认调用第一个sheet
ExcelReader excelReader = ExcelUtil.getReader(inputStream);
//忽略第一行头(第一行是中文的情况),直接读取表的内容
List<List<Object>> list = excelReader.read(1);
List<WorkPosition> workPositionList=new ArrayList<>();
if(CollectionUtil.isEmpty(list)){
return WebResponse.error(WebResponse.WARN_ERROE,"上传文件数据为空,请重新上传");
} else {
for (List<Object> row : list) {
、、、、逻辑处理、、、、
}
//批量插入数据
Integer count=workPositionMapper.insertList(workPositionList);
if(count>0){
return WebResponse.success("批量导入成功");
}else{
return WebResponse.error(WebResponse.CODE_ERROE,"数据导入失败");
}
}
} catch (Exception e) {
throw new RuntimeException("批量导入货位出错:" + e.getMessage());
}
}
下载
下载模板,(下载文件在项目中)
public void getWorkPositionExcelCase(HttpServletResponse response) throws IOException {
//获取文件所在位置
String path="file/";
String fileName="导入货位模板.xls";
// 清空输出流
String resultFileName = fileName;
resultFileName = URLEncoder.encode(resultFileName,"UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + resultFileName);// 设定输出文件头
response.setContentType("application/msexcel");// 定义输出类型
//输入流:文件路径
DataInputStream in = new DataInputStream(new ClassPathResource(path+fileName).getInputStream());
//输出流
OutputStream out = response.getOutputStream();
//输出文件
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
out.close();
in.close();
}
下载文件,(hutool,代码生成文件形式)
public void getAllHandleExcel(GetWorkAoCodeVo getWorkAoCodeVo, HttpServletResponse response) {
ExcelWriter writer = ExcelUtil.getWriter();
List<WorkAoHandleVo> workAoHandleVos =workAoHandleMapper.getAllHandle(getWorkAoCodeVo);
List<Map<String, Object>> rows = workAoHandleVos.stream().map(item -> {
Map<String, Object> maps = new HashMap<>();
String format = DateUtil.format(item.getCreateTime(), DatePattern.NORM_DATETIME_PATTERN);
maps.put("createTime", format);
maps.put("stationName", item.getStationName());
maps.put("shelvesName", item.getShelvesName());
maps.put("positionCode", item.getPositionCode());
maps.put("aoNo", item.getAoNo());
String handleName="";
if(WorkConstant.LOW_FRAME.equals(item.getHandle())){
handleName="下架";
}else if(WorkConstant.UP_FRAME.equals(item.getHandle())){
handleName="上架";
}else{
handleName="补货";
}
maps.put("handle", handleName);
return maps;
}).collect(Collectors.toList());
//设置整体标题
//标题占用表格长度
int columns = 6;
writer.merge(columns - 1, "AO上下架记录");
//各个字段标题
writer.addHeaderAlias("createTime", "时间");
writer.addHeaderAlias("stationName", "工位缓存区");
writer.addHeaderAlias("shelvesName", "所属货架");
writer.addHeaderAlias("positionCode", "货位码");
writer.addHeaderAlias("aoNo", "AO|架次号");
writer.addHeaderAlias("handle", "操作内容");
//设置各个
writer.setColumnWidth(0, 30);
writer.setColumnWidth(1, 30);
writer.setColumnWidth(2, 30);
writer.setColumnWidth(3, 30);
writer.setColumnWidth(4, 30);
writer.setColumnWidth(5, 30);
writer.write(rows, true);
response.setContentType("application/vnd.ms-excel;charset=utf-8");
try {
response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode("AO上下架记录-" + DateUtil.today() + ".csv", "utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
ServletOutputStream out = null;
try {
out = response.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
writer.flush(out, true);
writer.close();
IoUtil.close(out);
}
更多推荐
已为社区贡献5条内容
所有评论(0)