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

Tab (\t) character cannot be displayed in the generated workbook's cell #1865

Closed
ccaiy9 opened this issue Mar 28, 2024 · 1 comment
Closed
Labels
bug Something isn't working

Comments

@ccaiy9
Copy link

ccaiy9 commented Mar 28, 2024

Steps to reproduce the issue:

  1. 测试代码:
package main


import (
        "fmt"
        "github.com/xuri/excelize/v2"
)

func main() {
        var err error
  
        file := excelize.NewFile()
        defer func() {
                if err = file.Close(); err != nil {
                        fmt.Errorf("close xlsx file failed, error info: %+v", err)
                }
        }()

        sw, errN := file.NewStreamWriter("Sheet1")
        if errN != nil {
                fmt.Errorf("generate new sheet failed, error info: %+v", errN)
                return
        }

        cell, errE := excelize.CoordinatesToCellName(1, 1)
        if errE != nil {
                fmt.Errorf("CoordinatesToCellName failed, error info: %+v", errE)
                return
        }

        interfaceFields := make([]interface{}, 4)

        interfaceFields[0] = `xia
        nxu`
        interfaceFields[1] = "等\n"
        interfaceFields[2] = "  yanz\\n heng"
        interfaceFields[3] = "1         22"              // 制表符

        fmt.Printf("====== interfaceFields: %+v\n", interfaceFields)
        if errS := sw.SetRow(cell, interfaceFields); errS != nil {
                fmt.Errorf("set row failed, cell: %s, fields: %+v, error info: %+v", cell, interfaceFields, errS)
                return
        }

        if errF := sw.Flush(); errF != nil {
                fmt.Errorf("flush excel file failed, error info: %+v", errF)
                return
        }

        localPath := "/tmp/test_for_1.xlsx"
        if err = file.SaveAs(localPath); err != nil {
                fmt.Errorf("save excel file failed, error info: %+v", err)
                return
        }
}
  1. 代码中的打印内容:
image
  1. xlsx文件:
image

可以看到:
(1)122中不存在制表符
(2)“等” 后面应该是一个换行,但是xlsx中没有换行

谢谢,麻烦解答下

Output of go version:

GOVERSION="go1.19.13"

Excelize version or commit ID:

image

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

centos

@xuri xuri changed the title 当字符串中包含制表符时,生成的xlsx文件未包含制表符 Tab (\t) character cannot be displayed in the generated workbook's cell Mar 29, 2024
@xuri xuri closed this as completed in 9999221 Apr 1, 2024
@xuri xuri added the bug Something isn't working label Apr 1, 2024
@xuri
Copy link
Member

xuri commented Apr 1, 2024

Thanks for your issue.

  1. Excel doesn't support displaying tab characters in the cell, please insert space characters in a cell instead of it
  2. I have fixed the problem that the newline character in stream writer has been escaped, please upgrade to the master branch code by go get -u github.com/xuri/excelize/v2@master, and this patch will be released in the next released version. Note that, you need also create a style with the wrap text format by the NewStyle function, you will get a style ID, and set the cell with wrap text style by stream writer:
styleID, err := file.NewStyle(&excelize.Style{
    Alignment: &excelize.Alignment{WrapText: true},
})
if err != nil {
    fmt.Println(err)
    return
}
// ...
interfaceFields[1] = excelize.Cell{
    StyleID: styleID,
    Value:   "等\n",
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
No open projects
Status: Bugfix
Development

No branches or pull requests

2 participants