[alibaba/easyexcel]下载文件,文件后缀没有了

2024-05-21 42 views
3

触发场景描述 下载文件,没有文件后缀 系统:mac OS 10.15.6 (19G73)

User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36

触发Bug的代码

//        String fileName = "房间导入模板" + ".xlsx";
        response.reset();
//        String contentType = Mimetypes.getInstance().getMimetype(fileName);
        response.addHeader(HttpHeaders.CONTENT_TYPE,"application/vnd.ms-excel");
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
//

//        if ( ServletUtils.getRequest().getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0)    {
//            fileName = new String(fileName.getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1) ;//firefox浏览器
//        } else{
//            fileName = URLEncoder.encode(fileName, "UTF-8");
//        }
        String fileName = URLEncoder.encode("测试", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
//        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" +fileName+"\"");

        EasyExcel.write(response.getOutputStream(), ExcelHouseTemplateBO.class).registerWriteHandler(new HouseSheetWriteHandler(communityDataDictionaryService)).sheet("模板").doWrite(result);

提示的异常或者没有达到的效果 image

回答

2

@fushun1990 麻烦把你的HouseSheetWriteHandler的代码贴一下

4

`package com.hsh.service.handler;

import com.alibaba.excel.write.handler.SheetWriteHandler; import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.hsh.dao.po.CommunityDataDictionary; import com.hsh.service.CommunityDataDictionaryService; import com.hsh.service.constant.StaticColumnConstant; import org.apache.poi.ss.usermodel.DataValidation; import org.apache.poi.ss.usermodel.DataValidationConstraint; import org.apache.poi.ss.usermodel.DataValidationHelper; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.util.CellRangeAddressList; import org.apache.poi.xssf.usermodel.XSSFDataValidation;

import java.util.*;

public class HouseSheetWriteHandler implements SheetWriteHandler {

private CommunityDataDictionaryService communityDataDictionaryService;

public HouseSheetWriteHandler(CommunityDataDictionaryService communityDataDictionaryService) {
    this.communityDataDictionaryService = communityDataDictionaryService;
}

@Override
public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

}

@Override
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

    List<CommunityDataDictionary> type = communityDataDictionaryService.list(new QueryWrapper<CommunityDataDictionary>()
            .isNull(StaticColumnConstant.DELETED_TIME).in("type", Arrays.asList(1, 2)));
    if (type.isEmpty()) {
        return;
    }
    Map<String, List<String>> typeMap = new HashMap<>();
    for (CommunityDataDictionary communityDataDictionary : type) {
        typeMap.putIfAbsent(communityDataDictionary.getType(), new ArrayList<>());
        typeMap.get(communityDataDictionary.getType()).add(communityDataDictionary.getDataName());
    }

    //性别
    Map<Integer, String[]> mapDropDown = new HashMap<>();
    if (typeMap.get("1") != null) {
        mapDropDown.put(11, typeMap.get("1").toArray(new String[]{}));
    }
    if (typeMap.get("2") != null) {
        mapDropDown.put(12, typeMap.get("2").toArray(new String[]{}));
    }
    Sheet sheet = writeSheetHolder.getSheet();

    ///开始设置下拉框
    DataValidationHelper helper = sheet.getDataValidationHelper();
    mapDropDown.forEach((key, val) -> {
        //起始行、终止行、起始列、终止列
        CellRangeAddressList addressList = new CellRangeAddressList(1, 1000, key, key);
        //设置下拉框数据
        DataValidationConstraint constraint = helper.createExplicitListConstraint(val);
        DataValidation validation = helper.createValidation(constraint, addressList);
        // 阻止输入非下拉选项的值
        validation.setErrorStyle(DataValidation.ErrorStyle.STOP);
        //处理Excel兼容性问题
        if (validation instanceof XSSFDataValidation) {
            validation.setSuppressDropDownArrow(true);
            validation.setShowErrorBox(true);
        } else {
            validation.setSuppressDropDownArrow(false);
        }
        sheet.addValidationData(validation);
    });
}

} `

0

@Jiaxiayuan

3

下下来就可以了