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
18.05k stars 1.71k forks source link

SetCellStr creates cells that are interpreted by excel with format "General" #1915

Closed nicolasfranck closed 4 months ago

nicolasfranck commented 4 months ago

Description

When I use the method SetCellStr I get cells that are formatted by Microsoft Excel with format "General" instead of the expected format "Text"

Output of go version:

go version go1.21.4 darwin/arm64

Excelize version or commit ID:

2.8.0

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

MAC OS X

xuri commented 4 months ago

Thanks for your issue. This was duplicated with issue #705, you can set cell A1 as text by number format like this:

var style int
if style, err = f.NewStyle(&excelize.Style{NumFmt: 49}); err != nil {
    fmt.Println(err)
    return
}
if err = f.SetCellStyle("Sheet1", "A1", "A1", style); err != nil {
    fmt.Println(err)
    return
}

This library also support set multiple cell's style by SetColStyle and SetRowStyle functions.

nicolasfranck commented 4 months ago

I am not sure what this means: should the method SetCellStr do this actually, or should cell styling do this?

What does NumFmt 49 do btw?

xuri commented 4 months ago

The SetCellStr function used for accept Go language's string data type value, this function won't affect the format of the cell, so if you need set cell style with specified number format, may need to create the style with number format by the NewStyle function at first, and you will get a style index, and then set cells style with that. The excelize library supports create style with number format by built-in number format index or custom number format code, the number 49 represents the "Text" format. As the comments in issue #705 say, you can find more built-in number format mapping details on the documentation website.

nicolasfranck commented 4 months ago

Ok, thanks!