使用 GoZero 实现读取绩效表格 Excel 并打分

以下是一个使用GoZero框架读取Excel并进行打分的简化示例。假设我们有一个Excel文件,其中第一列包含绩效数据,我们将根据这些数据给出打分。

首先,需要安装GoZero依赖:

go get -u github.com/tal-tech/go-zero/tools/goctl

然后,使用goctl生成代码:

goctl rpc proto -dir ./excelrpc -pkg excelrpc

接下来,编写服务逻辑:

package excelrpc

import (

"context"

"fmt"

"github.com/tal-tech/go-zero/core/logx"

"github.com/xuri/excelize/v2"

)

type ExcelRpcServer struct {}

func NewExcelRpcServer() *ExcelRpcServer {

return &ExcelRpcServer{}

}

func (s *ExcelRpcServer) ScoreExcel(ctx context.Context, in *ScoreExcelRequest) (*ScoreExcelResponse, error) {

// 打开Excel文件

f, err := excelize.OpenFile(in.GetExcelFile())

if err != nil {

logx.Errorf("open excel error: %v", err)

return nil, err

}

// 读取第一个工作表

sheet := f.GetSheetName(0)

rows, err := f.GetRows(sheet)

if err != nil {

logx.Errorf("get rows error: %v", err)

return nil, err

}

// 假设第一行是标题行,从第二行开始读取数据并打分

scores := make([]int32, 0)

for _, row := range rows[1:] {

// 假设第一列是绩效数据

performance := row[0]

score := s.score(performance)

scores = append(scores, score)

}

return &ScoreExcelResponse{Scores: scores}, nil

}

func (s *ExcelRpcServer) score(performance string) int32 {

// 这里是一个简化的打分逻辑,实际情况需要根据业务规则来定

if performance == "优秀" {

return 90

} else if performance == "合格" {

return 60

}

return 0

}

最后,定义RPC请求和响应的protobuf模型:

syntax = "proto3";

package excelrpc;

service ExcelRpc {

rpc ScoreExcel (ScoreExcelRequest) returns (ScoreExcelResponse);

}

message ScoreExcelRequest {

string excelFile = 1;

}

message ScoreExcelResponse {

repeated int32 scores = 1;

}

这个示例展示了如何使用GoZero框架和excelize库来读取Excel文件并进行简单的分数打分。在实际应用中,你需要根据自己的需求来调整打分逻辑和Excel文件的读取方式。

相关推荐
百事牛科技1 天前
解锁你的文档:Excel 打开密码取消教程
windows·excel
开开心心就好1 天前
体积小巧的图片重复查找工具推荐
linux·运维·服务器·智能手机·自动化·excel·fabric
九转成圣2 天前
Spring Boot 导出 Excel 最佳实践:从 POI 函数式封装到 EasyExcel 的“降维打击”
spring boot·后端·excel
开开心心_Every2 天前
扫描软件,部分文档文字表格识别功能可免费
运维·服务器·pdf·电脑·excel·3dsmax·houdini
星越华夏2 天前
Pandas实现excel的IF函数功能
excel·pandas
梅孔立4 天前
Java 基于 POI 模板 Excel 导出工具类 双数据源 + 自动合并单元格 + 自适应行高 完整实战
java·开发语言·excel
开开心心就好4 天前
系统重装前必备的智能驱动备份工具
windows·计算机视觉·计算机外设·excel·模块测试·csdn开发云·威胁分析
向宇it5 天前
php高性能的导出excel读写扩展——xlswriter,比传统的Spreadsheet要快很多
php·excel·xlswriter
yanweijie03175 天前
对比VLOOKUP、XLOOKUP、INDEX+MATCH三大查找函数
excel
Codiggerworld5 天前
Vim的语法:删除、复制、粘贴,像说话一样自然
编辑器·vim·excel