针对简单导出进行简化
parent
2e149ab7be
commit
c4fa0fad1e
|
@ -40,6 +40,7 @@ func (e *Exporter) SetCell(sheet string, cell Cell) {
|
||||||
BottomY: bottomy,
|
BottomY: bottomy,
|
||||||
IsMerge: start_col != end_col,
|
IsMerge: start_col != end_col,
|
||||||
}
|
}
|
||||||
|
// 记录当前单元格位置
|
||||||
e.preLocation = &CellLocation
|
e.preLocation = &CellLocation
|
||||||
if cell.Style != nil {
|
if cell.Style != nil {
|
||||||
style, _ := e.xlsx.NewStyle(cell.Style)
|
style, _ := e.xlsx.NewStyle(cell.Style)
|
||||||
|
|
|
@ -23,8 +23,7 @@ func (e *Exporter) SetGlobalTitleStyle(style *excelize.Style) {
|
||||||
e.GlobalTitleStyle = style
|
e.GlobalTitleStyle = style
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Exporter) SetTitle(sheet string) {
|
func (e *Exporter) WriteTitle(sheet string, title Title) {
|
||||||
for _, title := range e.Titles {
|
|
||||||
start_col, end_col := e.CaculateCell(title.Location, title.Rowspan, title.Colspan)
|
start_col, end_col := e.CaculateCell(title.Location, title.Rowspan, title.Colspan)
|
||||||
e.xlsx.MergeCell(sheet, start_col, end_col)
|
e.xlsx.MergeCell(sheet, start_col, end_col)
|
||||||
e.xlsx.SetCellValue(sheet, start_col, title.Name)
|
e.xlsx.SetCellValue(sheet, start_col, title.Name)
|
||||||
|
@ -39,4 +38,9 @@ func (e *Exporter) SetTitle(sheet string) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *Exporter) SetTitle(sheet string) {
|
||||||
|
for _, title := range e.Titles {
|
||||||
|
e.WriteTitle(sheet, title)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,11 +54,10 @@ func (e *Exporter) Export(sheetIndex int) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if len(e.Titles) == 0 {
|
|
||||||
return fmt.Errorf("excel title is null")
|
|
||||||
}
|
|
||||||
sheet := e.Sheets[sheetIndex]
|
sheet := e.Sheets[sheetIndex]
|
||||||
|
if len(e.Titles) > 0 {
|
||||||
e.SetTitle(sheet)
|
e.SetTitle(sheet)
|
||||||
|
}
|
||||||
v := reflect.ValueOf(e.Data)
|
v := reflect.ValueOf(e.Data)
|
||||||
if v.Kind() != reflect.Pointer {
|
if v.Kind() != reflect.Pointer {
|
||||||
return fmt.Errorf("data must be pointer")
|
return fmt.Errorf("data must be pointer")
|
||||||
|
@ -117,8 +116,19 @@ func (e *Exporter) writeCell(sheet string, field reflect.Value, tag string) {
|
||||||
colSpan, _ := strconv.Atoi(tagMap["colspan"])
|
colSpan, _ := strconv.Atoi(tagMap["colspan"])
|
||||||
rowSpan, _ := strconv.Atoi(tagMap["rowspan"])
|
rowSpan, _ := strconv.Atoi(tagMap["rowspan"])
|
||||||
loop := tagMap["loop"]
|
loop := tagMap["loop"]
|
||||||
|
title := tagMap["title"]
|
||||||
|
if title != "" {
|
||||||
|
t := Title{
|
||||||
|
Name: title,
|
||||||
|
Location: Location{X: x, Y: 1},
|
||||||
|
Colspan: colSpan,
|
||||||
|
Rowspan: rowSpan,
|
||||||
|
}
|
||||||
|
e.WriteTitle(sheet, t)
|
||||||
|
}
|
||||||
var location Location
|
var location Location
|
||||||
if e.preLocation != nil {
|
if e.preLocation != nil {
|
||||||
|
// loop不为空,则表示该结构体需要循环输出,x坐标需要重新计算(即x坐标需要在上一个结构体的右边,主要用于导出字段数量不确定,用sliece类型的结构体存储)
|
||||||
if loop != "" {
|
if loop != "" {
|
||||||
y = e.preLocation.BottomY
|
y = e.preLocation.BottomY
|
||||||
if x <= e.preLocation.RightX {
|
if x <= e.preLocation.RightX {
|
||||||
|
@ -127,7 +137,7 @@ func (e *Exporter) writeCell(sheet string, field reflect.Value, tag string) {
|
||||||
} else {
|
} else {
|
||||||
if x > e.preLocation.RightX {
|
if x > e.preLocation.RightX {
|
||||||
y = e.preLocation.TopY
|
y = e.preLocation.TopY
|
||||||
} else if x <= e.preLocation.RightX {
|
} else if x <= e.preLocation.RightX { // 如果x坐标小于等于上一个单元格的x坐标,则表示需要换行,此时y坐标需要在上一个单元格的下面
|
||||||
y = e.preLocation.BottomY + 1
|
y = e.preLocation.BottomY + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -153,10 +163,8 @@ func value(field reflect.Value) (val string) {
|
||||||
}
|
}
|
||||||
if mthV.IsValid() && mthV.Kind() == reflect.Func && mthV.Type().NumIn() == 0 {
|
if mthV.IsValid() && mthV.Kind() == reflect.Func && mthV.Type().NumIn() == 0 {
|
||||||
callV := mthV.Call(nil)
|
callV := mthV.Call(nil)
|
||||||
|
|
||||||
if len(callV) > 0 {
|
if len(callV) > 0 {
|
||||||
val = fmt.Sprintf("%s", callV[0].String())
|
val = fmt.Sprintf("%s", callV[0].String())
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -147,7 +147,7 @@ func TestMain(t *testing.T) {
|
||||||
exporter := export.DefaultExporter()
|
exporter := export.DefaultExporter()
|
||||||
exporter.Data = &data
|
exporter.Data = &data
|
||||||
exporter.Titles = title
|
exporter.Titles = title
|
||||||
exporter.File = "test.xlsx"
|
exporter.File = "test1.xlsx"
|
||||||
exporter.Path = "./"
|
exporter.Path = "./"
|
||||||
exporter.Export(0)
|
exporter.Export(0)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
package xlsx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.botann.com/lijun/xlsx/export"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Data struct {
|
||||||
|
Name string `export:"true,x:1,y:1,title:项目名称"`
|
||||||
|
TotalInvest int64 `export:"true,x:2,y:1,title:总投资(万元)"`
|
||||||
|
Unit string `export:"true,x:3,y:1,title:单位"`
|
||||||
|
Remark string `export:"true,x:4,y:1,title:备注"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMain(t *testing.T) {
|
||||||
|
data := []Data{
|
||||||
|
{
|
||||||
|
Name: "项目1",
|
||||||
|
TotalInvest: 100,
|
||||||
|
Unit: "单位1",
|
||||||
|
Remark: "备注1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "项目2",
|
||||||
|
TotalInvest: 200,
|
||||||
|
Unit: "单位2",
|
||||||
|
Remark: "备注2",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "项目3",
|
||||||
|
TotalInvest: 300,
|
||||||
|
Unit: "单位3",
|
||||||
|
Remark: "备注3",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
exporter := export.DefaultExporter()
|
||||||
|
exporter.Data = &data
|
||||||
|
exporter.File = "test6.xlsx"
|
||||||
|
exporter.Path = "./"
|
||||||
|
err := exporter.Export(0)
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue