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数据导出


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

在这里插入图片描述
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数据导入入库

导入模板为
在这里插入图片描述

代码

@Service
@Slf4j
public class GpService {
    
    

	public List<BasicsInfoVo> importExcel(MultipartFile file){
    
    
		try {
    
    
			ImportParams params = new ImportParams();
			params.setTitleRows(1); //设置标题忽略的行数  setTitleRows(1)就是标题忽略1行  setTitleRows(2)标题忽略两行
			params.setHeadRows(1); //设置表头忽略的行数   setHeadRows(1)就是标题忽略1行   setHeadRows(2)标题忽略两行

			//multipartFile是上传文件
			List<TransactionDetail> list = ExcelImportUtil.importExcel(file.getInputStream(), TransactionDetail.class, params);
			System.out.println(list);
		}catch(Exception e){
    
    
			e.printStackTrace();
		}

		return null;
	}
}

InputSchoolDataPo 实体

@Data
@ExcelTarget("transactionDetail")
public class TransactionDetail {
    
    
	
	@Excel(name = "时间")
	private Date jyTime;

	@Excel(name = "价格")
	private String jyPrice;

	@Excel(name = "成交")
	private String chengjiao;

	@Excel(name = "笔数")
	private String biNum;
}

猜你喜欢

转载自blog.csdn.net/qq_38092788/article/details/130258364