-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Set style not applying custom format correctly #1677
Comments
Thanks for your issue. I have tested with the following code and it works well. Do you open an existing workbook or create a new workbook? Could you provide more details about this, such as an attachment to reproduce this issue. package main
import (
"fmt"
"github.com/xuri/excelize/v2"
)
func main() {
f := excelize.NewFile()
defer func() {
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
f.SetCellValue("Sheet1", "A3", 45187)
dateformat := "yyyy-mm-dd"
dateStyle, err := f.NewStyle(&excelize.Style{
CustomNumFmt: &dateformat,
})
if err != nil {
fmt.Println(err)
return
}
err = f.SetCellStyle("Sheet1", "A3", "A3", dateStyle)
if err != nil {
fmt.Println(err)
return
}
dateStr, err := f.GetCellValue("Sheet1", "A3")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(dateStr) // 2023-09-18
if err := f.SaveAs("Book1.xlsx"); err != nil {
fmt.Println(err)
}
} |
@xuri please find the following attachments. I removed some data. Just try to read the date from A3. This file is computer generated by a third party tool. |
Thanks for your feedback. I have tested it by following the code with your attachment, and it works well. Could you show us a complete, standalone example program or reproducible demo? package main
import (
"fmt"
"github.com/xuri/excelize/v2"
)
func main() {
f, err := excelize.OpenFile("sample.xlsx")
if err != nil {
fmt.Println(err)
return
}
defer func() {
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
sheet := f.GetSheetList()[1]
dateStr, err := f.GetCellValue(sheet, "A3")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(dateStr) // 09.18.2023
dateformat := "yyyy-mm-dd"
dateStyle, err := f.NewStyle(&excelize.Style{
CustomNumFmt: &dateformat,
})
if err != nil {
fmt.Println(err)
return
}
err = f.SetCellStyle(sheet, "A3", "A3", dateStyle)
if err != nil {
fmt.Println(err)
return
}
dateStr, err = f.GetCellValue(sheet, "A3")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(dateStr) // 2023-09-18
if err := f.SaveAs("Book1.xlsx"); err != nil {
fmt.Println(err)
}
} |
I have copied your code exactly and Got a different result.
Please note that opening the excel file and clicking on save seems to fix this issue. |
I have fixed it, please try to upgrade the master branch code, and this patch will be released in the next version. |
I have upgraded to the latest version. The issue is indeed fixed. Thank you very much @xuri. |
…located - Improve compatibility with empty custom number format code
Go: 1.21.1
Excelize: v2.8.1-0.20230917103058-744236b4b840
this is the raw value of the cell
applying a style using the Excel buitin formats works
Applying a custom format treats the value like money instead of a date
The text was updated successfully, but these errors were encountered: