[Golang] 纯文本查看 复制代码
/*
Description: 下载excel中的图片
author: Phoenix
date: 20230613
email: [url=mailto:1044714221@qq.com]1044714221@qq.com[/url]
*/
package main
import (
"errors"
"fmt"
"github.com/tealeg/xlsx"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"syscall"
"unsafe"
)
func main() {
// 1. 获取当前文件位置
dir, err := os.Getwd()
if err != nil {
Msg("Error", err.Error())
}
// 2. 获取当前文件夹下所有的excel文件
fileList := make([]string, 0)
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { // 遍历当前文件夹下所有文件
if filepath.Ext(path) == ".xlsx" { // 判断文件后缀是否为.xlsx
fileList = append(fileList, path) // 将文件路径添加到fileList中
}
return nil
})
// 3. 读取excel文件
for _, file := range fileList {
// 获取文件名称
fileName := filepath.Base(file)
// 创建同名文件夹
dirName := fileName[:len(fileName)-len(filepath.Ext(fileName))]
os.Mkdir(dirName, os.ModePerm)
dataList := ReadExcel(file)
wg := sync.WaitGroup{}
maxGoroutine := make(chan struct{}, 20) // 最大并发数
for _, data := range dataList {
m := make(map[string]string)
for k, v := range data {
m[k] = v
}
wg.Add(1)
go func(m map[string]string) {
maxGoroutine <- struct{}{}
DownLoadImgs(m["list"], m["name"], dir+"/"+dirName)
<-maxGoroutine
wg.Done()
}(m)
}
wg.Wait()
}
Msg("Success", "下载完成")
}
// ------------------------------ util ------------------------------
func DownLoadImgs(imgs string, filename, imgPath string) {
fmt.Println("imgPath:", imgPath)
// imgs 转为 []string 按照逗号分隔
urls := strings.Split(imgs, ",")
for index, url := range urls {
// 下载图片
imgName := filepath.Base(url)
imgTag := filepath.Ext(imgName) // 获取图片后缀
imgPath2 := filepath.Join(imgPath, filename)
os.Mkdir(imgPath2, os.ModePerm)
imgName2 := filepath.Join(imgPath, filename, filename+"_"+fmt.Sprintf("%d", index+1)+imgTag)
// 2. 下载图片
DownloadFile(url, imgName2)
}
}
// DownloadFile 下载图片
func DownloadFile(url, filename string) (err error) {
resp, err := http.Get(url)
if err != nil {
Msg("Error1", err.Error())
os.Exit(1)
return errors.New("下载图片失败" + err.Error())
}
defer resp.Body.Close()
file, err := os.Create(filename)
if err != nil {
Msg("Error2", err.Error())
os.Exit(1)
return errors.New("创建图片失败" + err.Error())
}
defer file.Close()
_, err = io.Copy(file, resp.Body)
if err != nil {
Msg("Error3", err.Error())
os.Exit(1)
return errors.New("保存图片失败" + err.Error())
}
return nil
}
// Msg 弹窗
func Msg(msgType string, msg string) {
user32 := syscall.MustLoadDLL("user32.dll")
MessageBoxW := user32.MustFindProc("MessageBoxW")
title := msgType
text := msg
ret, _, _ := MessageBoxW.Call(
0,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(title))),
0)
if ret == 0 {
return
}
}
// ReadExcel 读取xlsx文件
func ReadExcel(filename string) []map[string]string {
var data []map[string]string
xlFile, err := xlsx.OpenFile(filename)
if err != nil {
fmt.Println(err)
return data
}
for _, sheet := range xlFile.Sheets {
headers := make([]string, len(sheet.Rows[0].Cells))
for i, cell := range sheet.Rows[0].Cells {
headers[i] = cell.String()
}
for _, row := range sheet.Rows[1:] {
rowData := make(map[string]string)
for i, cell := range row.Cells {
if headers[i] == "" {
continue
}
rowData[headers[i]] = cell.Value
}
data = append(data, rowData)
}
}
return data
}