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
17.64k stars 1.69k forks source link

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

Closed ccaiy9 closed 3 months ago

ccaiy9 commented 3 months ago

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
    }

}


2. 代码中的打印内容:
<img width="462" alt="image" src="https://github.com/qax-os/excelize/assets/39152125/a29479dc-18a7-4256-a071-4889bffa6d28">

4. xlsx文件:
<img width="508" alt="image" src="https://github.com/qax-os/excelize/assets/39152125/ebbd3614-913f-4b1d-bb0f-b384b52e36d6">

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

谢谢,麻烦解答下

**Output of `go version`:**

```text
GOVERSION="go1.19.13"

Excelize version or commit ID:

image

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

centos

xuri commented 3 months ago

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",
}