go 查询采购单设备事项[小示例]V2-两种模式{严格,包含模式}

 第一版:

https://mp.csdn.net/mp_blog/creation/editor/131979385

 第二版:

优化内容:

检索数据的两种方式:

1.严格模式--找寻名称是一模一样的内容,在上一个版本实现了

2.包含模式,也就是我输入检索关键字“语音配线架”【原来名称为‘50对语音配线架’】,这样可以快速找到同类别的内容

go 查询采购单设备事项[小示例]V2-两种模式{严格,包含模式}_第1张图片

 

 代码:

package main

import (
	"fmt"
	"github.com/xuri/excelize/v2"
	"strings"
)

//eve sheet name的内容

// InSlice 判断字符串是否在 slice 中。
func InSlice(items []string, item string) bool {
	for _, eachItem := range items {
		if eachItem == item {
			return true
		}
	}
	return false
}

// InSlice 判断字符串是否在 slice 中。
func includeSlice(items []string, item string) bool {
	for _, eachItem := range items {
		//字符串包含函数,判断表格中名字是否包含我的列表中的内容
		//这样我的找寻字符串中写入关键字就可以了
		if strings.Contains(item, eachItem) {
			return true
		}
	}
	return false
}

// 我需要检索的信息
// var devicelists []string = []string{"50对语音配线架", "24口网络配线架", "24口网络理线架", "4口终端盒", "光纤收发器", "96芯光纤配线架"}
var devicelists = []string{"50对语音配线架", "24口网络配线架", "24口网络理线架", "4口终端盒", "光纤收发器", "96芯光纤配线架"}

var keydevicelists = []string{"语音配线架", "网络配线架", "网络理线架", "终端盒", "光纤收发器", "光纤配线架"}

func sheet(sheetname string, f *excelize.File, format int) {
	//fmt.Println("+++++++++++++++++++++++++")
	// 获取 Sheet1 上所有单元格
	rows, err := f.GetRows(sheetname)
	if err != nil {
		fmt.Println(err)
		return
	}
	//fmt.Println(rows)

	for _, row := range rows {
		//fmt.Println(row, "========", index)
		//判断每行的里的字段长度,如果是小于6 那就是不获取设备名字和设备价格
		if len(row) < 6 {
			continue
		}
		//获取excel中,设备的名称
		kindname := row[1]
		//获取excel中,设备的数量
		kcount := row[4]

		//如果表格中 kcount没有内容,那就是给复制一个“0”字符串
		if kcount == "" {
			kcount = "0"
		}

		//fmt.Printf("固定列名字是:%s,类别是%T", kindname, kindname)
		//fmt.Println()
		//调用函数  用来判断 设备名称是否是我需要寻找的内容
		//如果是我需要寻找的内容就是返回对应表的名称以及 设备名称和 对应数量
		//if InSlice(devicelists, kindname) {
		//	//fmt.Println(sheetname, kindname, "=====++++++=======》", kcount)
		//	fmt.Printf("获取信息:sheet表:%s;设备名称:%s;设备数量:%s\n;", sheetname, kindname, kcount)
		//	//fmt.Println()
		//}

		//判断模式,可以进行采用 不同的检索方式
		//1.包含模式,我检索的都是“关键字”  2.严格模式 ,内容必须得一模一样的查找
		if format == 1 {
			//fmt.Println("你匹配的模式为包含模式")

			//包含的调用
			if includeSlice(keydevicelists, kindname) {
				//fmt.Println(sheetname, kindname, "=====++++++=======》", kcount)
				fmt.Printf("获取信息:sheet表:%s;设备名称:%s;设备数量:%s\n;", sheetname, kindname, kcount)
				//fmt.Println()
			}

		} else {
			//fmt.Println("你匹配的模式为严格模式")
			if InSlice(devicelists, kindname) {
				//fmt.Println(sheetname, kindname, "=====++++++=======》", kcount)
				fmt.Printf("获取信息:sheet表:%s;设备名称:%s;设备数量:%s\n;", sheetname, kindname, kcount)
				//fmt.Println()
			}

		}

	}
	//fmt.Println("+++++++++++++++++++++++++")
	//指定单元格的值,查询
	//value, _ := f.GetCellValue(sheetname, "B7")
	//fmt.Println(value)
	//for _, row := range rows {
	//	for _, colCell := range row {
	//		fmt.Print(colCell, "\t")
	//	}
	//	fmt.Println()
	//}

}

