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())
}
}