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

About Excel two special dates: 1900/1/0 1900/2/29 #1212

Closed
CHANTXU64 opened this issue Apr 28, 2022 · 8 comments
Closed

About Excel two special dates: 1900/1/0 1900/2/29 #1212

CHANTXU64 opened this issue Apr 28, 2022 · 8 comments
Projects

Comments

@CHANTXU64
Copy link
Contributor

CHANTXU64 commented Apr 28, 2022

Description

Excel中有两个特殊日期:1900/1/0(值为0)、1900/2/29(值为50)。如果我要在xlsx文件中设置这两个值我该如何设置呢(比如我只想设置时间,在Excel中日期就会默认成1900/1/0,excelize就无法设置一模一样的值)?SetCellValue设置日期格式好像只能用time.Time来设置,而golang的time.Time不包含这两个特殊日期。

ExcelDateToTime也无法正确处理这两个日期,且文档内未说明。

Excelize version or commit ID:

version 2.6.0

Environment details (OS, Microsoft Excel™ version, physical, etc.):
macOS: Microsoft Excel, WPS

@xuri
Copy link
Member

xuri commented Apr 29, 2022

The value of date 1900/2/29 is 60, you can set number for the cell, then create and bind the custom time and date number format to the cells. For example, set the date value for A1 and A2:

fmtCode := "yyyy/mm/dd;@"
styleID, err := f.NewStyle(&excelize.Style{CustomNumFmt: &fmtCode})
if err != nil {
    fmt.Println(err)
}
if err := f.SetCellValue("Sheet1", "A1", 0); err != nil {
    fmt.Println(err)
}
if err := f.SetCellValue("Sheet1", "A2", 60); err != nil {
    fmt.Println(err)
}
if err := f.SetCellStyle("Sheet1", "A1", "A2", styleID); err != nil {
    fmt.Println(err)
}

@CHANTXU64
Copy link
Contributor Author

对于设置一个单元格的值,使用SetCellValue,但要设置这两个特殊日期,为什么还要特地使用SetCellStyle修改样式?我认为这不合理,而且还会改变原有的样式。能不能增加一个SetCellSpecialDate之类的方法,在不改变原有方法的前提下能够设置这两个特殊日期?

@xuri
Copy link
Member

xuri commented Apr 29, 2022

Thanks for your advice. The function SetCellValue is also implemented in two-part as in the example above up, and it only set the number format for the cell which uses the default styles to avoid breaking the existing styles. I think that not required to introduce a new exported function, and I'd like to update comments in the documentation for the developers who wanna set these special dates.

@CHANTXU64
Copy link
Contributor Author

明白了,谢谢。我还有个问题,如果我想设置某个单元格为“2022-01-01”,那么我如何知道这个表是不是启用了“date1904”呢?

@xuri xuri closed this as completed in 856ee57 Apr 30, 2022
@xuri
Copy link
Member

xuri commented Apr 30, 2022

I have added 1900 or 1904 date system support for getting cell value, and now you can check if the 1904 date system is enabled via GetWorkbookPrOptions like this:

var date1904 excelize.Date1904
if err := f.GetWorkbookPrOptions(&date1904); err != nil {
    fmt.Println(err)
}
fmt.Printf("date1904: %t\n", date1904)

xuri added a commit that referenced this issue May 2, 2022
…ce, and 1904 date system support

- Fix incorrect cell data types casting results when number formatting
- Support set cell value on 1904 date system enabled, ref #1212
- Improve performance for set sheet row and the merging cells, fix performance impact when resolving #1129
@CHANTXU64
Copy link
Contributor Author

timeToExcelTime() 未处理date1904。

func TestDate1904(t *testing.T) {
  file := excelize.NewFile()
  var date1904 excelize.Date1904 = true
  if err := file.SetWorkbookPrOptions(&date1904); err != nil {
    fmt.Println(err)
  }
  tt, _ := time.Parse("2006-01-02 15:04:05", "2000-01-01 00:00:00")
  if err := file.SetCellValue("Sheet1", "A1", tt); err != nil {
    fmt.Println(err)
  }
  value, _ := file.GetCellValue("Sheet1", "A1", excelize.Options{RawCellValue: true})
  assert.Equal(t, "35064", value)
  assert.NotEqual(t, "36526", value)
  file.Close()
}

@xuri
Copy link
Member

xuri commented May 2, 2022

I have let set cell value support 1904 date system in commit eed431e. Please upgrade to the master branch code.

@CHANTXU64
Copy link
Contributor Author

Thank you!

@xuri xuri added this to Feature in v2.6.1 Jul 7, 2022
xuri added a commit to carbin-gun/excelize that referenced this issue Oct 9, 2022
xuri added a commit to carbin-gun/excelize that referenced this issue Oct 9, 2022
…formance, and 1904 date system support

- Fix incorrect cell data types casting results when number formatting
- Support set cell value on 1904 date system enabled, ref qax-os#1212
- Improve performance for set sheet row and the merging cells, fix performance impact when resolving qax-os#1129
xuri added a commit to JDavidVR/excelize that referenced this issue Jul 11, 2023
xuri added a commit to JDavidVR/excelize that referenced this issue Jul 11, 2023
…formance, and 1904 date system support

- Fix incorrect cell data types casting results when number formatting
- Support set cell value on 1904 date system enabled, ref qax-os#1212
- Improve performance for set sheet row and the merging cells, fix performance impact when resolving qax-os#1129
jenbonzhang pushed a commit to jenbonzhang/excelize that referenced this issue Oct 22, 2023
jenbonzhang pushed a commit to jenbonzhang/excelize that referenced this issue Oct 22, 2023
…formance, and 1904 date system support

- Fix incorrect cell data types casting results when number formatting
- Support set cell value on 1904 date system enabled, ref qax-os#1212
- Improve performance for set sheet row and the merging cells, fix performance impact when resolving qax-os#1129
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
v2.6.1
Feature
Development

No branches or pull requests

2 participants