springboot use easypoi achieve export Excel spreadsheet

 Import dependence

<!--excel操作-->
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-spring-boot-starter</artifactId>
    <version>3.3.0</version>
</dependency> 

 

Vo defined entity class

@Data 
@EqualsAndHashCode (= callSuper to false) 
public class ProductExportVo the implements the Serializable { 
    Private Long Final static serialVersionUID = 1L; 
    / ** 
     * Product Code 
     * / 
    @Excel (name = "Item No.", orderNum = "0", width = 15 ) 
    Private String productNo; 

    / ** 
     * product name 
     * / 
    @Excel (name = "product name", orderNum = ". 1", width = 25) 
    Private String productName; 


    / ** 
     * product type 
     * / 
    @Excel (name = "product type", orderNum = "2", width = 15) 
    Private String productType; 

    / ** 
     * nominal value 
     * / 
    @Excel (name = "literals",orderNum = "3", width = 15) 
    Private Integer productDeno; 

    / ** 
     * provincial sales area 0 1 2 National City field 
     * / 
    @Excel (name = "sales area", orderNum = ". 4", width = 15) 
    Private String productSalesArea; 

    / ** 
     * selling price 
     * / 
    @Excel (name = "sale price", orderNum = ". 5", width = 15) 
    Private the BigDecimal productPrice; 

    / ** 
     * status 0 Added the shelf 1 
     * / 
    @Excel (name = "status", orderNum = ". 6", width = 15) 
    Private String productStatus; 

    / ** 
     * creation time 
     * / 
    @Excel (name = "creation time", orderNum = "7", width = 25, exportFormat = "yyyy-MM-dd HH:mm:ss")
    productCreateTime String Private; 

    / ** 
     * Modified  
     * / 
    @Excel (name =" modified ", orderNum =" 8 ",width = 25, exportFormat = "yyyy-MM-dd HH:mm:ss")
    Private String productModifyTime; 
}

  

Control Layer

@PostMapping("products/exportAsExcel")
public void exportAsExcel(HttpServletResponse response,ProductSearchDto productSearchDto) {
    final String name = SecurityContextHolder.getContext().getAuthentication().getName();
    log.info("用户[{}, {}]查导出商品表", name, request.getRemoteAddr());
    if (productSearchDto.getProductDenoRange() != null && productSearchDto.getProductDenoRange().length < 2) {
        productSearchDto.setProductDenoRange(null);
    }
    if (productSearchDto.getProductPriceRange() != null && productSearchDto.getProductPriceRange().length < 2) {
        productSearchDto.setProductPriceRange(null);
    }
     List<ProductExportVo> productList = productService.queryProduct(productSearchDto);
    {the try
        ExcelUtils.exportExcel (productList, "product information," and "product information", ProductExportVo.class, "product information", Response); 
    } the catch (IOException E) { 
        e.printStackTrace (); 
    } 

}

  

Front-end Vue

downLoad(parmas) {
      var temp = document.createElement("form");
      temp.action = `/api/products/exportAsExcel?${qs.stringify(parmas)}`;
      temp.method = "post";
      temp.style.display = "none";
      document.body.appendChild(temp);
      temp.submit();
    },

  

Guess you like

Origin www.cnblogs.com/song1024/p/12501751.html