alibaba / easyexcel

快速、简洁、解决大文件内存溢出的java处理Excel工具
https://easyexcel.opensource.alibaba.com
Apache License 2.0
32.09k stars 7.5k forks source link

内置format格式导致excel读取格式出错 #3923

Closed wshupengpeng closed 1 month ago

wshupengpeng commented 1 month ago

触发场景描述

使用easyExcel 3.0.5,在使用wps设置单元格格式为会计专用格式时,使用$符号开头的格式,在服务器上转换变成¥人民币符号,后面追踪代码发现在cellTagHandler读取DataFormatData的格式时,在获取excel的格式前,会先判断BuiltinFormats中是否含有对应下标的格式,如果存在则取内置枚举的格式,否则取当前excel中的格式,在BuiltinFormats#getBuiltinFormat方法中,会根据当前操作系统的locale,来判断取哪个builtinFormat,一个是US的一个是CN的,在服务器上默认使用US, 590507424d3a3c796197477394ad251 因此相同的index取值格式会有区别,具体情况如下图. 7001886e989759d107c7a97e1857a1c

触发Bug的代码

      @Test
    public void readFileToConvertIssue(){
        String filePath = "C:\\Users\\Administrator\\Desktop\\easyExcelIssue.xlsx";
        try {
            EasyExcel.read(new File(filePath))
                    .registerReadListener(new BaseListener())
//                    .registerConverter(new CustomStringStringConverter())
                    .locale(Locale.US)
                    .sheet(0)
                    .doRead();
        }catch (Exception e){
            e.printStackTrace();
        }finally {

        }
    }

所以请问一下, 这个内置格式是为了做什么用的,我们可以直接取excel自带的格式来处理么?

wshupengpeng commented 1 month ago

上面的内容有点描述错误,是中文的¥符号,在服务器上读取后被转换为$符号。

psxjoy commented 1 month ago

You can review the comments in the source code. From what I understand, this basic attribute was introduced to support internationalization and to align with local business practices. 你可以查看源码中的注释。我的理解是为了国际化和符合当地业务的使用习惯,加上了这个基础属性。

    /**
     * A <code>Locale</code> object represents a specific geographical, political, or cultural region. This parameter is
     * used when formatting dates and numbers.
     */
    private Locale locale;
wshupengpeng commented 1 month ago

但是这种方式,实际上影响到了正确格式的读取,所以,这个是否有更合适的处理方式?

psxjoy commented 1 month ago

I attempted to reproduce the issue, but it didn't occur in version 4.0.2. I'm unsure if it's a business requirement; otherwise, adding the locale(Locale.US) parameter seems intended to force conversion to U.S. currency units. Would removing .locale(Locale.US) resolve the issue? If not, please provide a minimal reproducible demo. 我尝试复现,但是在4.0.2版本上没有复现该问题。我不理解是否是业务上的要求,不然增加locale(Locale.US)这个参数,就是要求强制转换至美国货币单位。删除.locale(Locale.US)是否会正常?如果不行的话,请提供最小可复现demo。

wshupengpeng commented 1 month ago

之所以加这个参数,是因为本机的locale是CN,因此不会出现这个问题,上服务器上的locale是US,所以在服务器上出现这个问题。

psxjoy commented 1 month ago

Apologies, I don't have access to an international server at the moment, so I'm unable to reproduce the issue 😔. I suggest you try specifying Locale.CN and see if that resolves it. 抱歉我暂时没有国外的服务器,因此无法复现😔。我认为你可以指定Locale.CN尝试一下。

获取 Outlook for iOShttps://aka.ms/o0ukef


发件人: HPP @.> 发送时间: Thursday, August 8, 2024 2:29:17 PM 收件人: alibaba/easyexcel @.> 抄送: Black Pan @.>; Assign @.> 主题: Re: [alibaba/easyexcel] 内置format格式导致excel读取格式出错 (Issue #3923)

之所以加这个参数,是因为本机的locale是CN,因此不会出现这个问题,上服务器上的locale是US,所以在服务器上出现这个问题。

— Reply to this email directly, view it on GitHubhttps://github.com/alibaba/easyexcel/issues/3923#issuecomment-2275049092, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACRXFDVAGCD346EFWEWWMI3ZQMF33AVCNFSM6AAAAABMEVK45GVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENZVGA2DSMBZGI. You are receiving this because you were assigned.Message ID: @.***>

wshupengpeng commented 1 month ago

这个是我们部署到国内的云服务厂商上的,操作系统的locale是US,如果我们代码中手动设置locale,会不会导致相反的结果,比如原来是$,因为locale被人为设置为CN,从而读取变成了¥。因此,我想就这个问题,请教一下,是否可以丢弃这个内置格式,从而读取excel中对应的实际格式,从而避免这个问题,因为我发现如果使用欧元符号,相应的下标是176,超过内置的81,因此不会受到操作系统locale的限制。

psxjoy commented 1 month ago

Hi, I tested with version 4.0.2, and in the CN environment, the data doesn't convert to US. I'm not sure if this issue occurs only in earlier versions. At the moment, we don't have plans to modify this part of the code, as it involves significant core code. I'll flag this issue for discussion about a potential future upgrade. In the meantime, you could: 1. Upgrade to the latest version and see if it resolves the issue; 2. Retrieve the corresponding Locale on the server and then decide whether to perform the conversion.

Hi,我使用4.0.2版本进行测试,CN环境下的数据并不会转换为US,我不清楚这是否是版本低时候才会导致的问题。目前我们暂时没有修改这部分代码的打算(这涉及到很多底层核心代码)。我会标记这个问题并且讨论是否要后续进行升级。我觉得你可以:1.升级到最新版本尝试是否解决问题;2.在服务器中先获取对应的Locale,然后判断进行转换。

wshupengpeng commented 1 month ago

这个应该不是版本的问题,我去看了最新版本,代码逻辑没什么变化,本地也重跑测试了。如果你没有复现,可以尝试以下操作是否正确: 1 excel设置为会计专用格式带人民币符号 2 设置locale为US(目的是为了模拟服务器上的配置,否则默认读取当前操作系统的locale,有可能会导致无法复现) 稍后我也会把excel发出来

wshupengpeng commented 1 month ago

不过这个问题,我们想过把内置的Dataformat去掉就能解决,但是不知道影响面有哪些,所以问问你们,当时内置是为了什么考虑的,怕测试不充分,生产遇到新的问题。

psxjoy commented 1 month ago

The primary reason is to better accommodate the Chinese environment. I don't think it will impact other functionalities. However, I strongly recommend first checking the environment variables and then performing the appropriate conversion based on the environment.

最大的原因是为了更好的适配中文环境。我认为不会影响其他的功能。但是我最推荐的还是首先判断环境变量,根据环境进行对应的转换

获取 Outlook for iOShttps://aka.ms/o0ukef


发件人: HPP @.> 发送时间: Thursday, August 8, 2024 5:04:03 PM 收件人: alibaba/easyexcel @.> 抄送: Black Pan @.>; Assign @.> 主题: Re: [alibaba/easyexcel] 内置format格式导致excel读取格式出错 (Issue #3923)

不过这个问题,我们想过把内置的Dataformat去掉就能解决,但是不知道影响面有哪些,所以问问你们,当时内置是为了什么考虑的,怕测试不充分,生产遇到新的问题。

― Reply to this email directly, view it on GitHubhttps://github.com/alibaba/easyexcel/issues/3923#issuecomment-2275317851, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACRXFDRX3QGFB53MXVS547DZQMYAHAVCNFSM6AAAAABMEVK45GVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENZVGMYTOOBVGE. You are receiving this because you were assigned.Message ID: @.***>