func sheetv2(sheetname string, f *excelize.File) {
	//fmt.Println("+++++++++++++++++++++++++")
	// 获取 Sheet1 上所有单元格
	rows, err := f.GetRows(sheetname)
	if err != nil {
		fmt.Println(err)
		return
	}
	//fmt.Println(rows)

	for _, row := range rows {
		//fmt.Println(row, "========", index)
		//判断每行的里的字段长度,如果是小于6 那就是不获取设备名字和设备价格
		if len(row) < 6 {
			continue
		}
		//获取excel中,设备的名称
		kindname := row[1]
		//获取excel中,设备的数量
		kcount := row[4]

		//如果表格中 kcount没有内容,那就是给复制一个“0”字符串
		if kcount == "" {
			kcount = "0"
		}

		//fmt.Printf("固定列名字是:%s,类别是%T", kindname, kindname)
		//fmt.Println()
		//调用函数  用来判断 设备名称是否是我需要寻找的内容
		//如果是我需要寻找的内容就是返回对应表的名称以及 设备名称和 对应数量
		//if InSlice(devicelists, kindname) {
		//	//fmt.Println(sheetname, kindname, "=====++++++=======》", kcount)
		//	fmt.Printf("获取信息:sheet表:%s;设备名称:%s;设备数量:%s\n;", sheetname, kindname, kcount)
		//	//fmt.Println()
		//}

		//包含的调用
		if includeSlice(devicelists, kindname) {
			//fmt.Println(sheetname, kindname, "=====++++++=======》", kcount)
			fmt.Printf("获取信息:sheet表:%s;设备名称:%s;设备数量:%s\n;", sheetname, kindname, kcount)
			//fmt.Println()
		}
	}
	//fmt.Println("+++++++++++++++++++++++++")
	//指定单元格的值,查询
	//value, _ := f.GetCellValue(sheetname, "B7")
	//fmt.Println(value)
	//for _, row := range rows {
	//	for _, colCell := range row {
	//		fmt.Print(colCell, "\t")
	//	}
	//	fmt.Println()
	//}

}
func sheet_bak(sheetname string, f *excelize.File) {
	// 获取 Sheet1 上所有单元格
	rows, err := f.GetRows(sheetname)
	if err != nil {
		fmt.Println(err)
		return
	}
	for _, row := range rows {
		for _, colCell := range row {
			fmt.Print(colCell, "\t")
		}
		fmt.Println()
	}

}

// 读取Excel表格
func main() {
	//选择的匹配模式:1-包含模式,2-严格模式
	var mode int
	f, err := excelize.OpenFile("by.xlsx")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer func() {
		if err := f.Close(); err != nil {
			fmt.Println(err)
		}
	}()

	sheetnames := f.GetSheetList()
	//fmt.Printf("本excel表格的sheetnames:%s", sheetnames)
	//fmt.Println("===============")
	for {
		fmt.Println("请输入你需要的匹配模式:1-包含模式,2-严格模式,输入 1 或者 2 :")
		fmt.Scan(&mode)
		//退出内容
		if mode == 88 {
			break
		}

		//遍历所有的表格sheet,调用处理函数
		for _, sheetname := range sheetnames {
			//fmt.Println(sheetname)
			sheet(sheetname, f, mode)
		}

	}

}

你可能感兴趣的:(golang语言实战代码,golang,开发语言,后端)