|
@@ -2,381 +2,381 @@ package util
|
|
|
|
|
|
//数据导出excel
|
|
|
import (
|
|
|
- "errors"
|
|
|
- "fmt"
|
|
|
- "io"
|
|
|
- "jy/src/jfw/modules/subscribepay/src/config"
|
|
|
- "log"
|
|
|
- "os"
|
|
|
- "strings"
|
|
|
- "time"
|
|
|
+ "errors"
|
|
|
+ "fmt"
|
|
|
+ "io"
|
|
|
+ "jy/src/jfw/modules/subscribepay/src/config"
|
|
|
+ "log"
|
|
|
+ "os"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
|
|
|
- qutil "app.yhyue.com/moapp/jybase/common"
|
|
|
- "github.com/gogf/gf/v2/util/gconv"
|
|
|
+ qutil "app.yhyue.com/moapp/jybase/common"
|
|
|
+ "github.com/gogf/gf/v2/util/gconv"
|
|
|
|
|
|
- "github.com/tealeg/xlsx"
|
|
|
+ "github.com/tealeg/xlsx"
|
|
|
)
|
|
|
|
|
|
var (
|
|
|
- defaultExcelBorder = xlsx.Border{ //边框样式
|
|
|
- Left: "dotted",
|
|
|
- LeftColor: "00000000",
|
|
|
- Right: "dotted",
|
|
|
- RightColor: "00000000",
|
|
|
- Top: "dotted",
|
|
|
- TopColor: "00000000",
|
|
|
- Bottom: "dotted",
|
|
|
- BottomColor: "00000000",
|
|
|
- }
|
|
|
+ defaultExcelBorder = xlsx.Border{ //边框样式
|
|
|
+ Left: "dotted",
|
|
|
+ LeftColor: "00000000",
|
|
|
+ Right: "dotted",
|
|
|
+ RightColor: "00000000",
|
|
|
+ Top: "dotted",
|
|
|
+ TopColor: "00000000",
|
|
|
+ Bottom: "dotted",
|
|
|
+ BottomColor: "00000000",
|
|
|
+ }
|
|
|
|
|
|
- defaultExcelAlignment = xlsx.Alignment{ //对其方式
|
|
|
- Horizontal: "left",
|
|
|
- Vertical: "center",
|
|
|
- WrapText: true,
|
|
|
- }
|
|
|
- defaultFont = xlsx.NewFont(12, "等线")
|
|
|
- blueFont = xlsx.Font{
|
|
|
- Size: 12,
|
|
|
- Name: "等线",
|
|
|
- Color: "00376092",
|
|
|
- }
|
|
|
+ defaultExcelAlignment = xlsx.Alignment{ //对其方式
|
|
|
+ Horizontal: "left",
|
|
|
+ Vertical: "center",
|
|
|
+ WrapText: true,
|
|
|
+ }
|
|
|
+ defaultFont = xlsx.NewFont(12, "等线")
|
|
|
+ blueFont = xlsx.Font{
|
|
|
+ Size: 12,
|
|
|
+ Name: "等线",
|
|
|
+ Color: "00376092",
|
|
|
+ }
|
|
|
)
|
|
|
|
|
|
// Information 情报
|
|
|
type Information struct {
|
|
|
- Id string `ch:"id" json:"id"`
|
|
|
- Title string `ch:"title" json:"title"`
|
|
|
- Summary string `ch:"summary" json:"summary"`
|
|
|
- Content string `ch:"content" json:"content"`
|
|
|
- Basis string `ch:"basis" json:"basis"`
|
|
|
- Area string `ch:"area" json:"area"`
|
|
|
- City string `ch:"city" json:"city"`
|
|
|
- DataJson string `ch:"datajson" json:"datajson"`
|
|
|
- Class string `ch:"class" json:"class"`
|
|
|
- DataJsonId string `ch:"datajson_id" json:"datajson_id"`
|
|
|
- Buyer string `json:"buyer"`
|
|
|
- Amount float64 `json:"amount"`
|
|
|
- Href string `json:"href"`
|
|
|
+ Id string `ch:"id" json:"id"`
|
|
|
+ Title string `ch:"title" json:"title"`
|
|
|
+ Summary string `ch:"summary" json:"summary"`
|
|
|
+ Content string `ch:"content" json:"content"`
|
|
|
+ Basis string `ch:"basis" json:"basis"`
|
|
|
+ Area string `ch:"area" json:"area"`
|
|
|
+ City string `ch:"city" json:"city"`
|
|
|
+ DataJson string `ch:"datajson" json:"datajson"`
|
|
|
+ Class string `ch:"class" json:"class"`
|
|
|
+ DataJsonId string `ch:"datajson_id" json:"datajson_id"`
|
|
|
+ Buyer string `json:"buyer"`
|
|
|
+ Amount float64 `json:"amount"`
|
|
|
+ Href string `json:"href"`
|
|
|
}
|
|
|
|
|
|
// 获取
|
|
|
func GetExcelFilePath(orderCode string, num int) string {
|
|
|
- now := time.Now()
|
|
|
- filename := fmt.Sprintf("%s.xlsx", fmt.Sprintf("%s_%d_%s_%s", now.Format("20060102"), num, orderCode, qutil.GetLetterRandom(5)))
|
|
|
- return fmt.Sprintf("/dataexport/%s/%s/%s", now.Format("2006"), now.Format("01"), filename)
|
|
|
+ now := time.Now()
|
|
|
+ filename := fmt.Sprintf("%s.xlsx", fmt.Sprintf("%s_%d_%s_%s", now.Format("20060102"), num, orderCode, qutil.GetLetterRandom(5)))
|
|
|
+ return fmt.Sprintf("/dataexport/%s/%s/%s", now.Format("2006"), now.Format("01"), filename)
|
|
|
}
|
|
|
|
|
|
// 获取excel导出数据长度
|
|
|
func GetDataExportFileDataLen(path string) (int, error) {
|
|
|
- xFile, err := xlsx.OpenFile(path)
|
|
|
- if err != nil {
|
|
|
- return -1, err
|
|
|
- }
|
|
|
- sheet1, ok := xFile.Sheet["Sheet1"]
|
|
|
- if !ok {
|
|
|
- return -1, fmt.Errorf("not find sheet1")
|
|
|
- }
|
|
|
- if len(sheet1.Rows) == 0 {
|
|
|
- return -1, fmt.Errorf("sheet1 hasn't Rows data ")
|
|
|
- }
|
|
|
- //避免最后一行为合并单元格数据
|
|
|
- for l := len(sheet1.Rows) - 1; l > 0; l-- {
|
|
|
- if sheet1.Rows[l].Cells[0].Value != "" {
|
|
|
- return sheet1.Rows[l].Cells[0].Int()
|
|
|
- }
|
|
|
- }
|
|
|
- return -1, fmt.Errorf("sheet1 get error")
|
|
|
+ xFile, err := xlsx.OpenFile(path)
|
|
|
+ if err != nil {
|
|
|
+ return -1, err
|
|
|
+ }
|
|
|
+ sheet1, ok := xFile.Sheet["Sheet1"]
|
|
|
+ if !ok {
|
|
|
+ return -1, fmt.Errorf("not find sheet1")
|
|
|
+ }
|
|
|
+ if len(sheet1.Rows) == 0 {
|
|
|
+ return -1, fmt.Errorf("sheet1 hasn't Rows data ")
|
|
|
+ }
|
|
|
+ //避免最后一行为合并单元格数据
|
|
|
+ for l := len(sheet1.Rows) - 1; l > 0; l-- {
|
|
|
+ if sheet1.Rows[l].Cells[0].Value != "" {
|
|
|
+ return sheet1.Rows[l].Cells[0].Int()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return -1, fmt.Errorf("sheet1 get error")
|
|
|
}
|
|
|
|
|
|
// 生成数据导出excel
|
|
|
func CreateDataExportExcelFile(list *[]map[string]interface{}, isSenior bool, filePath string) error {
|
|
|
- file, err := ExportExcelFile(list, isSenior)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- //if err := FolderCheck(filePath); err != nil {
|
|
|
- // return err
|
|
|
- //}
|
|
|
- return file.Save(filePath)
|
|
|
+ file, err := ExportExcelFile(list, isSenior)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if err := FolderCheck(filePath); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return file.Save(filePath)
|
|
|
}
|
|
|
func ExportExcelFile(list *[]map[string]interface{}, isSenior bool) (*xlsx.File, error) {
|
|
|
- //if list == nil || len(*list) == 0 {
|
|
|
- // return errors.New("未查询到数据")
|
|
|
- //}
|
|
|
- //创建excel文件
|
|
|
- file := xlsx.NewFile()
|
|
|
- sheet, err := file.AddSheet("Sheet1")
|
|
|
- if err != nil {
|
|
|
- return nil, errors.New("创建Sheet1失败")
|
|
|
- }
|
|
|
- var row *xlsx.Row
|
|
|
- var cell *xlsx.Cell
|
|
|
- var excelHead []*config.ExeclCell
|
|
|
- //添加头部标题
|
|
|
- if !isSenior {
|
|
|
- row = sheet.AddRow()
|
|
|
- row.SetHeight(20)
|
|
|
- excelHead = config.ExConf.Standard_Fields
|
|
|
- for index, excel := range excelHead {
|
|
|
- cell = row.AddCell()
|
|
|
- cell.SetString(excel.Name)
|
|
|
- style := &xlsx.Style{
|
|
|
- Fill: *xlsx.NewFill("solid", excel.Color, excel.Color),
|
|
|
- Alignment: defaultExcelAlignment,
|
|
|
- Font: *defaultFont,
|
|
|
- Border: defaultExcelBorder,
|
|
|
- }
|
|
|
- cell.SetStyle(style)
|
|
|
- sheet.Col(index).Width = excel.Width
|
|
|
- }
|
|
|
- } else {
|
|
|
- excelHead = config.ExConf.Senior_Fields
|
|
|
- //铺设第一层title
|
|
|
- row = sheet.AddRow()
|
|
|
- row.SetHeight(20)
|
|
|
- lastF := false //合并行时标识
|
|
|
- for _, excel := range excelHead {
|
|
|
- if excel.VMerge != 1 && excel.HMerge == 0 {
|
|
|
- if lastF { //不可读取内容
|
|
|
- lastF = false
|
|
|
- continue
|
|
|
- }
|
|
|
- cell = row.AddCell()
|
|
|
- } else {
|
|
|
- lastF = true
|
|
|
- cell = row.AddCell()
|
|
|
- cell.SetString(excel.Name)
|
|
|
- cell.VMerge = excel.VMerge
|
|
|
- cell.HMerge = excel.HMerge
|
|
|
- }
|
|
|
- if excel.HMerge != 0 {
|
|
|
- lastF = true
|
|
|
- }
|
|
|
- style := &xlsx.Style{
|
|
|
- Fill: *xlsx.NewFill("solid", excel.Color, excel.Color),
|
|
|
- Alignment: defaultExcelAlignment,
|
|
|
- Font: *defaultFont,
|
|
|
- Border: defaultExcelBorder,
|
|
|
- }
|
|
|
- cell.SetStyle(style)
|
|
|
- }
|
|
|
- //铺设第二层title并设置宽度
|
|
|
- row = sheet.AddRow()
|
|
|
- row.SetHeight(20)
|
|
|
- cNum := 0
|
|
|
- for _, excel := range excelHead {
|
|
|
- if excel.HMerge != 0 {
|
|
|
- continue
|
|
|
- }
|
|
|
- cell = row.AddCell()
|
|
|
- if excel.VMerge != 1 {
|
|
|
- cell.SetString(excel.Name)
|
|
|
- }
|
|
|
- sheet.Col(cNum).Width = excel.Width
|
|
|
- cNum++
|
|
|
- cell.SetStyle(&xlsx.Style{
|
|
|
- Fill: *xlsx.NewFill("solid", excel.Color, excel.Color),
|
|
|
- Border: defaultExcelBorder,
|
|
|
- Alignment: defaultExcelAlignment,
|
|
|
- Font: *defaultFont,
|
|
|
- })
|
|
|
- }
|
|
|
- }
|
|
|
- if list != nil && len(*list) > 0 {
|
|
|
- //填充数据
|
|
|
- for k, data := range *list {
|
|
|
- winnerMaps := gconv.Maps(data["winnerMaps"])
|
|
|
- winnerTotal := len(winnerMaps)
|
|
|
- data["index"] = k + 1
|
|
|
- if winnerTotal > 0 {
|
|
|
- for index, winnerDatas := range winnerMaps {
|
|
|
- row = sheet.AddRow()
|
|
|
- row.SetHeight(20)
|
|
|
- for _, v := range excelHead {
|
|
|
- if v.Filed == "" {
|
|
|
- continue
|
|
|
- }
|
|
|
- cell = row.AddCell()
|
|
|
- style := &xlsx.Style{
|
|
|
- Border: defaultExcelBorder,
|
|
|
- Alignment: defaultExcelAlignment,
|
|
|
- }
|
|
|
- if v.Filed != "legal_person" && v.Filed != "company_phone" && v.Filed != "company_email" && v.Filed != "company_name" {
|
|
|
- if index == 0 {
|
|
|
- cell.VMerge = winnerTotal - 1
|
|
|
- if v.Filed == "title" || v.Filed == "url" {
|
|
|
- cell.SetFormula(fmt.Sprintf("=HYPERLINK(\"%s\",\"%s\")", data["url_jump"], data[v.Filed]))
|
|
|
- } else if v.Filed == "href" {
|
|
|
- cell.SetFormula(fmt.Sprintf("=HYPERLINK(\"%s\",\"%s\")", data["href"], data[v.Filed]))
|
|
|
- } else {
|
|
|
- cell.SetValue(data[v.Filed])
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- cell.SetValue(winnerDatas[v.Filed])
|
|
|
- }
|
|
|
- if v.Filed == "title" {
|
|
|
- style.Font = blueFont
|
|
|
- }
|
|
|
- cell.SetStyle(style)
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- row = sheet.AddRow()
|
|
|
- row.SetHeight(20)
|
|
|
- for _, v := range excelHead {
|
|
|
- if v.Filed == "" {
|
|
|
- continue
|
|
|
- }
|
|
|
- cell = row.AddCell()
|
|
|
- style := &xlsx.Style{
|
|
|
- Border: defaultExcelBorder,
|
|
|
- Alignment: defaultExcelAlignment,
|
|
|
- }
|
|
|
- if v.Filed == "title" || v.Filed == "url" {
|
|
|
- cell.SetFormula(fmt.Sprintf("=HYPERLINK(\"%s\",\"%s\")", data["url_jump"], data[v.Filed]))
|
|
|
- } else if v.Filed == "href" {
|
|
|
- cell.SetFormula(fmt.Sprintf("=HYPERLINK(\"%s\",\"%s\")", data["href"], data[v.Filed]))
|
|
|
- } else {
|
|
|
- cell.SetValue(data[v.Filed])
|
|
|
- }
|
|
|
- if v.Filed == "title" {
|
|
|
- style.Font = blueFont
|
|
|
- }
|
|
|
- cell.SetStyle(style)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return file, nil
|
|
|
+ //if list == nil || len(*list) == 0 {
|
|
|
+ // return errors.New("未查询到数据")
|
|
|
+ //}
|
|
|
+ //创建excel文件
|
|
|
+ file := xlsx.NewFile()
|
|
|
+ sheet, err := file.AddSheet("Sheet1")
|
|
|
+ if err != nil {
|
|
|
+ return nil, errors.New("创建Sheet1失败")
|
|
|
+ }
|
|
|
+ var row *xlsx.Row
|
|
|
+ var cell *xlsx.Cell
|
|
|
+ var excelHead []*config.ExeclCell
|
|
|
+ //添加头部标题
|
|
|
+ if !isSenior {
|
|
|
+ row = sheet.AddRow()
|
|
|
+ row.SetHeight(20)
|
|
|
+ excelHead = config.ExConf.Standard_Fields
|
|
|
+ for index, excel := range excelHead {
|
|
|
+ cell = row.AddCell()
|
|
|
+ cell.SetString(excel.Name)
|
|
|
+ style := &xlsx.Style{
|
|
|
+ Fill: *xlsx.NewFill("solid", excel.Color, excel.Color),
|
|
|
+ Alignment: defaultExcelAlignment,
|
|
|
+ Font: *defaultFont,
|
|
|
+ Border: defaultExcelBorder,
|
|
|
+ }
|
|
|
+ cell.SetStyle(style)
|
|
|
+ sheet.Col(index).Width = excel.Width
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ excelHead = config.ExConf.Senior_Fields
|
|
|
+ //铺设第一层title
|
|
|
+ row = sheet.AddRow()
|
|
|
+ row.SetHeight(20)
|
|
|
+ lastF := false //合并行时标识
|
|
|
+ for _, excel := range excelHead {
|
|
|
+ if excel.VMerge != 1 && excel.HMerge == 0 {
|
|
|
+ if lastF { //不可读取内容
|
|
|
+ lastF = false
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ cell = row.AddCell()
|
|
|
+ } else {
|
|
|
+ lastF = true
|
|
|
+ cell = row.AddCell()
|
|
|
+ cell.SetString(excel.Name)
|
|
|
+ cell.VMerge = excel.VMerge
|
|
|
+ cell.HMerge = excel.HMerge
|
|
|
+ }
|
|
|
+ if excel.HMerge != 0 {
|
|
|
+ lastF = true
|
|
|
+ }
|
|
|
+ style := &xlsx.Style{
|
|
|
+ Fill: *xlsx.NewFill("solid", excel.Color, excel.Color),
|
|
|
+ Alignment: defaultExcelAlignment,
|
|
|
+ Font: *defaultFont,
|
|
|
+ Border: defaultExcelBorder,
|
|
|
+ }
|
|
|
+ cell.SetStyle(style)
|
|
|
+ }
|
|
|
+ //铺设第二层title并设置宽度
|
|
|
+ row = sheet.AddRow()
|
|
|
+ row.SetHeight(20)
|
|
|
+ cNum := 0
|
|
|
+ for _, excel := range excelHead {
|
|
|
+ if excel.HMerge != 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ cell = row.AddCell()
|
|
|
+ if excel.VMerge != 1 {
|
|
|
+ cell.SetString(excel.Name)
|
|
|
+ }
|
|
|
+ sheet.Col(cNum).Width = excel.Width
|
|
|
+ cNum++
|
|
|
+ cell.SetStyle(&xlsx.Style{
|
|
|
+ Fill: *xlsx.NewFill("solid", excel.Color, excel.Color),
|
|
|
+ Border: defaultExcelBorder,
|
|
|
+ Alignment: defaultExcelAlignment,
|
|
|
+ Font: *defaultFont,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if list != nil && len(*list) > 0 {
|
|
|
+ //填充数据
|
|
|
+ for k, data := range *list {
|
|
|
+ winnerMaps := gconv.Maps(data["winnerMaps"])
|
|
|
+ winnerTotal := len(winnerMaps)
|
|
|
+ data["index"] = k + 1
|
|
|
+ if winnerTotal > 0 {
|
|
|
+ for index, winnerDatas := range winnerMaps {
|
|
|
+ row = sheet.AddRow()
|
|
|
+ row.SetHeight(20)
|
|
|
+ for _, v := range excelHead {
|
|
|
+ if v.Filed == "" {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ cell = row.AddCell()
|
|
|
+ style := &xlsx.Style{
|
|
|
+ Border: defaultExcelBorder,
|
|
|
+ Alignment: defaultExcelAlignment,
|
|
|
+ }
|
|
|
+ if v.Filed != "legal_person" && v.Filed != "company_phone" && v.Filed != "company_email" && v.Filed != "company_name" {
|
|
|
+ if index == 0 {
|
|
|
+ cell.VMerge = winnerTotal - 1
|
|
|
+ if v.Filed == "title" || v.Filed == "url" {
|
|
|
+ cell.SetFormula(fmt.Sprintf("=HYPERLINK(\"%s\",\"%s\")", data["url_jump"], data[v.Filed]))
|
|
|
+ } else if v.Filed == "href" {
|
|
|
+ cell.SetFormula(fmt.Sprintf("=HYPERLINK(\"%s\",\"%s\")", data["href"], data[v.Filed]))
|
|
|
+ } else {
|
|
|
+ cell.SetValue(data[v.Filed])
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ cell.SetValue(winnerDatas[v.Filed])
|
|
|
+ }
|
|
|
+ if v.Filed == "title" {
|
|
|
+ style.Font = blueFont
|
|
|
+ }
|
|
|
+ cell.SetStyle(style)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ row = sheet.AddRow()
|
|
|
+ row.SetHeight(20)
|
|
|
+ for _, v := range excelHead {
|
|
|
+ if v.Filed == "" {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ cell = row.AddCell()
|
|
|
+ style := &xlsx.Style{
|
|
|
+ Border: defaultExcelBorder,
|
|
|
+ Alignment: defaultExcelAlignment,
|
|
|
+ }
|
|
|
+ if v.Filed == "title" || v.Filed == "url" {
|
|
|
+ cell.SetFormula(fmt.Sprintf("=HYPERLINK(\"%s\",\"%s\")", data["url_jump"], data[v.Filed]))
|
|
|
+ } else if v.Filed == "href" {
|
|
|
+ cell.SetFormula(fmt.Sprintf("=HYPERLINK(\"%s\",\"%s\")", data["href"], data[v.Filed]))
|
|
|
+ } else {
|
|
|
+ cell.SetValue(data[v.Filed])
|
|
|
+ }
|
|
|
+ if v.Filed == "title" {
|
|
|
+ style.Font = blueFont
|
|
|
+ }
|
|
|
+ cell.SetStyle(style)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return file, nil
|
|
|
}
|
|
|
|
|
|
// 校验文件夹是否存在;
|
|
|
// 若不存在则创建;
|
|
|
func FolderCheck(path string) error {
|
|
|
- pathCheck := path[:strings.LastIndex(path, "/")]
|
|
|
- //校验文件夹是否存在
|
|
|
- _, pathError := os.Stat(pathCheck)
|
|
|
- if pathError != nil && os.IsNotExist(pathError) {
|
|
|
- // 创建文件夹
|
|
|
- return os.MkdirAll(pathCheck, os.ModePerm)
|
|
|
- }
|
|
|
- return nil
|
|
|
+ pathCheck := path[:strings.LastIndex(path, "/")]
|
|
|
+ //校验文件夹是否存在
|
|
|
+ _, pathError := os.Stat(pathCheck)
|
|
|
+ if pathError != nil && os.IsNotExist(pathError) {
|
|
|
+ // 创建文件夹
|
|
|
+ return os.MkdirAll(pathCheck, os.ModePerm)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
}
|
|
|
|
|
|
// 移动文件至指定目录
|
|
|
func MoveFile(src, dst string) (int64, error) {
|
|
|
- srcFile, err := os.Open(src)
|
|
|
- if err != nil {
|
|
|
- return 0, err
|
|
|
- }
|
|
|
- defer srcFile.Close()
|
|
|
- //文件夹
|
|
|
- err = FolderCheck(dst)
|
|
|
- if err != nil {
|
|
|
- return 0, err
|
|
|
- }
|
|
|
- dstFile, err := os.Create(dst)
|
|
|
- if err != nil {
|
|
|
- return 0, err
|
|
|
- }
|
|
|
- defer dstFile.Close()
|
|
|
- return io.Copy(dstFile, srcFile)
|
|
|
+ srcFile, err := os.Open(src)
|
|
|
+ if err != nil {
|
|
|
+ return 0, err
|
|
|
+ }
|
|
|
+ defer srcFile.Close()
|
|
|
+ //文件夹
|
|
|
+ err = FolderCheck(dst)
|
|
|
+ if err != nil {
|
|
|
+ return 0, err
|
|
|
+ }
|
|
|
+ dstFile, err := os.Create(dst)
|
|
|
+ if err != nil {
|
|
|
+ return 0, err
|
|
|
+ }
|
|
|
+ defer dstFile.Close()
|
|
|
+ return io.Copy(dstFile, srcFile)
|
|
|
}
|
|
|
|
|
|
// 人脉通导出情报信息
|
|
|
func ExportExcelFileForNw(list map[string][]Information) (*xlsx.File, error) {
|
|
|
- //创建excel文件
|
|
|
- file := xlsx.NewFile()
|
|
|
- for s, information := range list {
|
|
|
- sheet, err := file.AddSheet(s)
|
|
|
- if err != nil {
|
|
|
- log.Println("sheet创建失败: ", s)
|
|
|
- return nil, errors.New(s + "创建Sheet1失败")
|
|
|
- }
|
|
|
- var row *xlsx.Row
|
|
|
- var cell *xlsx.Cell
|
|
|
- excelHead := config.ExConf.NetWork_Fields
|
|
|
- //铺设第一层title
|
|
|
- row = sheet.AddRow()
|
|
|
- row.SetHeight(20)
|
|
|
- lastF := false //合并行时标识
|
|
|
- for _, excel := range excelHead {
|
|
|
- if excel.VMerge != 1 && excel.HMerge == 0 {
|
|
|
- if lastF { //不可读取内容
|
|
|
- lastF = false
|
|
|
- continue
|
|
|
- }
|
|
|
- cell = row.AddCell()
|
|
|
- } else {
|
|
|
- lastF = true
|
|
|
- cell = row.AddCell()
|
|
|
- cell.SetString(excel.Name)
|
|
|
- cell.VMerge = excel.VMerge
|
|
|
- cell.HMerge = excel.HMerge
|
|
|
- }
|
|
|
- if excel.HMerge != 0 {
|
|
|
- lastF = true
|
|
|
- }
|
|
|
- style := &xlsx.Style{
|
|
|
- Fill: *xlsx.NewFill("solid", excel.Color, excel.Color),
|
|
|
- Alignment: defaultExcelAlignment,
|
|
|
- Font: *defaultFont,
|
|
|
- Border: defaultExcelBorder,
|
|
|
- }
|
|
|
- cell.SetStyle(style)
|
|
|
- }
|
|
|
- //铺设第二层title并设置宽度
|
|
|
- row = sheet.AddRow()
|
|
|
- row.SetHeight(20)
|
|
|
- cNum := 0
|
|
|
- for _, excel := range excelHead {
|
|
|
- if excel.HMerge != 0 {
|
|
|
- continue
|
|
|
- }
|
|
|
- cell = row.AddCell()
|
|
|
- if excel.VMerge != 1 {
|
|
|
- cell.SetString(excel.Name)
|
|
|
- }
|
|
|
- sheet.Col(cNum).Width = excel.Width
|
|
|
- cNum++
|
|
|
- cell.SetStyle(&xlsx.Style{
|
|
|
- Fill: *xlsx.NewFill("solid", excel.Color, excel.Color),
|
|
|
- Border: defaultExcelBorder,
|
|
|
- Alignment: defaultExcelAlignment,
|
|
|
- Font: *defaultFont,
|
|
|
- })
|
|
|
- }
|
|
|
+ //创建excel文件
|
|
|
+ file := xlsx.NewFile()
|
|
|
+ for s, information := range list {
|
|
|
+ sheet, err := file.AddSheet(s)
|
|
|
+ if err != nil {
|
|
|
+ log.Println("sheet创建失败: ", s)
|
|
|
+ return nil, errors.New(s + "创建Sheet1失败")
|
|
|
+ }
|
|
|
+ var row *xlsx.Row
|
|
|
+ var cell *xlsx.Cell
|
|
|
+ excelHead := config.ExConf.NetWork_Fields
|
|
|
+ //铺设第一层title
|
|
|
+ row = sheet.AddRow()
|
|
|
+ row.SetHeight(20)
|
|
|
+ lastF := false //合并行时标识
|
|
|
+ for _, excel := range excelHead {
|
|
|
+ if excel.VMerge != 1 && excel.HMerge == 0 {
|
|
|
+ if lastF { //不可读取内容
|
|
|
+ lastF = false
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ cell = row.AddCell()
|
|
|
+ } else {
|
|
|
+ lastF = true
|
|
|
+ cell = row.AddCell()
|
|
|
+ cell.SetString(excel.Name)
|
|
|
+ cell.VMerge = excel.VMerge
|
|
|
+ cell.HMerge = excel.HMerge
|
|
|
+ }
|
|
|
+ if excel.HMerge != 0 {
|
|
|
+ lastF = true
|
|
|
+ }
|
|
|
+ style := &xlsx.Style{
|
|
|
+ Fill: *xlsx.NewFill("solid", excel.Color, excel.Color),
|
|
|
+ Alignment: defaultExcelAlignment,
|
|
|
+ Font: *defaultFont,
|
|
|
+ Border: defaultExcelBorder,
|
|
|
+ }
|
|
|
+ cell.SetStyle(style)
|
|
|
+ }
|
|
|
+ //铺设第二层title并设置宽度
|
|
|
+ row = sheet.AddRow()
|
|
|
+ row.SetHeight(20)
|
|
|
+ cNum := 0
|
|
|
+ for _, excel := range excelHead {
|
|
|
+ if excel.HMerge != 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ cell = row.AddCell()
|
|
|
+ if excel.VMerge != 1 {
|
|
|
+ cell.SetString(excel.Name)
|
|
|
+ }
|
|
|
+ sheet.Col(cNum).Width = excel.Width
|
|
|
+ cNum++
|
|
|
+ cell.SetStyle(&xlsx.Style{
|
|
|
+ Fill: *xlsx.NewFill("solid", excel.Color, excel.Color),
|
|
|
+ Border: defaultExcelBorder,
|
|
|
+ Alignment: defaultExcelAlignment,
|
|
|
+ Font: *defaultFont,
|
|
|
+ })
|
|
|
+ }
|
|
|
|
|
|
- if information != nil && len(information) > 0 {
|
|
|
- //填充数据
|
|
|
- for i, info := range information {
|
|
|
- m := make(map[string]interface{})
|
|
|
- m = gconv.Map(info)
|
|
|
- row = sheet.AddRow()
|
|
|
- row.SetHeight(20)
|
|
|
- for _, v := range excelHead {
|
|
|
- if v.Filed == "" {
|
|
|
- continue
|
|
|
- }
|
|
|
- cell = row.AddCell()
|
|
|
- style := &xlsx.Style{
|
|
|
- Border: defaultExcelBorder,
|
|
|
- Alignment: defaultExcelAlignment,
|
|
|
- }
|
|
|
- if v.Filed == "index" {
|
|
|
- cell.SetValue(i + 1)
|
|
|
- } else {
|
|
|
- cell.SetValue(m[v.Filed])
|
|
|
- if v.Filed == "title" {
|
|
|
- style.Font = blueFont
|
|
|
- }
|
|
|
- }
|
|
|
+ if information != nil && len(information) > 0 {
|
|
|
+ //填充数据
|
|
|
+ for i, info := range information {
|
|
|
+ m := make(map[string]interface{})
|
|
|
+ m = gconv.Map(info)
|
|
|
+ row = sheet.AddRow()
|
|
|
+ row.SetHeight(20)
|
|
|
+ for _, v := range excelHead {
|
|
|
+ if v.Filed == "" {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ cell = row.AddCell()
|
|
|
+ style := &xlsx.Style{
|
|
|
+ Border: defaultExcelBorder,
|
|
|
+ Alignment: defaultExcelAlignment,
|
|
|
+ }
|
|
|
+ if v.Filed == "index" {
|
|
|
+ cell.SetValue(i + 1)
|
|
|
+ } else {
|
|
|
+ cell.SetValue(m[v.Filed])
|
|
|
+ if v.Filed == "title" {
|
|
|
+ style.Font = blueFont
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- cell.SetStyle(style)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return file, nil
|
|
|
+ cell.SetStyle(style)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return file, nil
|
|
|
}
|