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

The first execution of Flush() data will be written to Excel, and SetRow() after Flush() will not take effect #1869

Closed chaimengfei closed 6 months ago

chaimengfei commented 6 months ago

When flush() is executed multiple times, it will only be written to the Excel file on the first execution,and the generated Excel table only has one record :Amy 10

The example:

// 创建一个新的 Excel 文件
    f := excelize.NewFile()

    // 使用 NewStreamWriter 方法创建一个流式写入器
    sw, err := f.NewStreamWriter("Sheet1")
    if err != nil {
        panic(err)
    }

    // 向工作表写入数据并多次调用 Flush() 方法
    if err = sw.SetRow("A1", []interface{}{"name", "age"}); err != nil {
        panic(err)
    }
    if err = sw.Flush(); err != nil {
        panic(err)
    }
    if err = sw.SetRow("A2", []interface{}{"Amy", "10"}); err != nil {
        panic(err)
    }
    if err = sw.Flush(); err != nil {
        panic(err)
    }
    if err = sw.SetRow("A3", []interface{}{"Danel", "12"}); err != nil {
        panic(err)
    }
    if err = sw.Flush(); err != nil {
        panic(err)
    }

    // 保存 Excel 文件
    if err = f.SaveAs("example.xlsx"); err != nil {
        panic(err)
    }
xuri commented 6 months ago

Thanks for your issue. In streaming writing mode, just call Flush function one time after data write finished for each worksheet. I'll close this issue, if you have any questions, please let me know, and you can reopen this anytime.