当前位置: 首页 > news >正文

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

 第一版:

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

 第二版:

优化内容:

检索数据的两种方式:

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

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

 

 代码:

package mainimport ("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 intf, 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)}}}

http://www.lryc.cn/news/99322.html

相关文章:

  • c++11 标准模板(STL)(std::basic_filebuf)(八)
  • 行为型模式之解释器模式
  • 阿里云域名备案
  • Clion开发Stm32之温湿度传感器(DS18B20)驱动编写和测试
  • 文档管理NAS储存安全吗?
  • 用windeployqt.exe打包Qt代码
  • 【Python机器学习】实验04(2) 机器学习应用实践--手动调参
  • 【爬虫案例】用Python爬取iPhone14的电商平台评论
  • 01)docker学习 centos7离线安装docker
  • 前端 - 实习两个星期总结
  • MySQL——主从复制
  • 报表下载工具
  • 树及其遍历
  • Qt报错解决办法
  • Python(四十七)列表对象的创建
  • #systemverilog# 说说Systemverilog中《automatic》那些事儿
  • C/C++ 动态内存分配与它的指针变量
  • UE5初学者快速入门教程
  • 论文笔记--FEDERATED LEARNING: STRATEGIES FOR IMPROVING COMMUNICATION EFFICIENCY
  • STM32MP157驱动开发——按键驱动(异步通知)
  • 医疗器械维修工程师心得
  • Vue3 Radio单选切换展示不同内容
  • FreeRTOS之二值信号量
  • ChatGPT API进阶调用指南
  • 人工智能术语翻译(四)
  • kubernetes持久化存储卷
  • 【Rust笔记】意译解构 Object Safety for trait
  • Spring Boot单元测试入门指南
  • 《面试1v1》如何能从Kafka得到准确的信息
  • 2023秋招面试题持续更新中。。。