qax-os / excelize

Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets
https://xuri.me/excelize
BSD 3-Clause "New" or "Revised" License
17.64k stars 1.69k forks source link

excelize按行读取内容成字符串数组导致日期格式错误 #1868

Closed 943885179 closed 3 months ago

943885179 commented 3 months ago

1.准备测试数据,wps正常编辑日期。 image 正常输入2024-03-25,wps会自动转成2024/3/25,为日期格式 但是通过下面代码读取,读取到的为[]string,数据异常。03-25-24 此问题会导致我输入的日期是2024-03-25 或1924-03-25 都是03-25-24

        rows, _ := f.Rows(sheet)
    for rows.Next() {
        row, err := rows.Columns(opts...)
               //上面的row返回的是[]string,解析出的日期格式异常
        if err != nil {
            break
        }
    }
xuri commented 3 months ago

Thanks for your issue. This was duplicated with issues #1157 and #1199. Please reference the documentation, this library supports specified system date and time format codes in the options when creating or opening the workbook. The built-in number format 14 and some date formats that begin with an asterisk (*) in the spreadsheet application's number format setting window were affected by system short date, long date, and long time settings. Now we can set these settings by ShortDatePattern, LongDatePattern, and LongTimePattern fields in the Options. For example, now you can open a workbook with custom date and time options like this:

f, err := excelize.OpenFile("Book1.xlsx", excelize.Options{
    ShortDatePattern: "yyyy/mm/dd",
})

After that, if you get a cell value with built-in number format ID 14, the number will be formatted with yyyy/mm/dd. This feature will be released in the next version. I'll close this, if you have any questions, please let me know, and you can reopen this anytime.