【go】Excelize处理excel表 带数据校验的文件导出

1 Excelize介绍

go get "github.com/xuri/excelize/v2"

2 相关需求与实现

2.1 数据的excel文件导出

  • 需求:入参是一个map,其键是每个sheet页的名字,其值是一个二维切片。在每个sheet页中依次写入切片中的数据。返回文件名和错误信息。
  • 代码:
// 写入数据到export/*.xlsx
// datas: map[sheet]每个sheet页的数据
// return: 文件名, error

func WriteExcel(datas map[string][][]string) (string, error) {
    
    
	file := excelize.NewFile()

	for sheetName, data := range datas {
    
    
		index, _ := file.NewSheet(sheetName)
		for i, row := range data {
    
    
			for j, val := range row {
    
    
				// 列行数字索引转excel坐标索引
				cellName, _ := excelize.CoordinatesToCellName(j+1, i+1)
				//fmt.Println("cellName:", cellName)
				// 设置,写入
				file.SetCellValue(sheetName, cellName, val)
			}
		}
		// 创建表格
		file.SetActiveSheet(index)
	}
	filename := "aaa" + ".xlsx"

	_, err := os.ReadDir("aaa/")
	if err != nil {
    
    
		// 不存在就创建
		err = os.MkdirAll("aaa/", fs.ModePerm)
		if err != nil {
    
    
			fmt.Println(err)
		}
	}
	// 删除默认工作表
	file.DeleteSheet("Sheet1")
	err = file.SaveAs("aaa/" + filename)
	if err != nil {
    
    
		return "", err
	}
	return filename, nil
}

func main() {
    
    
	datas := make(map[string][][]string)
	datas["xxx"] = [][]string{
    
    {
    
    "A1", "B1", "C1", "d1", "E1", "G1", "H1", "I1"}, {
    
    "A2", "B2", "C2"}, {
    
    "A3", "B3", "C3"}}
	datas["yyy"] = [][]string{
    
    {
    
    "X1", "Y1", "Z1"}, {
    
    "X2", "Y2", "Z2"}, {
    
    "X3", "Y3", "Z3"}}
	fileName, err := WriteExcel(datas)
	if err != nil {
    
    
		fmt.Println("Write excel error: ", err)
		return
	}

	fmt.Println("Write excel success, file name is: ", fileName)
  • 输出文件:
    在这里插入图片描述
    在这里插入图片描述

2.2 带数据校验的excel文件导出

  • 需求:入参是一个map,其键是每个sheet页的名字,其值是一个二维切片。在每个sheet页中依次写入切片中的数据。返回文件名和错误信息。
    数据校验:A列只能输入a,aa,aaa,aaaa;B列只能输入b,bb;C列只能输入c,cc,ccc。
  • 代码:
func WriteExcelValidation(datas map[string][][]string, validations map[string][]string) (string, error) {
    
    
	file := excelize.NewFile()

	for sheetName, data := range datas {
    
    

		// 创建sheet
		index, _ := file.NewSheet(sheetName)

		// 有效性校验
		for col, validation := range validations {
    
    
			dv := excelize.NewDataValidation(true)
			dv.SetSqref(col + "2:" + col + "1048576")
			err := dv.SetDropList(validation)
			if err != nil {
    
    
				return "", err
			}
			file.AddDataValidation(sheetName, dv)
		}

		for i, row := range data {
    
    
			for j, val := range row {
    
    
				// 列行数字索引转excel坐标索引
				cellName, _ := excelize.CoordinatesToCellName(j+1, i+1)
				// 设置,写入
				file.SetCellValue(sheetName, cellName, val)
			}
		}

		// 创建表格
		file.SetActiveSheet(index)
	}
	filename := "bbb" + ".xlsx"

	// 创建目录
	_, err := os.ReadDir("aaa/")
	if err != nil {
    
    
		// 不存在就创建
		err = os.MkdirAll("aaa/", fs.ModePerm)
		if err != nil {
    
    
			return "", err
		}
	}

	file.DeleteSheet("Sheet1")
	err = file.SaveAs("aaa/" + filename)
	if err != nil {
    
    
		return "", err
	}
	return filename, nil
}

func main() {
    
    
	validations := make(map[string][]string)
	validations["A"] = []string{
    
    "a", "aa", "aaa", "aaaa"}
	validations["B"] = []string{
    
    "b", "bb"}
	validations["E"] = []string{
    
    "c", "cc", "ccc"}

	datas := make(map[string][][]string)
	datas["xxx"] = [][]string{
    
    {
    
    "A1", "B1", "C1", "d1", "E1", "G1", "H1", "I1"}, {
    
    "A2", "B2", "C2"}, {
    
    "A3", "B3", "C3"}}
	datas["yyy"] = [][]string{
    
    {
    
    "X1", "Y1", "Z1"}, {
    
    "X2", "Y2", "Z2"}, {
    
    "X3", "Y3", "Z3"}}
	fileName, err := WriteExcelValidation(datas, validations)
	if err != nil {
    
    
		fmt.Println("Write excel error: ", err)
		return
	}

	fmt.Println("Write excel success, file name is: ", fileName)
}
  • 输出文件:
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_45859826/article/details/131330139