快捷搜索: 王者荣耀 脱发

java中Excel导入导出处理

导出采用EasyPoiAPI

http://doc.wupaas.com/docs/easypoi

加入maven依赖

<dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-base</artifactId>
        <version>4.1.0</version>
    </dependency>
    <dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-web</artifactId>
        <version>4.1.0</version>
    </dependency>
    <dependency>
        <groupId>cn.afterturn</groupId>
        <artifactId>easypoi-annotation</artifactId>
        <version>4.1.0</version>
    </dependency>

1.Exce数据导出


excel数据导出

创建模板,及配置模板渲染信息

渲染字段如果渲染集合必须集合前面 ” { {$fe: list ” 其中list 是再代码中put的key值。我目前的模板key值是inputSchoolDataPos, 然后就 t.对象 就能把相对应的对象给渲染出来。最后一个对象一定要写 " }} "

上代码

 TemplateExportParams params = new TemplateExportParams("templates/标准模版5.xlsx", true);
            params.setStyle(ExcelStyleType.BORDER.getClazz());
            Map<String, Object> map = new HashMap<String, Object>();
            List<InputSchoolDataPo> inputSchoolDataPos = new ArrayList<>();
           //inputSchoolDataPos就是 {
          
   {$fe: list中的list
            map.put("inputSchoolDataPos", inputSchoolDataPos); 
           Workbook workbook = ExcelExportUtil.exportExcel(params, map);
			 
			 
			//1.直接文件输入到固定的文件地址下面
            File savefile = new File("D:/test");
            if (!savefile.exists()) {
          
   
                savefile.mkdirs();
            }
            FileOutputStream fos = new FileOutputStream("D:/test/测试文件.xlsx");
            workbook.write(fos);
            fos.close();
			
			
			//2.流输入直接下载需要 传入HttpServletResponse  response
			 response.setHeader("content-Type", "application/vnd.ms-excel");
	         try {
          
   
	            response.setHeader("content-Disposition",
	             "attachment;filename=" + URLEncoder.encode("测试文件.xlsx", "utf-8"));
	            workbook.write(response.getOutputStream());
	          } catch (UnsupportedEncodingException e) {
          
   
	            e.printStackTrace();
	          } catch (IOException e) {
          
   
	            e.printStackTrace();
	          }

2.Excel数据导入入库

导入模板为 代码

try {
          
   
         ImportParams params = new ImportParams();
         params.setHeadRows(1); //头行忽略的行数
         //multipartFile是MultipartFile multipartFile
         inputSchoolDataPos = ExcelImportUtil.importExcel(multipartFile.getInputStream(),
         }catch(Exception e){
          
   }

InputSchoolDataPo 实体

@Data
@ExcelTarget("inputSchoolDataPo")
public class InputSchoolDataPo {
          
   

    @Excel(name = "专业大类(必选)")
    private String specialityBigType;

    @Excel(name = "专业名称(必填)")
    private String specialityName;

    @Excel(name = "专业代码(必填)")
    private String specialityCode;

    @Excel(name = "专业开始招生年份(必填)")
    private String specialityStartYear;

    @Excel(name = "专业名称校验")
    private String specialityNameCheck;

    @Excel(name = "代码校验")
    private String codeCheck;

    @Excel(name = "年份校验")
    private String yearCheck;
}
经验分享 程序员 微信小程序 职场和发展