zoukankan      html  css  js  c++  java
  • Excle导出模板

    easyExcel 导出

    需要引入的maven

         <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>easyexcel</artifactId>
                <version>2.1.6</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>3.17</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml-schemas</artifactId>
                <version>3.17</version>
            </dependency>
    
            <dependency>
                <groupId>org.apche.poi</groupId>
                <artifactId>poi</artifactId>
                <version>3.17</version>
            </dependency>
    

      

    后台代码

    /**
    	 /**
    	 * 导出excel
    	 * @param formInfoEntityList
    	 * @param response
    	 */
    	@PostMapping("/exportTargetDataResult")
    	@ApiOperationSupport(order = 7)
    	@ApiOperation(value = "导出", notes = "")
    	public void exportTargetDataResult(@RequestBody List<ContractFormInfoEntity> formInfoEntityList, HttpServletResponse response) {
    
    		if(CollectionUtil.isNotEmpty(formInfoEntityList)){
    			 /* 导出文件名称 */
    			String  fileName = "合同归档信息导出";
    			WriteSheet sheet1 = new WriteSheet();
    			/* 导出的sheet的名称 */
    			sheet1.setSheetName("合同归档信息导出");
    			sheet1.setSheetNo(0);
    			/* 需要存入的数据 */
    			List<List<Object>> data = new ArrayList<>();
    			/* formInfoEntityList 表示要写入的数据 因为是前台显示列表 由前台进行传值,后期可以根据自己的需求进行改变 */
    			for(ContractFormInfoEntity contractFormInfoEntity:formInfoEntityList){
    				/* 属性 cloumns 表示一行,cloumns包含的数据是一行的数据
    				  要将一行的每个值 作为list的一个属性存进到list里 ,数据要和展示的excel表头一致*/
    				List<Object> cloumns = new ArrayList<Object>();
                     /* 合同名称 */
    				cloumns.add(contractFormInfoEntity.getContractName());
    				/* 币种 */
    				cloumns.add(contractFormInfoEntity.getCurrencyCategory());
    				/* 合同金额 */
    				cloumns.add(contractFormInfoEntity.getContractAmount());
    				/* 创建人 */
    				cloumns.add(contractFormInfoEntity.getCreateUser());
    				/* 创建时间 */
    				cloumns.add(contractFormInfoEntity.getCreateTime());
    				/* 创建部门标识 */
    				cloumns.add(contractFormInfoEntity.getCreateDept());
    				/* 合同一级分类 */
    				cloumns.add(contractFormInfoEntity.getContractBigCategory());
    				/* 合同二级分类 */
    				cloumns.add(contractFormInfoEntity.getContractSmallCategory());
    				data.add(cloumns);
    			}
    			/* 表头名称,excel的表头 一个list对象为一行里的一个表头名称 */
    			List<List<String>> headList = new ArrayList<List<String>>();
    			/* 此处表头为一行要显示的所有表头,要和数据的顺序对应上  需要转换为list */
    			List<String> head = Arrays.asList("合同名称", "币种", "合同金额","创建人", "创建时间", "创建部门标识","合同一级分类","合同二级分类");
    			/* 为了生成一个独立的list对象,所进行的初始化 */
    			List<String>  head2 =null;
    			for( String head1:head){
    				  head2 = new ArrayList<>();
    				/* 将表头的数据赋值进入list对象 */
    				  head2.add(head1);
    				  /* 将数据赋值进入最终要输出的表头 */
    				headList.add(head2);
    			  }
    
    			try {
    				 response.setContentType("application/vnd.ms-excel");
    				 response.setCharacterEncoding(Charsets.UTF_8.name());
    				 fileName = URLEncoder.encode(fileName, Charsets.UTF_8.name());
    				 response.setHeader("Content-disposition", "att	achment;filename=" + fileName + ".xlsx");
    				 EasyExcel.write(response.getOutputStream()).head(headList).sheet().doWrite(data);
    			 }catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    
    	}
    

      

    前台代码

    配置文件:axios

     axios.interceptors.response.use(res => {
      .....
      const headers = res.headers;
      if (headers['content-type'] === 'application/octet-stream;charset=utf-8') {
        return res
      }
      return res;
      ......
    }, error => {
      NProgress.done();
      return Promise.reject(new Error(error));
    });
    

      

    js 文件

    //  导出excel
    export const getExportTarget = (params) => {
      return request({
        url: '/api/blade-contract/archive/exportTargetDataResult',
        method: 'post',
        data: params,
        responseType: "blob"
      })
    }
    

      

    vue 文件

    // 导出
          exportData(){
            getExportTarget(this.tableData).then((res) => {
              // 将文件流转成blob形式
              const blob = new Blob([res.data], {type: 'application/vnd.ms-excel'})
              let filename = '归档文件导出.xls'
              // 创建一个超链接,将文件流赋进去,然后实现这个超链接的单击事件
              const elink = document.createElement('a')
              elink.download = filename
              elink.style.display = 'none'
              elink.href = URL.createObjectURL(blob)
              document.body.appendChild(elink)
              elink.click()
              URL.revokeObjectURL(elink.href) // 释放URL 对象
              document.body.removeChild(elink)
    
            })
          }
    

      

  • 相关阅读:
    LeetCode对撞指针汇总
    167. Two Sum II
    215. Kth Largest Element in an Array
    2018Action Recognition from Skeleton Data via Analogical Generalization over Qualitative Representations
    题解 Educational Codeforces Round 84 (Rated for Div. 2) (CF1327)
    题解 JZPKIL
    题解 八省联考2018 / 九省联考2018
    题解 六省联考2017
    题解 Codeforces Round #621 (Div. 1 + Div. 2) (CF1307)
    题解Codeforces Round #620 (Div. 2)
  • 原文地址:https://www.cnblogs.com/blogxu/p/easyExcel.html
Copyright © 2011-2022 走看看