123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- package example
- import (
- "bytes"
- "errors"
- "fmt"
- "github.com/flipped-aurora/gin-vue-admin/server/global"
- "github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
- "github.com/flipped-aurora/gin-vue-admin/server/model/system"
- "github.com/xuri/excelize/v2"
- "strconv"
- "strings"
- )
- type ExcelService struct {
- }
- func (exa *ExcelService) ParseInfoList2Excel(infoList []system.SysBaseMenu, filePath string) error {
- excel := excelize.NewFile()
- excel.SetSheetRow("Sheet1", "A1", &[]string{"ID", "路由Name", "路由Path", "是否隐藏", "父节点", "排序", "文件名称"})
- for i, menu := range infoList {
- axis := fmt.Sprintf("A%d", i+2)
- excel.SetSheetRow("Sheet1", axis, &[]interface{}{
- menu.ID,
- menu.Name,
- menu.Path,
- menu.Hidden,
- menu.ParentId,
- menu.Sort,
- menu.Component,
- })
- }
- err := excel.SaveAs(filePath)
- return err
- }
- func (exa *ExcelService) ProblemInfoList2Excel(infoList []autocode.ProblemInfo, filePath string, dictList []system.SysDictionary) error {
- excel := excelize.NewFile()
- excel.SetSheetRow("Sheet1", "A1", &[]string{"ID", "问题发布时间", "问题处理时间", "问题视频地址", "问题图片", "站点名称", "站点位置", "问题", "站点类型", "责任部门", "处理状态", "处理图片", "处理内容", "处理人"})
- for i, problemInfo := range infoList {
- axis := fmt.Sprintf("A%d", i+2)
- var matterStr bytes.Buffer
- for _, dictionary := range dictList {
- if dictionary.Name == problemInfo.SiteType {
- for index, id := range strings.Split(problemInfo.Matter, "|") {
- tempid, _ := strconv.Atoi(id)
- if len(dictionary.SysDictionaryDetails) >= tempid+1 {
- matterStr.WriteString(strconv.Itoa(index+1) + "." + dictionary.SysDictionaryDetails[tempid].Label + " \n")
- }
- }
- }
- }
- excel.SetSheetRow("Sheet1", axis, &[]interface{}{
- problemInfo.ID,
- problemInfo.CreatedAt,
- problemInfo.UpdatedAt,
- problemInfo.Video,
- problemInfo.Imgs,
- problemInfo.SiteName,
- "https://apis.map.qq.com/uri/v1/marker?marker=coord:" + problemInfo.Position + ";title:" + problemInfo.SiteName + ";addr:" + problemInfo.SiteType,
- matterStr.String(),
- problemInfo.SiteType,
- problemInfo.Department,
- problemInfo.Status,
- problemInfo.HandImgs,
- problemInfo.HandText,
- problemInfo.Handler,
- })
- }
- err := excel.SaveAs(filePath)
- return err
- }
- func (exa *ExcelService) ParseExcel2InfoList() ([]system.SysBaseMenu, error) {
- skipHeader := true
- fixedHeader := []string{"ID", "路由Name", "路由Path", "是否隐藏", "父节点", "排序", "文件名称"}
- file, err := excelize.OpenFile(global.GVA_CONFIG.Excel.Dir + "ExcelImport.xlsx")
- if err != nil {
- return nil, err
- }
- menus := make([]system.SysBaseMenu, 0)
- rows, err := file.Rows("Sheet1")
- if err != nil {
- return nil, err
- }
- for rows.Next() {
- row, err := rows.Columns()
- if err != nil {
- return nil, err
- }
- if skipHeader {
- if exa.compareStrSlice(row, fixedHeader) {
- skipHeader = false
- continue
- } else {
- return nil, errors.New("Excel格式错误")
- }
- }
- if len(row) != len(fixedHeader) {
- continue
- }
- id, _ := strconv.Atoi(row[0])
- hidden, _ := strconv.ParseBool(row[3])
- sort, _ := strconv.Atoi(row[5])
- menu := system.SysBaseMenu{
- GVA_MODEL: global.GVA_MODEL{
- ID: uint(id),
- },
- Name: row[1],
- Path: row[2],
- Hidden: hidden,
- ParentId: row[4],
- Sort: sort,
- Component: row[6],
- }
- menus = append(menus, menu)
- }
- return menus, nil
- }
- func (exa *ExcelService) compareStrSlice(a, b []string) bool {
- if len(a) != len(b) {
- return false
- }
- if (b == nil) != (a == nil) {
- return false
- }
- for key, value := range a {
- if value != b[key] {
- return false
- }
- }
- return true
- }
|