java 一對多導出excel


pom

 

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

 

import cn.afterturn.easypoi.excel.annotation.ExcelCollection;
import com.huitian.mine.domain.SurveyDetailComplete;
import lombok.Data;
import cn.afterturn.easypoi.excel.annotation.Excel;

import java.io.Serializable;
import java.util.List;

@Data
public class ExportTemplateExcelOne implements Serializable {

    private static final long serialVersionUID = 1L;

    @Excel(name = "序號",needMerge = true,width = 20)
    private String id;

    @Excel(name = "統一編號",needMerge = true,width = 20)
    private String mineNumber;

    @Excel(name = "歷史遺留礦山名稱",needMerge = true,width = 20)
    private String mineName;

    //@Excel(name = "遙感圖斑編號",needMerge = true)
    private String remoteSensingSpotNumber;

    @Excel(name = "主要開采礦種",needMerge = true,width = 20)
    private String mineType;

    @Excel(name = "開采方式",needMerge = true,width = 20)
    private String miningWay;

    @Excel(name = "廢棄原因",needMerge = true,width = 20)
    private String abandonReason;

    @Excel(name = "礦山地址",needMerge = true,width = 20)
    private String villageTown;

    @Excel(name = "經度",needMerge = true,width = 20)
    private String centerPositionLongitude;

    @Excel(name = "緯度",needMerge = true,width = 20)
    private String centerPositionLatitude;

    @ExcelCollection(name = "")
    private List<SurveyDetailComplete> surveyDetailCompleteList;

}
 
         
import cn.afterturn.easypoi.excel.annotation.Excel;
@Data
public class SurveyDetailComplete {
    /**系統生成ID*/
    @TableId(type = IdType.ID_WORKER_STR)
    private String id;
    /**礦山ID*/
    //@Excel(name = "礦山ID", width = 15)
    private String mineId;
    /**圖斑號*/
    @Excel(name = "圖斑號", width = 25)
    private String remoteSensingSpotNumber;
    /**修復方式及面積-自然修復*/
    @Excel(name = "修復方式及面積-自然修復", width = 15)
    private String naturalRepair;
    /**修復方式及面積-人工修復*/
    @Excel(name = "修復方式及面積-人工修復", width = 15)
    private String artificialRepairArea;
    /**修復方式及面積-投入資金(萬元)*/
    @Excel(name = "修復方式及面積-投入資金(萬元)", width = 15)
    private String investedCapital;
    /**修復土地資源-耕地*/
    @Excel(name = "修復土地資源-耕地", width = 15)
    private String arableLand;
    /**修復土地資源-種植園用地*/
    @Excel(name = "修復土地資源-種植園用地", width = 15)
    private String plantationLand;
    /**修復土地資源-林地*/
    @Excel(name = "修復土地資源-林地", width = 15)
    private String woodLand;
    /**修復土地資源-草地*/
    @Excel(name = "修復土地資源-草地", width = 15)
    private String grassLand;
    /**修復土地資源-建設用地*/
    @Excel(name = "修復土地資源-建設用地", width = 15)
    private String constructionLand;
    /**修復土地資源-其他用地*/
    @Excel(name = "修復土地資源-其他用地", width = 15)
    private String otherLand;
    /**修復土地資源-小計*/
    @Excel(name = "修復土地資源-小計", width = 15)
    private String landSubtotal;

}
@RequestMapping(value = "/complete", method = RequestMethod.POST)
    public Result exportXlsComplete(@RequestParam(name = "selectRegionCode", defaultValue = "") String selectRegionCode,
                                    @RequestParam(name = "selectRegionType", defaultValue = "") String selectRegionType, HttpServletResponse response) throws Exception {
        Map<String, Object> paramsMap = this.getRegionCodeAndFileName(selectRegionCode, selectRegionType);
        //查詢一對多數據
        List<ExportTemplateExcelOne> list = exportExcelMapper.exportExcelOne(String.valueOf(paramsMap.get("selectRegionCode")));       
        // 簡單模板導出方法
        cn.afterturn.easypoi.excel.entity.ExportParams params = new cn.afterturn.easypoi.excel.entity.ExportParams();
        params.setTitle("歷史遺留礦山調查基本情況表(已治理的)");//設置表頭
        params.setSheetName("已治理(含自然修復)");//設置sheet名
        Workbook workbook = ExcelExportUtil.exportExcel(params, ExportTemplateExcelOne.class, list);
        this.setExportExcelFormat(response, workbook, "歷史遺留礦山調查基本情況表(已治理的)");
        return new Result().ok();
    }
/**
* 響應數據
* @param response
* @param workbook
* @param fileName
* @throws Exception
*/
public void setExportExcelFormat(HttpServletResponse response, Workbook workbook, String fileName) throws Exception {
response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(), "ISO8859-1"));
response.setHeader("Pargam", "no-cache");
response.setHeader("Cache-Control", "no-cache");
ServletOutputStream outStream = null;
try {
outStream = response.getOutputStream();
workbook.write(outStream);
} finally {
outStream.flush();
outStream.close();
}
}


List<ExportTemplateExcelOne> exportExcelOne(@Param("regionCode") String regionCode);
<resultMap id="completeBean" type="com.xxxxx.mine.domain.vo.ExportTemplateExcelOne">
        <id column="id" property="id"></id>
        <result column="mine_number" property="mineNumber"></result>
        <result column="mine_name" property="mineName"></result>
        <result column="remote_sensing_spot_number" property="remoteSensingSpotNumber"></result>
        <result column="mine_type" property="mineType"></result>
        <result column="mining_way" property="miningWay"></result>
        <result column="abandon_reason" property="abandonReason"></result>
        <result column="village_town" property="villageTown"></result>
        <result column="center_position_longitude" property="centerPositionLongitude"></result>
        <result column="center_position_latitude" property="centerPositionLatitude"></result>

        <collection property="surveyDetailCompleteList" ofType="com.xxxx.mine.domain.SurveyDetailComplete">
            <id column="cid" property="id"></id>
            <result column="mine_id" property="mineId"></result>
            <result column="natural_repair" property="naturalRepair"></result>
            <result column="artificial_repair_area" property="artificialRepairArea"></result>
            <result column="invested_capital" property="investedCapital"></result>
            <result column="arable_land" property="arableLand"></result>
            <result column="plantation_land" property="plantationLand"></result>
            <result column="wood_land" property="woodLand"></result>
            <result column="grass_land" property="grassLand"></result>
            <result column="construction_land" property="constructionLand"></result>
            <result column="other_land" property="otherLand"></result>
            <result column="land_subtotal" property="landSubtotal"></result>
            <result column="spotNumber" property="remoteSensingSpotNumber"></result>
        </collection>
    </resultMap>
    <select id="exportExcelOne" parameterType="java.lang.String"
            resultMap="completeBean">
        select t.* ,tc.id cid, tc.*,tc.remote_sensing_spot_number spotNumber
        FROM t_o_mines_data t LEFT JOIN t_o_survey_detail_complete tc ON t.id=tc.mine_id
        WHERE t.sys_org_code
        <if test="regionCode.length()==2">
            like CONCAT(#{regionCode},'%')
        </if>
        <if test="regionCode.length()==4">
            like CONCAT(#{regionCode},'%')
        </if>
        <if test="regionCode.length()==6">
            = #{regionCode}
        </if>
        and del_flag='0'
        order by t.id asc;

    </select>

 

 

vue 前端

 fileSaver.saveAs(response.data, '附表1-雲南省歷史遺留礦山調查基本情況表(已治理的).xls', true)

導出效果

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM