go节假日execl导入

//导入
func (c *HolidayController) Import() {
	if c.Ctx.Request.Method == "POST" {
		c.ImportSave()
	}
	c.setTpl("holiday/import.html", "shared/layout_pullbox.html")
	c.LayoutSections = make(map[string]string)
	c.LayoutSections["footerjs"] = "holiday/import_footerjs.html"
}
type MonthCoordinate struct{
	MonthLine int
	MonthRow  int
}

/*数字转换成Excle列中的字母,支持AA、AAA等,实际导入用不到那么多列*/
func Div(Num int)  string{
	var(
		Str string = ""
		k int
		temp []int   //保存转化后每一位数据的值,然后通过索引的方式匹配A-Z
	)
	//用来匹配的字符A-Z
	Slice := []string{"","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O",
		"P","Q","R","S","T","U","V","W","X","Y","Z"}

	if Num >26 {  //数据大于26需要进行拆分
		for {
			k = Num % 26  //从个位开始拆分,如果求余为0,说明末尾为26,也就是Z,如果是转化为26进制数,则末尾是可以为0的,这里必须为A-Z中的一个
			if k == 0 {
				temp = append(temp, 26)
				k = 26
			} else {
				temp = append(temp, k)
			}
			Num = (Num - k) / 26 //减去Num最后一位数的值,因为已经记录在temp中
			if Num <= 26{   //小于等于26直接进行匹配,不需要进行数据拆分
				temp = append(temp, Num)
				break
			}
		}
	}else{
		return Slice[Num]
	}

	for _,value := range temp{
		Str = Slice[value] + Str //因为数据切分后存储顺序是反的,所以Str要放在后面
	}
	return Str
}

func IsSingleDigit(data string) bool {
	digit := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
	for _, item := range digit {
		if data == item {
			return true
		}
	}
	return false
}

/*判断字符串是否是纯数字*/
func IsDigit(data string) bool {
	for _, item := range data {
		if IsSingleDigit(string(item)) {
			continue
		} else {
			return false
		}
	}
	return true
}

func (c *HolidayController) ImportSave() {
	var sheetCount int
	var mystr string
	t :=time.Now()
	var ImportYear string
	var MyMonth  = [12]MonthCoordinate{
		{0,0},
		{0,0},
		{0,0},
		{0,0},
		{0,0},
		{0,0},
		{0,0},
		{0,0},
		{0,0},
		{0,0},
		{0,0},
		{0,0},
	}

	//获取上传url
	str := c.GetString("Url")
	fmt.Println("Url:", str)
	//read excel
	excelFileName := "static/excel/" + str
	fmt.Println("excelFileName:", excelFileName)
	//判断文件后缀 .xlsx
	//fmt.Println("文件后缀:",path.Ext(excelFileName))
	if path.Ext(excelFileName) != ".xlsx" {
		c.jsonResult(enums.JRCodeFailed, "文件类型错误,请选择 *.xlsx 文件 !", 0)
	}
	//xlFile, err := xlsx.OpenFile(excelFileName)
	xlFile, err := excelize.OpenFile(excelFileName)
	//defer os.Remove(excelFileName)
	if err != nil {
		fmt.Printf("open failed: %s\n", err)
		c.jsonResult(enums.JRCodeFailed, "文件打开失败!", 0)
	}

	sheetCount = 0
	for index, name := range xlFile.GetSheetMap() {
		sheetCount++  //统计工作表个数
		fmt.Println(index, name)
	}
	//reVal :=
	if sheetCount > 1 {
		c.jsonResult(enums.JRCodeFailed, fmt.Sprintf("导入文件存在多个工作表! 必须保证只有一个有效的工作表!当前存在%d个", sheetCount), 0)
	}
	SheetIndex :=  xlFile.GetActiveSheetIndex()
	fmt.Println("SheetIndex:",SheetIndex)
	SheetName := xlFile.GetSheetName(SheetIndex)
	fmt.Println("GetDefinedName:",SheetName)
	SheetNameNumber := SheetName[0:4]
	fmt.Println("SheetNameNumber:",SheetNameNumber)
	if !IsDigit(SheetNameNumber){ //如果表单名前四字节不是数字,则返回异常
		c.jsonResult(enums.JRCodeFailed, fmt.Sprintf("表单名的前四个字节必须填入正确年份,当前输入异常,值为:%s", SheetNameNumber), 0)
	}

	CurrentYear :=fmt.Sprintf("%d", t.Year())
	fmt.Println("CurrentYear is :",CurrentYear)
	if SheetNameNumber >= CurrentYear{
		ImportYear = SheetNameNumber
	}else{
		c.jsonResult(enums.JRCodeFailed, fmt.Sprintf("表单名的前四个字节年份小于当前实际年份,当前年份是:%s,要求导入年份是:%s", CurrentYear,SheetNameNumber), 0)
	}

	// 获取工作表中指定单元格的值,判断是否定义了节假日对应选项 Q3  --  Q7
	for i:=0 ; i<5 ;i++{
		cell, err := xlFile.GetCellValue(SheetName, fmt.Sprintf("Q%d", i+3))
		if err != nil {
			println(err.Error())
		}
		println(cell)
		if i ==0 {
			mystr = "假日类型对应颜色:"
		}else if i==1 {
			mystr = "周末"
		}else if i==2{
			mystr = "法定节假日"
		}else if i==3{
			mystr = "年休假"
		}else if i==4{
			mystr = "调休上班日"
		}
		if cell != "假日类型对应颜色:" && cell != "周末" && cell != "法定节假日" && cell != "年休假" && cell != "调休上班日"{
			c.jsonResult(enums.JRCodeFailed, fmt.Sprintf("工作表Q%d定义异常,必须定义为:%s", i+3,mystr), 0)
		}
	}

	weekend,_ := xlFile.GetCellStyle(SheetName, "Q4")
	holiday,_ := xlFile.GetCellStyle(SheetName, "Q5")
	annualleave,_ := xlFile.GetCellStyle(SheetName, "Q6")
	other,_ := xlFile.GetCellStyle(SheetName, "Q8")
	fmt.Println("weekend:",weekend,"holiday:",holiday,"annualleave:",annualleave,"other:",other)

/*	weekend1,_ := xlFile.GetCellStyle(SheetName, "N6")
	holiday1,_ := xlFile.GetCellStyle(SheetName, "O13")
	annualleave1,_ := xlFile.GetCellStyle(SheetName, "O4")
	fmt.Println("weekend:",weekend1,"holiday:",holiday1,"annualleave:",annualleave1,"other:",other)*/
	var line int
	var column int
	line = 0


	qs := orm.NewOrm().QueryTable(models.HolidayTBName())
	i, _ := qs.PrepareInsert()
	defer i.Close(); os.Remove(excelFileName)
	succNum := 0
	failNum := 0
	fail := ""

	// 获取 Sheet1 上所有单元格,并且获取指定的节假日数据
	rows, err := xlFile.GetRows(SheetName)
	fmt.Println("rows:", rows)
	//fmt.Println("================================================================================================")
	for _, row := range rows {
		line++
		column = 0
		//fmt.Println("line:",line,"row:",row)
		fmt.Println("row:",row)
		for _, colCell := range row {
			column++
			print(colCell, "\t")
			//记录月份坐标
			if colCell == "1月"{
				MyMonth[0].MonthLine = line
				MyMonth[0].MonthRow = column
				fmt.Println("get the 1月:",line, column)
			}else if colCell == "2月"{
				MyMonth[1].MonthLine = line
				MyMonth[1].MonthRow = column
				fmt.Println("get the 2月:",line, column)
			}else if colCell == "3月"{
				MyMonth[2].MonthLine = line
				MyMonth[2].MonthRow = column
				fmt.Println("get the 3月:",line, column)
			}else if colCell == "4月"{
				MyMonth[3].MonthLine = line
				MyMonth[3].MonthRow = column
				fmt.Println("get the 4月:",line, column)
			}else if colCell == "5月"{
				MyMonth[4].MonthLine = line
				MyMonth[4].MonthRow = column
				fmt.Println("get the 5月:",line, column)
			}else if colCell == "6月"{
				MyMonth[5].MonthLine = line
				MyMonth[5].MonthRow = column
				fmt.Println("get the 6月:",line, column)
			}else if colCell == "7月"{
				MyMonth[6].MonthLine = line
				MyMonth[6].MonthRow = column
				fmt.Println("get the 7月:",line, column)
			}else if colCell == "8月"{
				MyMonth[7].MonthLine = line
				MyMonth[7].MonthRow = column
				fmt.Println("get the 8月:",line, column)
			}else if colCell == "9月"{
				MyMonth[8].MonthLine = line
				MyMonth[8].MonthRow = column
				fmt.Println("get the 9月:",line, column)
			}else if colCell == "10月"{
				MyMonth[9].MonthLine = line
				MyMonth[9].MonthRow = column
				fmt.Println("get the 10月:",line, column)
			}else if colCell == "11月"{
				MyMonth[10].MonthLine = line
				MyMonth[10].MonthRow = column
				fmt.Println("get the 11月:",line, column)
			}else if colCell == "12月"{
				MyMonth[11].MonthLine = line
				MyMonth[11].MonthRow = column
				fmt.Println("get the 12月:",line, column)
			}

			//根据月份坐标,查找对应节假日节点,并写入到数据库中
			if line > MyMonth[0].MonthLine + 1 && line < MyMonth[0].MonthLine + 8 && column >= MyMonth[0].MonthRow && column <= MyMonth[0].MonthRow + 6 {
			//1月 ,月份数据占用行数,最大是七行(包括了星期标识行),举例如下:
			//	3月
			//	一	二	三	四	五	六	日
			//	                        1
			//	2	3	4	5	6	7	8
			//	9	10	11	12	13	14	15
			//	16	17	18	19	20	21	22
			//	23	24	25	26	27	28	29
			//	30	31
			//fmt.Printf("Row:%s,line:%d",Div(column),line)
			dayStyle,_ := xlFile.GetCellStyle(SheetName,fmt.Sprintf("%s%d",Div(column),line) )
			fmt.Println("dayStyle:",dayStyle)
			if dayStyle == weekend || dayStyle == holiday ||dayStyle == annualleave  {
				data := models.Holiday{}
				if dayStyle == weekend{
					data.Name = "周末"
				}else if dayStyle == holiday{
					data.Name = "法定节假日"
				}else if dayStyle == annualleave{
					data.Name = "年休假"
				}
				//data.Name = fmt.Sprintf("%s", sheet.Rows[j].Cells[0])
				//data.Holiday = fmt.Sprintf("%s", sheet.Rows[j].Cells[1])
				day,_ := strconv.Atoi(colCell)
				data.Holiday  = fmt.Sprintf("%s-%02d-%02d", ImportYear,1,day)
				fmt.Println("holiday is :",data.Holiday)
				data.Modify = time.Now().Format("2006-01-02 15:04:05")
				data.Creator = &c.curUser

				if data.Name == "" || data.Holiday == ""{
					failNum += 1
					fail += "假日名称或假日时间不能为空."
					continue
				}else {

					succNum += 1
					id, _ := i.Insert(&data)
					fmt.Printf("id :%v \n", id)
				}
			}
			}else if line > MyMonth[1].MonthLine + 1 && line < MyMonth[1].MonthLine + 8 && column >= MyMonth[1].MonthRow && column <= MyMonth[1].MonthRow + 6 {//二月
				//fmt.Printf("Row:%s,line:%d",Div(column),line)
				dayStyle,_ := xlFile.GetCellStyle(SheetName,fmt.Sprintf("%s%d",Div(column),line) )
				fmt.Println("dayStyle:",dayStyle)
				if dayStyle == weekend || dayStyle == holiday ||dayStyle == annualleave  {
					data := models.Holiday{}
					if dayStyle == weekend{
						data.Name = "周末"
					}else if dayStyle == holiday{
						data.Name = "法定节假日"
					}else if dayStyle == annualleave{
						data.Name = "年休假"
					}
					//data.Name = fmt.Sprintf("%s", sheet.Rows[j].Cells[0])
					//data.Holiday = fmt.Sprintf("%s", sheet.Rows[j].Cells[1])
					day,_ := strconv.Atoi(colCell)
					data.Holiday  = fmt.Sprintf("%s-%02d-%02d", ImportYear,2,day)
					fmt.Println("holiday is :",data.Holiday)
					data.Modify = time.Now().Format("2006-01-02 15:04:05")
					data.Creator = &c.curUser

					if data.Name == "" || data.Holiday == ""{
						failNum += 1
						fail += "假日名称或假日时间不能为空."
						continue
					}else {

						succNum += 1
						id, _ := i.Insert(&data)
						fmt.Printf("id :%v \n", id)
					}
				}

			}else if line > MyMonth[2].MonthLine + 1 && line < MyMonth[2].MonthLine + 8 && column >= MyMonth[2].MonthRow && column <= MyMonth[2].MonthRow + 6 {//三月
				//fmt.Printf("Row:%s,line:%d",Div(column),line)
				dayStyle,_ := xlFile.GetCellStyle(SheetName,fmt.Sprintf("%s%d",Div(column),line) )
				fmt.Println("dayStyle:",dayStyle)
				if dayStyle == weekend || dayStyle == holiday ||dayStyle == annualleave  {
					data := models.Holiday{}
					if dayStyle == weekend{
						data.Name = "周末"
					}else if dayStyle == holiday{
						data.Name = "法定节假日"
					}else if dayStyle == annualleave{
						data.Name = "年休假"
					}
					//data.Name = fmt.Sprintf("%s", sheet.Rows[j].Cells[0])
					//data.Holiday = fmt.Sprintf("%s", sheet.Rows[j].Cells[1])
					day,_ := strconv.Atoi(colCell)
					data.Holiday  = fmt.Sprintf("%s-%02d-%02d", ImportYear,3,day)
					fmt.Println("holiday is :",data.Holiday)
					data.Modify = time.Now().Format("2006-01-02 15:04:05")
					data.Creator = &c.curUser

					if data.Name == "" || data.Holiday == ""{
						failNum += 1
						fail += "假日名称或假日时间不能为空."
						continue
					}else {

						succNum += 1
						id, _ := i.Insert(&data)
						fmt.Printf("id :%v \n", id)
					}
				}

			}else if line > MyMonth[3].MonthLine + 1 && line < MyMonth[3].MonthLine + 8 && column >= MyMonth[3].MonthRow && column <= MyMonth[3].MonthRow + 6 {//四月
				//fmt.Printf("Row:%s,line:%d",Div(column),line)
				dayStyle,_ := xlFile.GetCellStyle(SheetName,fmt.Sprintf("%s%d",Div(column),line) )
				fmt.Println("dayStyle:",dayStyle)
				if dayStyle == weekend || dayStyle == holiday ||dayStyle == annualleave  {
					data := models.Holiday{}
					if dayStyle == weekend{
						data.Name = "周末"
					}else if dayStyle == holiday{
						data.Name = "法定节假日"
					}else if dayStyle == annualleave{
						data.Name = "年休假"
					}
					//data.Name = fmt.Sprintf("%s", sheet.Rows[j].Cells[0])
					//data.Holiday = fmt.Sprintf("%s", sheet.Rows[j].Cells[1])
					day,_ := strconv.Atoi(colCell)
					data.Holiday  = fmt.Sprintf("%s-%02d-%02d", ImportYear,4,day)
					fmt.Println("holiday is :",data.Holiday)
					data.Modify = time.Now().Format("2006-01-02 15:04:05")
					data.Creator = &c.curUser

					if data.Name == "" || data.Holiday == ""{
						failNum += 1
						fail += "假日名称或假日时间不能为空."
						continue
					}else {

						succNum += 1
						id, _ := i.Insert(&data)
						fmt.Printf("id :%v \n", id)
					}
				}

			}else if line > MyMonth[4].MonthLine + 1 && line < MyMonth[4].MonthLine + 8 && column >= MyMonth[4].MonthRow && column <= MyMonth[4].MonthRow + 6 {//四月
				//fmt.Printf("Row:%s,line:%d",Div(column),line)
				dayStyle,_ := xlFile.GetCellStyle(SheetName,fmt.Sprintf("%s%d",Div(column),line) )
				fmt.Println("dayStyle:",dayStyle)
				if dayStyle == weekend || dayStyle == holiday ||dayStyle == annualleave  {
					data := models.Holiday{}
					if dayStyle == weekend{
						data.Name = "周末"
					}else if dayStyle == holiday{
						data.Name = "法定节假日"
					}else if dayStyle == annualleave{
						data.Name = "年休假"
					}
					//data.Name = fmt.Sprintf("%s", sheet.Rows[j].Cells[0])
					//data.Holiday = fmt.Sprintf("%s", sheet.Rows[j].Cells[1])
					day,_ := strconv.Atoi(colCell)
					data.Holiday  = fmt.Sprintf("%s-%02d-%02d", ImportYear,5,day)
					fmt.Println("holiday is :",data.Holiday)
					data.Modify = time.Now().Format("2006-01-02 15:04:05")
					data.Creator = &c.curUser

					if data.Name == "" || data.Holiday == ""{
						failNum += 1
						fail += "假日名称或假日时间不能为空."
						continue
					}else {

						succNum += 1
						id, _ := i.Insert(&data)
						fmt.Printf("id :%v \n", id)
					}
				}

			}else if line > MyMonth[5].MonthLine + 1 && line < MyMonth[5].MonthLine + 8 && column >= MyMonth[5].MonthRow && column <= MyMonth[5].MonthRow + 6 {//六月
				//fmt.Printf("Row:%s,line:%d",Div(column),line)
				dayStyle,_ := xlFile.GetCellStyle(SheetName,fmt.Sprintf("%s%d",Div(column),line) )
				fmt.Println("dayStyle:",dayStyle)
				if dayStyle == weekend || dayStyle == holiday ||dayStyle == annualleave  {
					data := models.Holiday{}
					if dayStyle == weekend{
						data.Name = "周末"
					}else if dayStyle == holiday{
						data.Name = "法定节假日"
					}else if dayStyle == annualleave{
						data.Name = "年休假"
					}
					//data.Name = fmt.Sprintf("%s", sheet.Rows[j].Cells[0])
					//data.Holiday = fmt.Sprintf("%s", sheet.Rows[j].Cells[1])
					day,_ := strconv.Atoi(colCell)
					data.Holiday  = fmt.Sprintf("%s-%02d-%02d", ImportYear,6,day)
					fmt.Println("holiday is :",data.Holiday)
					data.Modify = time.Now().Format("2006-01-02 15:04:05")
					data.Creator = &c.curUser

					if data.Name == "" || data.Holiday == ""{
						failNum += 1
						fail += "假日名称或假日时间不能为空."
						continue
					}else {

						succNum += 1
						id, _ := i.Insert(&data)
						fmt.Printf("id :%v \n", id)
					}
				}

			}else if line > MyMonth[6].MonthLine + 1 && line < MyMonth[6].MonthLine + 8 && column >= MyMonth[6].MonthRow && column <= MyMonth[6].MonthRow + 6 {//七月
				//fmt.Printf("Row:%s,line:%d",Div(column),line)
				dayStyle,_ := xlFile.GetCellStyle(SheetName,fmt.Sprintf("%s%d",Div(column),line) )
				fmt.Println("dayStyle:",dayStyle)
				if dayStyle == weekend || dayStyle == holiday ||dayStyle == annualleave  {
					data := models.Holiday{}
					if dayStyle == weekend{
						data.Name = "周末"
					}else if dayStyle == holiday{
						data.Name = "法定节假日"
					}else if dayStyle == annualleave{
						data.Name = "年休假"
					}
					//data.Name = fmt.Sprintf("%s", sheet.Rows[j].Cells[0])
					//data.Holiday = fmt.Sprintf("%s", sheet.Rows[j].Cells[1])
					day,_ := strconv.Atoi(colCell)
					data.Holiday  = fmt.Sprintf("%s-%02d-%02d", ImportYear,7,day)
					fmt.Println("holiday is :",data.Holiday)
					data.Modify = time.Now().Format("2006-01-02 15:04:05")
					data.Creator = &c.curUser

					if data.Name == "" || data.Holiday == ""{
						failNum += 1
						fail += "假日名称或假日时间不能为空."
						continue
					}else {

						succNum += 1
						id, _ := i.Insert(&data)
						fmt.Printf("id :%v \n", id)
					}
				}

			}else if line > MyMonth[7].MonthLine + 1 && line < MyMonth[7].MonthLine + 8 && column >= MyMonth[7].MonthRow && column <= MyMonth[7].MonthRow + 6 {//八月
				//fmt.Printf("Row:%s,line:%d",Div(column),line)
				dayStyle,_ := xlFile.GetCellStyle(SheetName,fmt.Sprintf("%s%d",Div(column),line) )
				fmt.Println("dayStyle:",dayStyle)
				if dayStyle == weekend || dayStyle == holiday ||dayStyle == annualleave  {
					data := models.Holiday{}
					if dayStyle == weekend{
						data.Name = "周末"
					}else if dayStyle == holiday{
						data.Name = "法定节假日"
					}else if dayStyle == annualleave{
						data.Name = "年休假"
					}
					//data.Name = fmt.Sprintf("%s", sheet.Rows[j].Cells[0])
					//data.Holiday = fmt.Sprintf("%s", sheet.Rows[j].Cells[1])
					day,_ := strconv.Atoi(colCell)
					data.Holiday  = fmt.Sprintf("%s-%02d-%02d", ImportYear,8,day)
					fmt.Println("holiday is :",data.Holiday)
					data.Modify = time.Now().Format("2006-01-02 15:04:05")
					data.Creator = &c.curUser

					if data.Name == "" || data.Holiday == ""{
						failNum += 1
						fail += "假日名称或假日时间不能为空."
						continue
					}else {

						succNum += 1
						id, _ := i.Insert(&data)
						fmt.Printf("id :%v \n", id)
					}
				}

			}else if line > MyMonth[8].MonthLine + 1 && line < MyMonth[8].MonthLine + 8 && column >= MyMonth[8].MonthRow && column <= MyMonth[8].MonthRow + 6 {//九月
				//fmt.Printf("Row:%s,line:%d",Div(column),line)
				dayStyle,_ := xlFile.GetCellStyle(SheetName,fmt.Sprintf("%s%d",Div(column),line) )
				fmt.Println("dayStyle:",dayStyle)
				if dayStyle == weekend || dayStyle == holiday ||dayStyle == annualleave  {
					data := models.Holiday{}
					if dayStyle == weekend{
						data.Name = "周末"
					}else if dayStyle == holiday{
						data.Name = "法定节假日"
					}else if dayStyle == annualleave{
						data.Name = "年休假"
					}
					//data.Name = fmt.Sprintf("%s", sheet.Rows[j].Cells[0])
					//data.Holiday = fmt.Sprintf("%s", sheet.Rows[j].Cells[1])
					day,_ := strconv.Atoi(colCell)
					data.Holiday  = fmt.Sprintf("%s-%02d-%02d", ImportYear,9,day)
					fmt.Println("holiday is :",data.Holiday)
					data.Modify = time.Now().Format("2006-01-02 15:04:05")
					data.Creator = &c.curUser

					if data.Name == "" || data.Holiday == ""{
						failNum += 1
						fail += "假日名称或假日时间不能为空."
						continue
					}else {

						succNum += 1
						id, _ := i.Insert(&data)
						fmt.Printf("id :%v \n", id)
					}
				}

			}else if line > MyMonth[9].MonthLine + 1 && line < MyMonth[9].MonthLine + 8 && column >= MyMonth[9].MonthRow && column <= MyMonth[9].MonthRow + 6 {//十月
				//fmt.Printf("Row:%s,line:%d",Div(column),line)
				dayStyle,_ := xlFile.GetCellStyle(SheetName,fmt.Sprintf("%s%d",Div(column),line) )
				fmt.Println("dayStyle:",dayStyle)
				if dayStyle == weekend || dayStyle == holiday ||dayStyle == annualleave  {
					data := models.Holiday{}
					if dayStyle == weekend{
						data.Name = "周末"
					}else if dayStyle == holiday{
						data.Name = "法定节假日"
					}else if dayStyle == annualleave{
						data.Name = "年休假"
					}
					//data.Name = fmt.Sprintf("%s", sheet.Rows[j].Cells[0])
					//data.Holiday = fmt.Sprintf("%s", sheet.Rows[j].Cells[1])
					day,_ := strconv.Atoi(colCell)
					data.Holiday  = fmt.Sprintf("%s-%02d-%02d", ImportYear,10,day)
					fmt.Println("holiday is :",data.Holiday)
					data.Modify = time.Now().Format("2006-01-02 15:04:05")
					data.Creator = &c.curUser

					if data.Name == "" || data.Holiday == ""{
						failNum += 1
						fail += "假日名称或假日时间不能为空."
						continue
					}else {

						succNum += 1
						id, _ := i.Insert(&data)
						fmt.Printf("id :%v \n", id)
					}
				}

			}else if line > MyMonth[10].MonthLine + 1 && line < MyMonth[10].MonthLine + 8 && column >= MyMonth[10].MonthRow && column <= MyMonth[10].MonthRow + 6 {//十一月
				//fmt.Printf("Row:%s,line:%d",Div(column),line)
				dayStyle,_ := xlFile.GetCellStyle(SheetName,fmt.Sprintf("%s%d",Div(column),line) )
				fmt.Println("dayStyle:",dayStyle)
				if dayStyle == weekend || dayStyle == holiday ||dayStyle == annualleave  {
					data := models.Holiday{}
					if dayStyle == weekend{
						data.Name = "周末"
					}else if dayStyle == holiday{
						data.Name = "法定节假日"
					}else if dayStyle == annualleave{
						data.Name = "年休假"
					}
					//data.Name = fmt.Sprintf("%s", sheet.Rows[j].Cells[0])
					//data.Holiday = fmt.Sprintf("%s", sheet.Rows[j].Cells[1])
					day,_ := strconv.Atoi(colCell)
					data.Holiday  = fmt.Sprintf("%s-%02d-%02d", ImportYear,11,day)
					fmt.Println("holiday is :",data.Holiday)
					data.Modify = time.Now().Format("2006-01-02 15:04:05")
					data.Creator = &c.curUser

					if data.Name == "" || data.Holiday == ""{
						failNum += 1
						fail += "假日名称或假日时间不能为空."
						continue
					}else {

						succNum += 1
						id, _ := i.Insert(&data)
						fmt.Printf("id :%v \n", id)
					}
				}

			}else if line > MyMonth[11].MonthLine + 1 && line < MyMonth[11].MonthLine + 8 && column >= MyMonth[11].MonthRow && column <= MyMonth[11].MonthRow + 6 {//十二月
				//fmt.Printf("Row:%s,line:%d",Div(column),line)
				dayStyle,_ := xlFile.GetCellStyle(SheetName,fmt.Sprintf("%s%d",Div(column),line) )
				fmt.Println("dayStyle:",dayStyle)
				if dayStyle == weekend || dayStyle == holiday ||dayStyle == annualleave  {
					data := models.Holiday{}
					if dayStyle == weekend{
						data.Name = "周末"
					}else if dayStyle == holiday{
						data.Name = "法定节假日"
					}else if dayStyle == annualleave{
						data.Name = "年休假"
					}
					//data.Name = fmt.Sprintf("%s", sheet.Rows[j].Cells[0])
					//data.Holiday = fmt.Sprintf("%s", sheet.Rows[j].Cells[1])
					day,_ := strconv.Atoi(colCell)
					data.Holiday  = fmt.Sprintf("%s-%02d-%02d", ImportYear,12,day)
					fmt.Println("holiday is :",data.Holiday)
					data.Modify = time.Now().Format("2006-01-02 15:04:05")
					data.Creator = &c.curUser

					if data.Name == "" || data.Holiday == ""{
						failNum += 1
						fail += "假日名称或假日时间不能为空."
						continue
					}else {

						succNum += 1
						id, _ := i.Insert(&data)
						fmt.Printf("id :%v \n", id)
					}
				}

			}

			//print("column",column,colCell, "\t")
		}
		//for idx ,val :=range MyMonth{
		//	fmt.Println("idx:",idx,"val:",val)
		//}
		println()
	}

	if succNum > 0 && failNum > 0 {
		c.jsonResult(enums.JRCodeSucc, "成功数量:"+strconv.Itoa(succNum)+",失败数量:"+strconv.Itoa(failNum)+";原因:"+fail, 0)
	}else if succNum > 0 && failNum == 0 {
		c.jsonResult(enums.JRCodeSucc, "成功数量: "+strconv.Itoa(succNum)+".", 0)
	}else {
		c.jsonResult(enums.JRCodeFailed, "失败数量:"+strconv.Itoa(failNum)+";原因:"+fail, 0)
	}
}
//文件上传
func (c *HolidayController) Upload() {
	//这里type没有用,只是为了演示传值
	stype, _ := c.GetInt32("type", 0)
	if stype > 0 {
		f, h, err := c.GetFile("fileUrl")
		if err != nil {
			c.jsonResult(enums.JRCodeFailed, "上传失败", "")
		}
		defer f.Close()
		if !FileExists("static/excel") {
			os.MkdirAll("static/excel", os.ModePerm)
		}
		filePath := "static/excel/" + h.Filename
		// 保存位置在 static/upload, 没有文件夹要先创建
		c.SaveToFile("fileUrl", filePath)
		c.jsonResult(enums.JRCodeSucc, "上传成功", h.Filename)
	} else {
		c.jsonResult(enums.JRCodeFailed, "上传失败", "")
	}
}
在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值