main
lj-wsdj 2024-05-22 15:32:09 +08:00
parent 58753da6f9
commit d862973c8d
4 changed files with 141 additions and 6 deletions

View File

@ -5,6 +5,7 @@ excel的导出导入目前仅完成导出
## 导出功能
如果要使用导出则传入的数据必须为指针类型可以是struct或者元素为struct的slice。
通过tag来确认需要导出的字段内容为`export:”x:1,y:1”`注意下x,y分别表是该字段在excel表格中的起始位置x为coly为row且tag中的x在数据中需要顺序列出。
通过tag来确认需要导出的字段内容为`export:”x:1,y:1”`注意下x,y分别表是该字段在excel表格中的起始位置x为coly为row且tag中的x,y在数据中需要顺序列出,优先y其次x。
当导出的excel表格列数不确定时tag使用`export:"loop:true"`这样会将所有字段导出到excel表格中。
详情请看测试文件
详情请看测试文件

View File

@ -116,12 +116,20 @@ func (e *Exporter) writeCell(sheet string, field reflect.Value, tag string) {
y, _ := strconv.Atoi(tagMap["y"])
colSpan, _ := strconv.Atoi(tagMap["colspan"])
rowSpan, _ := strconv.Atoi(tagMap["rowspan"])
loop := tagMap["loop"]
var location Location
if e.preLocation != nil {
if x > e.preLocation.RightX {
y = e.preLocation.TopY
} else if x <= e.preLocation.RightX {
y = e.preLocation.BottomY + 1
if loop != "" {
y = e.preLocation.BottomY
if x <= e.preLocation.RightX {
x = e.preLocation.RightX + 1
}
} else {
if x > e.preLocation.RightX {
y = e.preLocation.TopY
} else if x <= e.preLocation.RightX {
y = e.preLocation.BottomY + 1
}
}
}
location = Location{

126
export5_test.go 100644
View File

@ -0,0 +1,126 @@
package xlsx
import (
"testing"
"time"
"git.botann.com/lijun/xlsx/export"
)
type CategoryReport struct {
ProjectID int `json:"project_id"`
ProjectName string `json:"project_name" export:"x:1,y:3"`
ProjectUnit string `json:"project_unit" export:"x:2,y:3"`
Developer string `json:"developer" export:"x:3,y:3"`
Location string `json:"location" export:"x:4,y:3"`
ProductionTime time.Time `json:"production_time" export:"x:5,y:3"`
CompanyInvest int64 `json:"company_invest" export:"x:6,y:3"`
Business []BusinessIndex `json:"business"`
}
type BusinessIndex struct {
ProjectID int `json:"project_id"`
IndexId int `json:"index_id"`
Year int `json:"year"`
Name string `json:"name"`
Amount int64 `json:"amount" export:"x:7,y:3,loop:true"`
Unit string `json:"unit"`
}
type BusinessIndexTotal struct {
IndexId int `json:"index_id"`
Name string `json:"name"`
Amount int64 `json:"amount" export:"x:7,y:2,loop:true"`
}
type CategoryStatistics struct {
Info Info `json:"info"`
List []CategoryReport `json:"list"`
}
type Info struct {
Total []BusinessIndexTotal `json:"total"`
Title []BusinessIndex `json:"title"`
}
func TestMain(t *testing.T) {
title := []export.Title{
{
Name: "项目名称",
Location: export.Location{X: 1, Y: 1},
},
{
Name: "项目单位",
Location: export.Location{X: 2, Y: 1},
},
{
Name: "开发商",
Location: export.Location{X: 3, Y: 1},
},
{
Name: "位置",
Location: export.Location{X: 4, Y: 1},
},
{
Name: "生产时间",
Location: export.Location{X: 5, Y: 1},
},
{
Name: "公司投资",
Location: export.Location{X: 6, Y: 1},
},
{
Name: "生产规模",
Location: export.Location{X: 7, Y: 1},
},
{
Name: "权益规模",
Location: export.Location{X: 8, Y: 1},
},
{
Name: "总产量",
Location: export.Location{X: 9, Y: 1},
},
{
Name: "合计",
Location: export.Location{X: 1, Y: 2},
Colspan: 6,
},
}
total := []BusinessIndexTotal{
{IndexId: 1, Name: "生产规模", Amount: 100}, {IndexId: 2, Name: "权益规模", Amount: 200}, {IndexId: 3, Name: "总产量", Amount: 300},
}
report := []CategoryReport{
CategoryReport{
ProjectID: 1, ProjectName: "项目1", ProjectUnit: "单位1", Developer: "开发商1", Location: "位置1", ProductionTime: time.Now(), CompanyInvest: 1000,
Business: []BusinessIndex{
{IndexId: 1, Name: "生产规模", Amount: 10, Unit: "吨"}, {IndexId: 2, Name: "权益规模", Amount: 20, Unit: "吨"}, {IndexId: 3, Name: "总产量", Amount: 30, Unit: "吨"},
},
},
CategoryReport{
ProjectID: 2, ProjectName: "项目2", ProjectUnit: "单位2", Developer: "开发商2", Location: "位置2", ProductionTime: time.Now(), CompanyInvest: 2000,
Business: []BusinessIndex{
{IndexId: 1, Name: "生产规模", Amount: 20, Unit: "吨"}, {IndexId: 2, Name: "权益规模", Amount: 40, Unit: "吨"}, {IndexId: 3, Name: "总产量", Amount: 60, Unit: "吨"},
},
},
CategoryReport{
ProjectID: 3, ProjectName: "项目3", ProjectUnit: "单位3", Developer: "开发商3", Location: "位置3", ProductionTime: time.Now(), CompanyInvest: 3000,
Business: []BusinessIndex{
{IndexId: 1, Name: "生产规模", Amount: 30, Unit: "吨"}, {IndexId: 2, Name: "权益规模", Amount: 60, Unit: "吨"}, {IndexId: 3, Name: "总产量", Amount: 90, Unit: "吨"},
},
},
}
info := Info{
Total: total,
}
data := CategoryStatistics{
List: report,
Info: info,
}
exporter := export.DefaultExporter()
exporter.Titles = title
exporter.Data = &data
exporter.File = "test5.xlsx"
exporter.Path = "./"
exporter.Export(0)
}

BIN
test5.xlsx 100755

Binary file not shown.