【GO】excel文件处理

xlsx文件的读写,备用

package handler

import (
	"fmt"
	"strconv"

	"github.com/tealeg/xlsx"
)

type Ora struct {
	Type        string //类型
	Description string //描述
	Function    string //功能
	Parameter   string //参数
	LimitLow    string //下限

}

func ReadXlsx(filename string) map[string]Ora {
	results := make(map[string]Ora)

	xlFile, err := xlsx.OpenFile(filename)
	if err != nil {
		fmt.Printf("open failed: %s\n", err)
	}
	for _, sheet := range xlFile.Sheets {

		fmt.Printf("Sheet Name: %s\n", sheet.Name)

		for _, row := range sheet.Rows {
			tmpOra := Ora{}

			var strs []string

			for _, cell := range row.Cells {
				text := cell.String()
				strs = append(strs, text)
			}
			tmpOra.Type = strs[0]
			tmpOra.Description = strs[1]
			tmpOra.Function = strs[2]
			tmpOra.Parameter = strs[3]
			tmpOra.LimitLow = strs[4]

			results[strs[1]] = tmpOra
		}
	}
	return results
}

//备用
func WritingXlsx(oraList []Ora) {
	var file *xlsx.File
	var sheet *xlsx.Sheet
	var row *xlsx.Row
	var cell *xlsx.Cell
	var err error

	file = xlsx.NewFile()
	sheet, err = file.AddSheet("Sheet1")
	if err != nil {
		fmt.Printf(err.Error())
	}
	row = sheet.AddRow()
	row.SetHeightCM(0.5)
	cell = row.AddCell()
	cell.Value = "类型"
	cell = row.AddCell()
	cell.Value = "描述"
	cell = row.AddCell()
	cell.Value = "功能"
	cell = row.AddCell()
	cell.Value = "参数"
	cell = row.AddCell()
	cell.Value = "下限"

	for _, i := range oraList {
		if i.Type == "类型" {
			continue
		}

		var row1 *xlsx.Row

		row1 = sheet.AddRow()
		row1.SetHeightCM(0.5)

		cell = row1.AddCell()
		cell.Value = i.Type
		cell = row1.AddCell()
		cell.Value = i.Description
		cell = row1.AddCell()
		cell.Value = i.Function
		cell = row1.AddCell()
		cell.Value = i.Parameter

		// 判断值是大于0,大于变成黄色
		v1, _ := strconv.ParseFloat(i.LimitLow, 64)
		if v1 > 0 {
			cell = row1.AddCell()
			cell.Value = i.LimitLow
			cell.GetStyle().Font.Color = "FFCD7F32"

		} else {
			cell = row1.AddCell()
			cell.Value = i.LimitLow
			cell.GetStyle().Font.Color = xlsx.RGB_Dark_Red

			cell.GetStyle().Fill.FgColor = "FFFFFF00"
			cell.GetStyle().Fill.PatternType = "solid"

		}

	}

	err = file.Save("test3.xlsx")
	if err != nil {
		fmt.Printf(err.Error())
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值