tealeg / xlsx

Go library for reading and writing XLSX files.
Other
5.81k stars 810 forks source link

Use Cell.SetDataValidation to set the cell drop list, the saved xlsx file cannot be opened #722

Closed linzhicheng5512 closed 1 year ago

linzhicheng5512 commented 2 years ago

I wrote a simple program (use xlsx v3.23) to test the cell drop-down list, but the saved xlsx file cannot be opened. My code is as follows:

    file := xlsx.NewFile()
    sheet, err := file.AddSheet("sheet aaaa")
    if err != nil {
        panic(err)
    }
    row := sheet.AddRow()
        row.SetHeight(13.5)
    cell := row.AddCell()
    cell.Value = "hello123"
    var title = "hello cell"
    var msg = "hello cell msg"
    dd := xlsx.NewDataValidation(0, 0, 0, 0, true)
    err = dd.SetDropList([]string{"hello123", "hello456", "hello789"})
    if err != nil {
        panic(err)
    }
    dd.SetInput(&title, &msg)
    cell.SetDataValidation(dd)
    if err := file.Save("testdroplist.xlsx"); err != nil {
        panic(err)
    }

When I tried to open testdroplist.xlsx with Excel, the following error was thrown: "An error was detected in testdroplist.xlsx, the replaced part: /xl/worksheets/sheet1.xml with xml error. Loading error. Line 2, Column 762"

But when I use xlsx v2.0.1 to implement the above code, there is no problem. I tried to compare the xlsx files generated by the two versions, and found that the order of the sheetData and dataValidations in /xl/worksheets/sheet1.xml is different. In the xlsx file generated by v2.0.1, sheetData is before dataValidations, but in the xlsx file generated by v3.2.3, dataValidations are before sheetData. Therefore, when I try to use a text editor to put sheetData before dataValidations, save it, and then open the file, no error is reported. I don't know what the problem is

linzhicheng5512 commented 2 years ago

I'm running v3.2.3 with go1.16.2 , Excel 2013

linzhicheng5512 commented 2 years ago

Well, I found that the master has solved this bug. image

rentiansheng commented 2 years ago

https://github.com/tealeg/xlsx/issues/679 the reason for the problem is explained here

github-actions[bot] commented 1 year ago

Stale issue message