You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
2.6 KiB

4 years ago
package main
import (
"github.com/360EntSecGroup-Skylar/excelize"
"github.com/golang/glog"
"gopkg.in/yaml.v2"
"io/ioutil"
"path/filepath"
)
func main() {
glog.Info("开始-读取配置文件...")
config := initFunc()
if config == nil {
return
}
pathFile := config.Book
sheetName := config.Sheet
glog.Info("打开文件文件:", pathFile)
xlsx, err := excelize.OpenFile(pathFile)
if err != nil {
glog.Error("打开文件失败!")
return
}
glog.Info("开始-读取文件内容获取异常单元格列表...")
cells, err := readExcel(xlsx, sheetName)
if err != nil {
return
}
for _, s := range cells {
glog.Info("xxx:", s)
}
//glog.Info("开始-为异常单元格设置指定样式...")
//makeExcelStyle(xlsx, sheetName, cells)
//
//// Save xlsx file by the given path.
//err = xlsx.SaveAs(pathFile)
//if err != nil {
// glog.Error("保存文件失败!")
// return
//}
glog.Info("执行完成...")
}
/**
readExcel 读取Excel实现对比操作完成统计异常单元格列表
*/
4 years ago
func readExcel(xlsx *excelize.File, sheetName string) (map[int]string, error) {
cells, ii := map[int]string{}, 0
glog.Info(xlsx.GetCellValue(sheetName, "A1"))
4 years ago
rows := xlsx.GetRows(sheetName)
for _, row := range rows {
for _, colCell := range row {
if !(len(colCell) > 20) {
cells[ii] = colCell
ii++
}
}
}
return cells, nil
}
/**
makeExcelStyle 设置单元格样式
*/
4 years ago
func makeExcelStyle(xlsx *excelize.File, sheetName string, cells map[int]string) {
xlsx.GetSheetIndex(sheetName)
style, _ := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["#FF0000"],"pattern":1}}`)
for _, s := range cells {
xlsx.SetCellStyle(sheetName, s, s, style)
}
}
/**
initFunc 执行读取 yaml 配置文件
*/
4 years ago
func initFunc() *Config {
path, _ := filepath.Abs("./")
path += "\\config.yaml"
glog.Info("读取配置文件:", path)
path = "D:\\ViviCode\\golangs_learn\\golang_learn\\data_office\\get_excel_velidation\\config.yaml"
4 years ago
load, err := load(path)
if err != nil {
glog.Error("读取文件发生异常...")
return nil
}
glog.Info("读取配置文件内容:", load)
return load
}
type Config struct {
Book string `yaml:"文件"`
Sheet string `yaml:"页签"`
RowNum string `yaml:"开始行"`
4 years ago
CellNum []string `yaml:"开始列"`
}
/**
load 解析 Yaml 文件
*/
4 years ago
func load(path string) (*Config, error) {
conf := new(Config)
yamlFile, err := ioutil.ReadFile(path)
if err != nil {
glog.Info("发生异常:", err)
return nil, err
}
err = yaml.Unmarshal(yamlFile, conf)
if err != nil {
glog.Error("Unmarshal:", err)
return nil, err
}
return conf, err
}