Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json4excel 导出时间格式不正确 #106

Open
howe opened this issue Jun 13, 2018 · 6 comments
Open

json4excel 导出时间格式不正确 #106

howe opened this issue Jun 13, 2018 · 6 comments

Comments

@howe
Copy link
Member

howe commented Jun 13, 2018

/**
     * 完成时间
     */
    @J4EName("完成时间")
    @JsonField(dataFormat = “yyyy-MM-dd”)
    @J4EDateFormat(from = "", to = “yyyy-MM-dd”)
    @J4EDefine(type = J4EColumnType.DATA)
    private Date completeDate;

    public Date getCompleteDate() {
        return completeDate;
    }

    public void setCompleteDate(Date completeDate) {
        this.completeDate = completeDate;
    }

导出时间格式是 2018-06-01 00:00:00
@pangwu86

@jirongjun
Copy link

好像1.r.66的版本里对于toExcel只处理了IMAGE、NUMBERIC、STRING,但Type里有DATE,还有FUNCTION是没有处理的。

后来看代码,其实是可以变通的。

  1. 首先把Date的字段define成STRING(因为这个版本里只有STRING才能使用Fun)
  2. 然后弄个class实现接口J4ECellToExcel
  3. 最后在J4EConfig里对这个字段设置ToExcelFun是这个Class

亲测有效!

其实有这个Fun(toExcel & fromExcel)可以做好多转换、格式化的处理了,只是给的数据是你绑的字段的值,如果是整条记录就更好了。

@pangwu86
Copy link
Member

嗯 强烈欢迎楼上来个pull request!

@jirongjun
Copy link

jirongjun commented Nov 26, 2018

那就现丑了

比如实现一个人员的民族字段,很明显这是个数据字典一类的东西,假设是code和name吧,表里保存的是code,但导出到excel或从excel导入时用的是name。

pojo上字段的设置

    @J4EName("民族")
    @J4EDefine(type = J4EColumnType.STRING)
    private String ethnicity;

准备一个民族的Excel数据转换类(这里面我封装了一下,因为我有很多这样的处理)

public abstract class ExcelColumnHandler implements J4ECellToExcel, J4ECellFromExcel {}

public abstract class DictExcelColumnHandler extends ExcelColumnHandler {

    public abstract String getCatalog();
    
    @Override
    public Object fromExcel(Object cellVal) {
        if (Lang.isEmpty(cellVal))
            return null;
        return CommonUtil.getDictCode(getCatalog(), cellVal.toString());
    }

    @Override
    public Object toExecl(Object fieldVal) {
        if (Lang.isEmpty(fieldVal))
            return "";
        return CommonUtil.getDictName(getCatalog(), fieldVal.toString());
    }
}

public class EthnicityExcelColumnHandler extends DictExcelColumnHandler {

    @Override
    public String getCatalog() {
        return "民族";
    }
}

导出Excel(导入类似)

List<pojo> list;  //你查到的数据
//设置导出要转换的column
HashMap<String, Class<? extends ExcelColumnHandler>> handlers = new HashMap<String, Class<? extends ExcelColumnHandler>>();
//民族:码表转换
handlers.put("ethnicity", EthnicityExcelColumnHandler.class);
J4EConf config = ExcelUtil.getConfig(pojol.class, handlers);
//按顺序设置Excel的列
ExcelUtil.orderConfig(config, "...","...","ethnicity","...");
//生成Excel,流输出
super._exportToExcel(pojo.class, list, config, response);

ExcelUtil的两个方法实现

public static <T> J4EConf getConfig(Class<T> classOfT, HashMap<String, Class<? extends ExcelColumnHandler>> handlers) {
        J4EConf result = getDefaultConfig(classOfT);
        for (String key : handlers.keySet()) {
            J4EColumn column = Linq.single(J4EColumn.class, result.getColumns(), "fieldName", key);
            if (!Lang.isEmpty(column)) {
                column.setFromExcelFun(handlers.get(key));
                column.setToExcelFun(handlers.get(key));
            }
        }
        return result;
    }
    
    public static <T> void orderConfig(J4EConf config, String... fieldNames) {
        List<J4EColumn> columns = new ArrayList<J4EColumn>();
        
        for (String fieldName : fieldNames) {
            J4EColumn column = Linq.single(J4EColumn.class, config.getColumns(), "fieldName", fieldName);
            if (!Lang.isEmpty(column))
                columns.add(column);
        }
        
        config.setColumns(columns);
    }
    
    public static <T> J4EConf getDefaultConfig(Class<T> classOfT) {
        //获取默认的Config
        J4EConf conf = J4EConf.from(classOfT);
        List<J4EColumn> jcols = conf.getColumns();
        // 共通字段ignore
        for (J4EColumn col : jcols) {
            if ("createUserId".equals(col.getFieldName())
                || "createUser".equals(col.getFieldName())
                || "createTime".equals(col.getFieldName())
                || "updateUserId".equals(col.getFieldName())
                || "updateUser".equals(col.getFieldName())
                || "updateTime".equals(col.getFieldName())
                || "deleteUserId".equals(col.getFieldName())
                || "deleteUser".equals(col.getFieldName())
                || "deleteTime".equals(col.getFieldName())) {
                col.setIgnore(true);
            }
        }
        return conf;
    }

@jirongjun
Copy link

像楼主的DATE的处理,就可以是这样。

在Date的字段上设置Type是STRING,然后准备一个Handler。


public class J4EDateHandler extends ExcelColumnHandler {

    @Override
    public Object fromExcel(Object cellVal) {
        if (Lang.isEmpty(cellVal))
            return null;
        try {
            return DateUtils.parse(DateUtils.FORMAT_DATE, cellVal.toString());
        } catch (ParseException e) {
            return null;
        }
    }

    @Override
    public Object toExecl(Object fieldVal) {
        if (Lang.isEmpty(fieldVal))
            return "";
        return DateUtils.format((Date)fieldVal, DateUtils.FORMAT_DATE);
    }

}

@jirongjun
Copy link

反正你绑在啥字段上都可以,甚至可以是fetchLinks的字段,List的,都行,这样一个单元格里可以塞一个从表关系数据。
一正一反,怎么解析,怎么生成,你都可以通过这种Handler来控制。

@wendal
Copy link
Member

wendal commented Nov 26, 2018

来吧, 做成pull request提交上来

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants