tealeg / xlsx

Go library for reading and writing XLSX files.
Other
5.85k stars 819 forks source link

Bug:Version v3 appendsheet is empty from the second sheet, v1.0.5 is normal #783

Closed bondliu2020 closed 11 months ago

bondliu2020 commented 1 year ago

Bug: Version v3 appendsheet is empty from the second sheet,but v1.0.5 is normal.

bondliu2020 commented 1 year ago

I tested v2 and it was normal. V3 is error. wb.AppendSheet(*sheet, sheetName)

tealeg commented 11 months ago

@bondliu2020 how are you constructing the Sheet that you're passing to AppendSheet? The CellStores got added in V3 IIRC and you might have an issue around those.

bondliu2020 commented 11 months ago

func main() { var out string var inPath string flag.StringVar(&inPath, "d", "tmp", "指定需要合并的工作薄目录") flag.StringVar(&out, "o", "out.xlsx", "指定合并后文件的命名,格式:out.xlsx") flag.Parse() //设置路径,文件夹放在main的同级目录下 PthSep := string(os.PathSeparator) PthDir := inPath

dir, err := os.ReadDir(PthDir)
if err != nil {
    fmt.Printf("open dir failed: %s\n", err.Error())
}

//申明合并后的文件
var newFile *xlsx.File
newFile = xlsx.NewFile()
sCount := 0
var sheetName string
var newErr error
//检索所有文件
for _, fi := range dir {
    fmt.Printf("open success: %s\n", PthDir+PthSep+fi.Name())
    if newErr != nil {
        fmt.Printf(newErr.Error())
    }
    //循环读取每个文件
    xlFile, err := xlsx.OpenFile(PthDir + PthSep + fi.Name())
    if err != nil {
        fmt.Printf("open failed: %s\n", err)
    }
    sCount++
    for _, sheet := range xlFile.Sheets {
        fmt.Printf("Sheet Name: %s\n", sheet.Name)
        //
        sheetName = "sheet" + strconv.Itoa(sCount)
        fmt.Println("marrow", sheet.MaxRow, "合并后sheetName:", sheetName)
        _, newErr := newFile.AppendSheet(*sheet, sheetName)
        if newErr != nil {
            fmt.Printf(newErr.Error())
        }
    }
}

//写入文件
newErr = newFile.Save(out)
if newErr != nil {
    fmt.Printf(newErr.Error())
} else {
    fmt.Printf("全部文件合并完成.")
}

}

bondliu2020 commented 11 months ago

@bondliu2020 how are you constructing the Sheet that you're passing to AppendSheet? The CellStores got added in V3 IIRC and you might have an issue around those.

Above is all the code.

tealeg commented 11 months ago

@bondliu2020 - thank you, I'll try and find time to look at this soon.

tealeg commented 11 months ago

OK @bondliu2020 - I now know why this is happening. When you append the sheets you give them new sheet names (perfectly correct!), but the backing row store uses the sheet name as part of its key, so it can no longer find the rows by name (even though it has all the data still). I'll try to work up a fix.