java-导⼊导出下载模板等功能
⼀:先写 导⼊
1:java是基于包:poi 组件实现的,pom组件如下:
<!-- Excel导⼊功能实现 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
2:但是如果完全基于poi去做,需要封装很多代码,⽐较复杂⼀下,使⽤阿⾥的组件  easyexcel
该组件是基于  上⾯的poi 再封装⼀次完成。所以如果使⽤ easyexcel还是必须引⽤poi的包
pom⾥⾯的⽂件如下:但是这⾥需要注意  easyexcel 跟 poi的版本⼀定要对应起来,如果不对应就会报错,错误信息是:提⽰某⼀个类不存在
java.lang.NoClassDefFoundError: org/apache/poi/poifs/filesystem/FileMagic
<!-- alibaba的Excel导⼊功能实现 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.8</version>
</dependency>
3:控制器 Controller ⾥⾯的API代码:
需要注意:
EasyExcel 就是阿⾥组件⾥⾯的类;
HealthTargetImport 是⾃定义的模板样式;如下,也贴出来
svg图片长什么样
MultipartFile  是 springframework⾥⾯的封装类
count = this.service.importExcel(healthTargetImports)  这句话就是 ⾃⼰写的批量添加⽅法,
因为上⾯已经拿到了excel的数据。存放在healthTargetImports 中,所以后⾯怎么玩都⾏,
@PostMapping("/importExcel")
@ApiOperation(value = "导⼊功能", notes = "导⼊功能")
public ApiResponse<Integer>  importExcel(@RequestParam("file")  MultipartFile file) throws IOException {        InputStream inputStream = InputStream();
List<HealthTargetImport> healthTargetImports = ad(inputStream)
.head(HealthTargetImport.class)
// 设置sheet,默认读取第⼀个
.sheet()
// 设置标题所在⾏数
.headRowNumber(1)
.doReadSync();
// 获取excel的数据后添加到数据库
int count = 0;
if (healthTargetImports.size() > 0) {
count = this.service.importExcel(healthTargetImports);
}
return ApiResponse.success(count);
}
package com.health.l;
import l.annotation.ExcelProperty;
import lombok.Data;
/**
*  对接单位检项细项指标导⼊模板
*/
@Data
public class HealthTargetImport {
hbuilder个人简介网页模板
/**
* 单位编码
*/
react耐克拆解@ExcelProperty(value = "单位编码",index = 0)
private String healthCode;
/
**
* 项⽬编码
*/
@ExcelProperty(value ="项⽬编码",index = 1)
private String healthProjectCode;
/**
* 项⽬名称
*/
@ExcelProperty(value = "项⽬名称",index = 2)
private String healthProjectName;
/**
java下载过程* 指标编码
*/
@ExcelProperty(value = "指标编码",index = 3)
private String healthTargetCode;
/**
* 指标名称
*/
@ExcelProperty(value = "指标名称",index = 4)
private String healthTargetName;
/**
* 数据类型
* 数据类型
*/
@ExcelProperty(value = "数据类型",index = 5)
private String dataType;
/**
* 备注
*/
@ExcelProperty(value = "备注",index = 6)
private String remarks;
/**
* 对接单位项⽬内容
*/
@ExcelProperty(value = "对接单位项⽬内容",index = 7)
private String healthProjectContent;
/**
* 对接单位项⽬说明
*/
@ExcelProperty(value = "对接单位项⽬说明",index = 8)
private String healthProjectExplain;靠谱的it培训机构
}
4:导⼊已完成。当然这⾥还可以去做对excel的每⾏ 每列 做⼀些数据约束校验。这个 可以去对阿⾥的⼀个类去重写⽅法。5:测试环节:
通常⽤swagger测试,很简单。
因为api的参数是MultipartFile类型,所以⼊参直接是现在⽂件,如图:选择模板⼀致的excel直接上传⽂件即可。
也可以⽤postman上传⽂件:
详细操作地址如下:
⼆:下载模板
1:默认上⾯的导⼊功能已完成,所以 pom⽂件不再描述,
2:先写⼀个⼯具类ExcelUtil
public class ExcelUtil {
/
**
* 导出
* @param response
* @param data
* @param fileName
* @param sheetName
* @param clazz
* @throws Exception
*/
public static void writeExcel(HttpServletResponse response, List<? extends Object> data, String fileName, String sheetName, Class clazz) throws Exception {        //表头样式
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
//设置表头居中对齐
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//内容样式
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
//设置内容靠左对齐
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
EasyExcel.write(getOutputStream(fileName, response), clazz).excelType(ExcelTypeEnum.XLSX).sheet(sheetName).registerWriteHandler(horizontalCellStyle    }
private static OutputStream getOutputStream(String fileName, HttpServletResponse response) throws Exception {
fileName = de(fileName, "UTF-8");
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
response.addHeader("Access-Control-Expose-Headers", "Content-disposition");
OutputStream();
}
3:在控制器中添加⼀个下载模板⽅法:
/*下载模板*/
@GetMapping("/downloadTemplate")
@ApiOperation(value = "下载模板", notes = "下载模板", produces = "application/octet-stream")
public void downloadTemplate(HttpServletResponse response){
String fileName = "对接单位细项导⼊模板";
String sheetName="对接单位细项导⼊模板";
List<HealthTargetImport> teacherExcelList = new ArrayList<>();
HealthTargetImport teacherExcel = new HealthTargetImport();
teacherExcel.setHealthCode("healthCode");
//        teacherExcel.setIntro("清华毕业,⾼材⽣");
//        teacherExcel.setCareer("资深讲师");
//        teacherExcel.setSort(1);
//        teacherExcel.setLevel(1);
teacherExcelList.add(teacherExcel);
try {
//TeacherExcel.class对应你的模板类
xml一般应用于//teacherExcelList模板的例⼦
//也可以使⽤这种⽅式导出你查询出数据excel⽂件
ExcelUtil.writeExcel(response,teacherExcelList,fileName,sheetName,HealthTargetImport.class);
} catch (Exception e) {
System.out.Cause());
}
}
4:这⾥如果⽤postmat去测试 或者⽤swagger测试都会返回乱码,需要注意:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。