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.62k stars 1.68k forks source link

Generated corrupted Excel file on go1.21.5, excelize v2.8.1 #1925

Closed MeTuA closed 2 weeks ago

MeTuA commented 2 weeks ago

Description Hello, I am having a corruption error while generating an Excel file. Using go version 1.21.5, excelize v2.8.1. It looks like a duplicate, but I couldn't find an issue with the go 1.21.5 version.

Steps to reproduce the issue:

func Export(ctx context.Context) (*excelize.File, error) {
    sheetName := "Sheet1"

    f := excelize.NewFile()

    err = drawTableHeaders(ctx, f, sheetName)
    if err != nil {
        return nil, err
    }

    err = drawTableBody(ctx, f, sheetName)
    if err != nil {
        return nil, err
    }

    limit := 5
    tableRange := fmt.Sprintf("A1:N%d", limit)
    disable := false
    err = f.AddTable(sheetName, &excelize.Table{
        Range:             tableRange,
        Name:              "table",
        StyleName:         "TableStyleMedium2",
        ShowFirstColumn:   true,
        ShowLastColumn:    true,
        ShowRowStripes:    &disable,
        ShowColumnStripes: true,
    })
    if err != nil {
        return nil, err
    }

    f.SetColWidth(sheetName, "A", "L", 17)
    f.SetColWidth(sheetName, "M", "N", 20)

    return f, nil
}

then

func ExportController(c *gin.Context) (interface{}, error) {
    ...

    f, err := Export(c.Request.Context())
    if err != nil {
        return nil, errors.Ctx().Just(err)
    }
    defer f.Close()

    c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
    c.Header("Content-Disposition", "attachment; filename=data.xlsx")
    c.Header("Content-Transfer-Encoding", "binary")
    c.Header("Expires", "0")

    if err := f.Write(c.Writer); err != nil {
        c.String(http.StatusInternalServerError, err.Error())
    }

    return nil, nil
}

Describe the results you received: image

Describe the results you expected: Opening without error Output of go version: 1.21.5

Excelize version or commit ID: v2.8.1

Environment details (OS, Microsoft Excel™ version, physical, etc.): Windows 11, Microsoft® Excel® for Microsoft 365 MSO (Version 2405 Build 16.0.17628.20006) 64-bit

MeTuA commented 2 weeks ago

Problem was on another part

tjalp commented 2 weeks ago

Problem was on another part

I'm having the same issue. What did you do to fix it?

xuri commented 1 day ago

@tjalp, the workbook maybe corrupted caused by many different reasons, please provide more details or create issue to show us a reproducible demo.