feat: 增加excel2picPy生成图片方法
This commit is contained in:
parent
ff6f97aa37
commit
a665dea13d
|
|
@ -1,7 +1,13 @@
|
||||||
package bbxt
|
package bbxt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"mime/multipart"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
|
|
@ -308,6 +314,17 @@ func (b *BbxtTools) resellerDetailFillExcelV2(templatePath, outputPath string, d
|
||||||
// 取消合并合计行的A、B列
|
// 取消合并合计行的A、B列
|
||||||
// f.MergeCell(sheet, fmt.Sprintf("A%d", currentRow), fmt.Sprintf("B%d", currentRow))
|
// f.MergeCell(sheet, fmt.Sprintf("A%d", currentRow), fmt.Sprintf("B%d", currentRow))
|
||||||
|
|
||||||
|
excelBytes, err := f.WriteToBuffer()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("write to bytes failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
picBytes, err := b.excel2picPy(templatePath, excelBytes.Bytes())
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("excel2picPy failed: %v", err)
|
||||||
|
}
|
||||||
|
b.SavePic("temp.png", picBytes)
|
||||||
|
|
||||||
// 6. 保存
|
// 6. 保存
|
||||||
return f.SaveAs(outputPath)
|
return f.SaveAs(outputPath)
|
||||||
}
|
}
|
||||||
|
|
@ -319,7 +336,80 @@ func (b *BbxtTools) resellerDetailFillExcelV2(templatePath, outputPath string, d
|
||||||
// --form 'file=@"C:\\Users\\Administrator\\Downloads\\销售同比分析2025-12-29 0-12点.xlsx"' \
|
// --form 'file=@"C:\\Users\\Administrator\\Downloads\\销售同比分析2025-12-29 0-12点.xlsx"' \
|
||||||
// --form 'sheet_name="销售同比分析"'
|
// --form 'sheet_name="销售同比分析"'
|
||||||
func (b *BbxtTools) excel2picPy(templatePath string, excelBytes []byte) ([]byte, error) {
|
func (b *BbxtTools) excel2picPy(templatePath string, excelBytes []byte) ([]byte, error) {
|
||||||
|
// 1. 获取 Sheet Name
|
||||||
|
// 尝试从 excelBytes 解析,如果失败则使用默认值 "Sheet1"
|
||||||
|
sheetName := "Sheet1"
|
||||||
|
f, err := excelize.OpenReader(bytes.NewReader(excelBytes))
|
||||||
|
if err == nil {
|
||||||
|
sheetName = f.GetSheetName(0)
|
||||||
|
if sheetName == "" {
|
||||||
|
sheetName = "Sheet1"
|
||||||
|
}
|
||||||
|
f.Close()
|
||||||
|
}
|
||||||
|
|
||||||
return nil, nil
|
// 2. 构造 Multipart 请求
|
||||||
// return picBytes, nil
|
body := &bytes.Buffer{}
|
||||||
|
writer := multipart.NewWriter(body)
|
||||||
|
|
||||||
|
// 添加文件字段
|
||||||
|
// 使用 templatePath 的文件名作为上传文件名,如果没有则用 default.xlsx
|
||||||
|
filename := "default.xlsx"
|
||||||
|
if templatePath != "" {
|
||||||
|
filename = filepath.Base(templatePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
part, err := writer.CreateFormFile("file", filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("create form file failed: %v", err)
|
||||||
|
}
|
||||||
|
if _, err = part.Write(excelBytes); err != nil {
|
||||||
|
return nil, fmt.Errorf("write file part failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加 sheet_name 字段
|
||||||
|
if err = writer.WriteField("sheet_name", sheetName); err != nil {
|
||||||
|
return nil, fmt.Errorf("write field sheet_name failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = writer.Close(); err != nil {
|
||||||
|
return nil, fmt.Errorf("close writer failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 发送 HTTP POST 请求
|
||||||
|
url := "http://192.168.6.109:8010/api/v1/convert"
|
||||||
|
req, err := http.NewRequest("POST", url, body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("create request failed: %v", err)
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("send request failed: %v", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
respBody, _ := io.ReadAll(resp.Body)
|
||||||
|
return nil, fmt.Errorf("api request failed with status: %d, body: %s", resp.StatusCode, string(respBody))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 读取响应 Body (图片内容)
|
||||||
|
picBytes, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("read response body failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return picBytes, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SavePic 保存图片到本地
|
||||||
|
func (b *BbxtTools) SavePic(outputPath string, picBytes []byte) error {
|
||||||
|
dir := filepath.Dir(outputPath)
|
||||||
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||||
|
return fmt.Errorf("create directory failed: %v", err)
|
||||||
|
}
|
||||||
|
return os.WriteFile(outputPath, picBytes, 0644)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue