actual combat 46 —— 若依导出接口改造

导出

public void importTemplateExcel(HttpServletResponse response, String sheetName, String title)
{
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setCharacterEncoding("utf-8");
    this.init(null, sheetName, title, Type.IMPORT);
    exportExcel(response);
}
public void init(List<T> list, String sheetName, String title, Type type)
{
    if (list == null)
    {
        list = new ArrayList<T>();
    }
    this.list = list;
    this.sheetName = sheetName;
    this.type = type;
    this.title = title;
    // 构造属性
    createExcelField();
    // 创建工作簿wb,工作簿wb创建sheet,给工作簿的第一个sheet设置名称,构建样式
    createWorkbook();
    createTitle();
    createSubHead();
}
/**
 * 得到所有定义字段
 */
private void createExcelField()
{
    this.fields = getFields();
    this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList());
    this.maxHeight = getRowHeight();
}
    /**
     * 创建一个工作簿
     */
    public void createWorkbook()
    {
        this.wb = new SXSSFWorkbook(500);
        this.sheet = wb.createSheet();
        wb.setSheetName(0, sheetName);
        this.styles = createStyles(wb);
    }
    /**
     * 创建表格样式
     * 
     * @param wb 工作薄对象
     * @return 样式列表
     */
    private Map<String, CellStyle> createStyles(Workbook wb)
    {
        // 写入各条记录,每条记录对应excel表中的一行
        Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
        CellStyle style = wb.createCellStyle();
        // 设置一个单元格水平对其方式,居中
        style.setAlignment(HorizontalAlignment.CENTER);
        // 设置一个单元格垂直对其方式,居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        // 设置字体
        Font titleFont = wb.createFont();
        // 字体名
        titleFont.setFontName("Arial");
        // 字体高度
        titleFont.setFontHeightInPoints((short) 16);
        titleFont.setBold(true);
        style.setFont(titleFont);
        // 设置标题为纯文本
        DataFormat dataFormat = wb.createDataFormat();
        style.setDataFormat(dataFormat.getFormat("@"));
        styles.put("title", style);

        style = wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        // 设置右边框为细线
        style.setBorderRight(BorderStyle.THIN);
        // 右侧边框的颜色设置为Excel调色板中的“50%灰色
        style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        style.setBorderLeft(BorderStyle.THIN);
        style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        style.setBorderTop(BorderStyle.THIN);
        style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        style.setBorderBottom(BorderStyle.THIN);
        style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        Font dataFont = wb.createFont();
        dataFont.setFontName("Arial");
        dataFont.setFontHeightInPoints((short) 10);
        style.setFont(dataFont);
        styles.put("data", style);

        style = wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        Font totalFont = wb.createFont();
        totalFont.setFontName("Arial");
        totalFont.setFontHeightInPoints((short) 10);
        style.setFont(totalFont);
        styles.put("total", style);

        styles.putAll(annotationHeaderStyles(wb, styles));

        styles.putAll(annotationDataStyles(wb));

        return styles;
    }
  • 想要该表头,在createExcelField方法中该,
  • 所有的表格样式都在createStyles方法中
  • 想要自定义属性表格中下拉框列表,使用反射获取到该属性的Field对象,然后使用反射获取该属性的Excel注解,获取Excel注解的memberValues属性的Field对象,hField使用get方法获取所有注解Map,然后动态给map添加数据
    举例:
/**
 * 导入模版
 * @param response
 */
@PostMapping("/importTemplate")
public void importTemplate(HttpServletResponse response)
    throws IOException, NoSuchFieldException, IllegalAccessException {
    List<String> importFields = Arrays.asList("memberId", "name",
        "belongBranchName","idNumber","sex","educationLevel","politicalStatus","nationality","regularStatus","joinTime","phone","mail");
    Field field = PartyMember.class.getDeclaredField("belongBranchName");
    Excel foo = field.getAnnotation(Excel.class);
    InvocationHandler h = Proxy.getInvocationHandler(foo);
    Field hField = h.getClass().getDeclaredField("memberValues");
    hField.setAccessible(true);
    Map memberValues = (Map)hField.get(h);
    PartyBranch query = new PartyBranch();
    query.setDelFlag(0);
    List<PartyBranch> list = partyBranchService.selectPartyBranchList(query);
    if(CollectionUtils.isNotEmpty(list)) {
        String[] strs1 = list.stream().map(PartyBranch::getBranchName).toArray(String[]::new);
        memberValues.put("combo",strs1);
    }
    ExcelUtil<PartyMember> util = new ExcelUtil<>(PartyMember.class);
    util.importTemplateExcel(response, "党员数据", null,importFields);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值