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

Support insert the Kingsoft WPS Office embedded image cells via the DISPIMG formula function #1873

Open LinLiang66 opened 2 months ago

LinLiang66 commented 2 months ago

有些业务场景需要将图片嵌入到单元格内,因为如果图片浮动在单元格上面的话,业务筛选的时候会特别卡,甚至筛选后图片对位置混乱, 支持将图片嵌入到单元格后会很好的解决这一重大资源占用问题

xuri commented 2 months ago

Thanks for your issue. Sorry, there no plan to add this feature recently, but I'll certainly accept that patch if somebody did that. Note that, please enable the AutoFit options to make picture size fit with the cell when inserting pictures

LinLiang66 commented 2 months ago

`

func HandleExcel(fileBytes []byte, fileheader *multipart.FileHeader) { newFile := bytes.NewReader(fileBytes) f, err := excelize.OpenReader(newFile) if err != nil { return } sheetIndex := 1 // 假设要获取第1个工作表(下标从0开始) sheetName := f.GetSheetName(sheetIndex) rows, err := f.GetRows(sheetName) columnMap := make(map[string]int) pro := "AC" for i, header := range rows[0] { columnMap[header] = i + 1 // 行号从1开始,所以加1 if header == "申诉凭证" { pro = NumToExcelCol(i + 1) } }

for i, row := range rows[1:] {
    waybillNo := row[columnMap["快递单号"]-1]
    resp, err := http.Get("http://101.227.48.127:81/img/weightimg?waybillNo=" + waybillNo)
    if err != nil {
        continue
    }
    if !strings.HasPrefix(resp.Header.Get("Content-Type"), "image/png") {
        continue
    }
    body, err := io.ReadAll(resp.Body)
    if err != nil {
        continue
    }
    if err := f.AddPictureFromBytes(sheetName, pro+strconv.Itoa(i+2), &excelize.Picture{
        Extension: ".png",
        File:      body,
        Format: &excelize.GraphicOptions{
            AutoFit: true,
        },
    }); err != nil {
        continue
    }
}
Filename := strconv.FormatInt(time.Now().Unix()/1000, 10) + fileheader.Filename
filePath := "/gofile/" + Filename
if err := f.SaveAs(filePath); err != nil {
    fmt.Println("保存文件失败" + err.Error())
}

}

`

Yes, the current method is to insert the picture, download it to the local area, open the selected picture through WPS, and right-click to embed the cell.