Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cainiaoyigao committed Jan 20, 2022
0 parents commit 7c04cc0
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Searchfiles
查找xlsx里面内容,对应那个在哪个文件。
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module example.com/m/v2

go 1.17

require github.com/360EntSecGroup-Skylar/excelize v1.4.1

require github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/360EntSecGroup-Skylar/excelize v1.4.1 h1:l55mJb6rkkaUzOpSsgEeKYtS6/0gHwBYyfo5Jcjv/Ks=
github.com/360EntSecGroup-Skylar/excelize v1.4.1/go.mod h1:vnax29X2usfl7HHkBrX5EvSCJcmH3dT9luvxzu8iGAE=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.3-0.20181224173747-660f15d67dbb h1:cRItZejS4Ok67vfCdrbGIaqk86wmtQNOjVD7jSyS2aw=
github.com/stretchr/testify v1.2.3-0.20181224173747-660f15d67dbb/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
141 changes: 141 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package main

import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strconv"
"strings"

"github.com/360EntSecGroup-Skylar/excelize"
)

var pathStr = ""
var queryType = 0 // 0 全民查询 1 模糊查询

func getPath(strPatch string) string {
if strPatch == "" {
strPatch = GetCurrentDirectory()
}
return strPatch
}

func GetCurrentDirectory() string {
//返回绝对路径 filepath.Dir(os.Args[0])去除最后一个元素的路径
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}

//将\替换成/
return strings.Replace(dir, "\\", "/", -1)
}

func GetAllFile(pathname string, s []string) ([]string, error) {
rd, err := ioutil.ReadDir(pathname)
if err != nil {
fmt.Println("read dir fail:", err)
return s, err
}

for _, fi := range rd {
if !fi.IsDir() {
fullName := pathname + "/" + fi.Name()
s = append(s, fullName)
}
}
return s, nil
}

func main() {
fmt.Println("........目前不支持指定地址查找,请将该程序放入查找的文件夹...........")
fmt.Println("........目前只会查询当前文件夹下的xlsx文件,不会查询子文件夹下文件...........")
pathdDirectory := getPath(pathStr)
fmt.Println("........当前文件夹", pathdDirectory, "...........")
LOOP:
if !selectType() {
return
}

for {
targetStr := FuzzyHanding()

if targetStr == "exit" {
goto LOOP
}
var files []string
files, _ = GetAllFile(pathdDirectory, files)
for _, filePath := range files {
log.Println("filePath", filePath)
fileType := path.Ext(filePath)
if fileType != ".xlsx" {
continue
}
ImportFromXLS(filePath, targetStr)
}
}
}

func selectType() bool {
fmt.Println("\n请选择查找模式: 0全称 1模糊")
var qType string
_, err := fmt.Scan(&qType)
qTypeInt, err := strconv.Atoi(qType)
if err != nil {
log.Println("选择错误")
return false
}
queryType = qTypeInt

return true
}

func FuzzyHanding() string {
fmt.Println("\n请输入查找内容 输入exit重新选择查询方式:")
var targetStr string
_, err := fmt.Scan(&targetStr)
if err != nil {
return err.Error()
}
return targetStr
}

func ImportFromXLS(file, targetStr string) {
f, err := excelize.OpenFile(file)
if err != nil {
log.Println(err)
return
}

sheets := f.GetSheetMap()
for _, sheet := range sheets {
//log.Println("table name", sheet)
rows := f.GetRows(sheet)
for _, row := range rows {
for _, colCell := range row {
if IsQuery(targetStr, colCell) {
log.Println("....................find path", file, "table ", sheet, "..............................")
}
}
}
}
}

func IsQuery(targetStr, colCell string) bool {
if queryType == 0 {
if targetStr == colCell {
return true
}
} else if queryType == 1 {
if strings.Contains(colCell, targetStr) {
return true
}
} else {
log.Println("!!!!选择错误!!!!!")
}

return false
}

0 comments on commit 7c04cc0

Please sign in to comment.