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

When using file.SetCellValue(),it shows a warning that the number is in text format, which could lead to calculation errors. #1778

Closed
moyu-yangl opened this issue Jan 10, 2024 · 2 comments

Comments

@moyu-yangl
Copy link

I'm currently facing an issue with excelize. When using file.SetCellValue(), I input the value "$1231". However, when I download the xlsx file, it shows a warning that the number is in text format, which could lead to calculation errors. I want to input a string like "$1231" because it clearly should not be directly converted to a number based on the requirements, but I would like to convert it to a number in the final step before downloading.
image
I'm not sure if there is a suitable function or approach to solve this problem.

@xuri
Copy link
Member

xuri commented Jan 10, 2024

Thanks for your issue. As the documentation of the SetCellValue function says, it supports multiple data types. Please set the cell value by Go's numeric data type, then set the currency number formats for the cells. For example:

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)
        }
    }()
    // Set cell value
    if err := f.SetCellValue("Sheet1", "A1", 1231); err != nil {
        fmt.Println(err)
        return
    }
    // Create new style with built-in currency formats
    styleID, err := f.NewStyle(&excelize.Style{
        NumFmt: 165,
    })
    if err != nil {
        fmt.Println(err)
        return
    }
    // Set number format for the cells
    if err := f.SetCellStyle("Sheet1", "A1", "A1", styleID); err != nil {
        fmt.Println(err)
        return
    }
    if err := f.SaveAs("Book1.xlsx"); err != nil {
        fmt.Println(err)
        return
    }
}

@xuri
Copy link
Member

xuri commented Jan 17, 2024

I've closed this. If you have any question, please let me know, and reopen this anytime.

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

2 participants