283 lines
7.4 KiB
Go
283 lines
7.4 KiB
Go
|
package test
|
|||
|
|
|||
|
import (
|
|||
|
"context"
|
|||
|
"fmt"
|
|||
|
"github.com/xuri/excelize/v2"
|
|||
|
"ifms/pkg/convert"
|
|||
|
"ifms/pkg/oss"
|
|||
|
"log"
|
|||
|
"os"
|
|||
|
"reflect"
|
|||
|
"testing"
|
|||
|
"time"
|
|||
|
)
|
|||
|
|
|||
|
func TestExcel(t *testing.T) {
|
|||
|
f := excelize.NewFile()
|
|||
|
defer func() {
|
|||
|
if err := f.Close(); err != nil {
|
|||
|
fmt.Println(err)
|
|||
|
}
|
|||
|
}()
|
|||
|
// Upload a new sheet.
|
|||
|
index, err := f.NewSheet("Sheet2")
|
|||
|
if err != nil {
|
|||
|
fmt.Println(err)
|
|||
|
return
|
|||
|
}
|
|||
|
// Set value of a cell.
|
|||
|
f.SetCellValue("Sheet2", "A2", "Hello world.")
|
|||
|
f.SetCellValue("Sheet1", "B2", 100)
|
|||
|
// Set active sheet of the workbook.
|
|||
|
f.SetActiveSheet(index)
|
|||
|
// Save spreadsheet by the given path.
|
|||
|
if err := f.SaveAs("Book1.xlsx"); err != nil {
|
|||
|
fmt.Println(err)
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
func TestExcel2(t *testing.T) {
|
|||
|
// 模拟查询出来的列表数据
|
|||
|
listData := [][]string{
|
|||
|
{"姓名", "年龄", "邮箱"}, // 表头
|
|||
|
{"张三", "25", "zhangsan@example.com"},
|
|||
|
{"李四", "30", "lisi@example.com"},
|
|||
|
{"王五", "28", "wangwu@example.com"},
|
|||
|
}
|
|||
|
|
|||
|
// 创建一个新的 Excel 文件
|
|||
|
f := excelize.NewFile()
|
|||
|
// 创建一个工作表
|
|||
|
sheetName := "Sheet1"
|
|||
|
index, _ := f.NewSheet(sheetName)
|
|||
|
f.SetActiveSheet(index)
|
|||
|
|
|||
|
// 将数据写入 Excel
|
|||
|
for rowIdx, rowData := range listData {
|
|||
|
for colIdx, cellValue := range rowData {
|
|||
|
// 将列索引转换为字母,例如 0 -> 'A', 1 -> 'B', ...
|
|||
|
colLetter := string(rune('A' + colIdx))
|
|||
|
// 设置单元格的值
|
|||
|
cellName := fmt.Sprintf("%s%d", colLetter, rowIdx+1)
|
|||
|
f.SetCellValue(sheetName, cellName, cellValue)
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// 保存 Excel 文件
|
|||
|
if err := f.SaveAs("output.xlsx"); err != nil {
|
|||
|
fmt.Println("Error saving Excel file:", err)
|
|||
|
}
|
|||
|
fmt.Println("Excel file created successfully.")
|
|||
|
}
|
|||
|
|
|||
|
// 定义一个结构体,并在字段中添加 excel 标签
|
|||
|
type Person struct {
|
|||
|
Name string `excel:"姓名"`
|
|||
|
Age int `excel:"年龄"`
|
|||
|
Email string `excel:"邮箱"`
|
|||
|
}
|
|||
|
|
|||
|
func TestExcel3(t *testing.T) {
|
|||
|
// 创建一个包含数据的结构体切片
|
|||
|
people := []Person{
|
|||
|
{"张三", 25, "zhangsan@example.com"},
|
|||
|
{"李四", 30, "lisi@example.com"},
|
|||
|
{"王五", 28, "wangwu@example.com"},
|
|||
|
}
|
|||
|
|
|||
|
// 创建一个新的 Excel 文件
|
|||
|
f := excelize.NewFile()
|
|||
|
sheetName := "Sheet1"
|
|||
|
index, _ := f.NewSheet(sheetName)
|
|||
|
f.SetActiveSheet(index)
|
|||
|
|
|||
|
// 获取结构体的类型
|
|||
|
structType := reflect.TypeOf(people[0])
|
|||
|
|
|||
|
// 写入表头
|
|||
|
for colIdx := 0; colIdx < structType.NumField(); colIdx++ {
|
|||
|
field := structType.Field(colIdx)
|
|||
|
colLetter := string(rune('A' + colIdx))
|
|||
|
err := f.SetCellValue(sheetName, fmt.Sprintf("%s1", colLetter), field.Tag.Get("excel"))
|
|||
|
if err != nil {
|
|||
|
fmt.Println("Error setting Excel header:", err)
|
|||
|
return
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// 写入数据行
|
|||
|
for rowIdx, person := range people {
|
|||
|
rowNum := rowIdx + 2 // 从第二行开始写入数据(第一行是表头)
|
|||
|
for colIdx := 0; colIdx < structType.NumField(); colIdx++ {
|
|||
|
colLetter := string(rune('A' + colIdx))
|
|||
|
fieldValue := reflect.ValueOf(person).Field(colIdx).Interface()
|
|||
|
err := f.SetCellValue(sheetName, fmt.Sprintf("%s%d", colLetter, rowNum), fieldValue)
|
|||
|
if err != nil {
|
|||
|
fmt.Println("Error setting Excel header:", err)
|
|||
|
return
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// 保存 Excel 文件
|
|||
|
if err := f.SaveAs("output_reflect.xlsx"); err != nil {
|
|||
|
fmt.Println("Error saving Excel file:", err)
|
|||
|
}
|
|||
|
fmt.Println("Excel file created successfully.")
|
|||
|
|
|||
|
ctx := context.Background()
|
|||
|
file, err := os.Open("/Users/shaolingjin/source/qk/ifms/test/output_reflect.xlsx")
|
|||
|
if err != nil {
|
|||
|
fmt.Println("Failed to open file:", err)
|
|||
|
return
|
|||
|
}
|
|||
|
defer file.Close()
|
|||
|
|
|||
|
//获取文件大小
|
|||
|
fileInfo, err := file.Stat()
|
|||
|
if err != nil {
|
|||
|
fmt.Println("Failed to get file size:", err)
|
|||
|
return
|
|||
|
}
|
|||
|
fileSize := fileInfo.Size()
|
|||
|
|
|||
|
name := oss.FormatObjectName("test", "output_reflect.xlsx")
|
|||
|
result, err := oss.Ins.PutObject(ctx, "qkkj-ifms-test", name, file, fileSize, oss.PutObjectOptions{
|
|||
|
ContentType: "text/plain",
|
|||
|
})
|
|||
|
if err != nil {
|
|||
|
fmt.Println("Failed to upload object:", err)
|
|||
|
return
|
|||
|
}
|
|||
|
|
|||
|
fmt.Println(result)
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
// 定义一个结构体,并在字段中添加 excel 和 excel_type 标签
|
|||
|
type P struct {
|
|||
|
Name string `excel:"姓名" excel_type:"string"`
|
|||
|
Age int `excel:"年龄" excel_type:"number"`
|
|||
|
Email string `excel:"邮箱" excel_type:"string"`
|
|||
|
Birth time.Time `excel:"出生日期" excel_type:"date"`
|
|||
|
}
|
|||
|
|
|||
|
func TestExcel4(t *testing.T) {
|
|||
|
// 创建一个包含数据的结构体切片
|
|||
|
people := []P{
|
|||
|
{"张三", 25, "zhangsan@example.com", time.Now()},
|
|||
|
{"李四", 30, "lisi@example.com", time.Now()},
|
|||
|
{"王五", 28, "wangwu@example.com", time.Now()},
|
|||
|
}
|
|||
|
|
|||
|
// 创建一个新的 Excel 文件
|
|||
|
f := excelize.NewFile()
|
|||
|
sheetName := "Sheet1"
|
|||
|
index, _ := f.NewSheet(sheetName)
|
|||
|
f.SetActiveSheet(index)
|
|||
|
|
|||
|
// 获取结构体的类型
|
|||
|
structType := reflect.TypeOf(people[0])
|
|||
|
|
|||
|
// 写入表头
|
|||
|
for colIdx := 0; colIdx < structType.NumField(); colIdx++ {
|
|||
|
field := structType.Field(colIdx)
|
|||
|
colLetter := string(rune('A' + colIdx))
|
|||
|
err := f.SetCellValue(sheetName, fmt.Sprintf("%s1", colLetter), field.Tag.Get("excel"))
|
|||
|
if err != nil {
|
|||
|
fmt.Println("Error setting Excel header:", err)
|
|||
|
return
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// 写入数据行
|
|||
|
for rowIdx, person := range people {
|
|||
|
rowNum := rowIdx + 2 // 从第二行开始写入数据(第一行是表头)
|
|||
|
for colIdx := 0; colIdx < structType.NumField(); colIdx++ {
|
|||
|
colLetter := string(rune('A' + colIdx))
|
|||
|
fieldValue := reflect.ValueOf(person).Field(colIdx).Interface()
|
|||
|
fieldType := structType.Field(colIdx).Tag.Get("excel_type")
|
|||
|
|
|||
|
f.SetCellValue(sheetName, fmt.Sprintf("%s%d", colLetter, rowNum), fieldValue)
|
|||
|
|
|||
|
// 根据 excel_type 标签设置单元格格式
|
|||
|
switch fieldType {
|
|||
|
case "number":
|
|||
|
f.SetCellInt(sheetName, fmt.Sprintf("%s%d", colLetter, rowNum), int64(fieldValue.(int)))
|
|||
|
case "date":
|
|||
|
|
|||
|
f.SetCellValue(sheetName, fmt.Sprintf("%s%d", colLetter, rowNum), fieldValue.(string))
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// 设置日期格式
|
|||
|
style, _ := f.NewStyle(&excelize.Style{
|
|||
|
NumFmt: 51,
|
|||
|
})
|
|||
|
for _, pe := range people {
|
|||
|
rowNum := pe.Age + 2
|
|||
|
colLetter := string(rune('D' + 0)) // 假设日期列是第四列(D)
|
|||
|
f.SetCellStyle(sheetName, fmt.Sprintf("%s%d", colLetter, rowNum), fmt.Sprintf("%s%d", colLetter, rowNum), style)
|
|||
|
}
|
|||
|
|
|||
|
// 保存 Excel 文件
|
|||
|
if err := f.SaveAs("output_123.xlsx"); err != nil {
|
|||
|
fmt.Println("Error saving Excel file:", err)
|
|||
|
}
|
|||
|
fmt.Println("Excel file created successfully.")
|
|||
|
}
|
|||
|
|
|||
|
func TestExcelInput(t *testing.T) {
|
|||
|
|
|||
|
// 打开 Excel 文件
|
|||
|
f, err := excelize.OpenFile("output_123.xlsx")
|
|||
|
if err != nil {
|
|||
|
log.Fatalf("打开 Excel 文件失败: %v", err)
|
|||
|
}
|
|||
|
|
|||
|
// 获取所有 sheet 名称
|
|||
|
sheetNames := f.GetSheetMap()
|
|||
|
for _, sheetName := range sheetNames {
|
|||
|
// 获取指定 sheet 的行数和列数
|
|||
|
rows, err := f.GetRows(sheetName)
|
|||
|
if err != nil {
|
|||
|
log.Fatalf("获取 sheet 数据失败: %v", err)
|
|||
|
}
|
|||
|
|
|||
|
// 跳过表头行
|
|||
|
if len(rows) > 0 {
|
|||
|
rows = rows[1:]
|
|||
|
}
|
|||
|
|
|||
|
// 遍历每一行数据
|
|||
|
for _, row := range rows {
|
|||
|
// 解析每一行数据到 Factory 结构体
|
|||
|
if row[0] == "" {
|
|||
|
continue
|
|||
|
}
|
|||
|
var p P
|
|||
|
|
|||
|
// 这里根据实际 Excel 表格的列顺序和数据类型进行解析
|
|||
|
// 假设 Excel 表格的第一列是 ID,第二列是 TenantId 等
|
|||
|
//if id, err := strconv.ParseInt(row[0], 10, 64); err == nil {
|
|||
|
// p.ID = id
|
|||
|
//}
|
|||
|
p.Name = row[0]
|
|||
|
p.Age = convert.ToInt(row[1])
|
|||
|
p.Email = row[2]
|
|||
|
|
|||
|
// 处理时间字段
|
|||
|
if dt, err := time.Parse(time.DateOnly, row[3]); err == nil {
|
|||
|
p.Birth = dt
|
|||
|
} else {
|
|||
|
println("时间解析失败", err)
|
|||
|
}
|
|||
|
// 打印解析后的数据
|
|||
|
fmt.Printf("P: %+v\n", p)
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|