增加导出数据到Excel的方法
This commit is contained in:
233
examples/excel_example.go
Normal file
233
examples/excel_example.go
Normal file
@@ -0,0 +1,233 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.toowon.com/jimmy/go-common/excel"
|
||||
"git.toowon.com/jimmy/go-common/factory"
|
||||
)
|
||||
|
||||
// User 用户结构体示例
|
||||
type User struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Status int `json:"status"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
// 创建工厂(可选,Excel导出不需要配置)
|
||||
fac, err := factory.NewFactoryFromFile("./config/example.json")
|
||||
if err != nil {
|
||||
// Excel导出不需要配置,可以传nil
|
||||
fac = factory.NewFactory(nil)
|
||||
}
|
||||
|
||||
// 示例1:导出结构体切片到文件
|
||||
fmt.Println("=== Example 1: Export Struct Slice to File ===")
|
||||
example1(fac)
|
||||
|
||||
// 示例2:导出到HTTP响应
|
||||
fmt.Println("\n=== Example 2: Export to HTTP Response ===")
|
||||
example2(fac)
|
||||
|
||||
// 示例3:使用格式化函数
|
||||
fmt.Println("\n=== Example 3: Export with Format Functions ===")
|
||||
example3(fac)
|
||||
|
||||
// 示例4:使用ExportData接口
|
||||
fmt.Println("\n=== Example 4: Export with ExportData Interface ===")
|
||||
example4(fac)
|
||||
}
|
||||
|
||||
// 示例1:导出结构体切片到文件
|
||||
func example1(fac *factory.Factory) {
|
||||
users := []User{
|
||||
{ID: 1, Name: "Alice", Email: "alice@example.com", CreatedAt: time.Now(), Status: 1},
|
||||
{ID: 2, Name: "Bob", Email: "bob@example.com", CreatedAt: time.Now().Add(-24 * time.Hour), Status: 1},
|
||||
{ID: 3, Name: "Charlie", Email: "charlie@example.com", CreatedAt: time.Now().Add(-48 * time.Hour), Status: 0},
|
||||
}
|
||||
|
||||
columns := []factory.ExportColumn{
|
||||
{Header: "ID", Field: "ID", Width: 10},
|
||||
{Header: "姓名", Field: "Name", Width: 20},
|
||||
{Header: "邮箱", Field: "Email", Width: 30},
|
||||
{Header: "创建时间", Field: "CreatedAt", Width: 20},
|
||||
{Header: "状态", Field: "Status", Width: 10},
|
||||
}
|
||||
|
||||
err := fac.ExportToExcelFile("users.xlsx", "用户列表", columns, users)
|
||||
if err != nil {
|
||||
log.Printf("Failed to export to file: %v", err)
|
||||
} else {
|
||||
fmt.Println("Excel file exported successfully: users.xlsx")
|
||||
}
|
||||
}
|
||||
|
||||
// 示例2:导出到HTTP响应
|
||||
func example2(fac *factory.Factory) {
|
||||
// 模拟HTTP响应
|
||||
w := &mockResponseWriter{}
|
||||
|
||||
users := []User{
|
||||
{ID: 1, Name: "Alice", Email: "alice@example.com", CreatedAt: time.Now(), Status: 1},
|
||||
{ID: 2, Name: "Bob", Email: "bob@example.com", CreatedAt: time.Now(), Status: 1},
|
||||
}
|
||||
|
||||
columns := []factory.ExportColumn{
|
||||
{Header: "ID", Field: "ID"},
|
||||
{Header: "姓名", Field: "Name"},
|
||||
{Header: "邮箱", Field: "Email"},
|
||||
}
|
||||
|
||||
// 设置HTTP响应头
|
||||
w.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
||||
w.Header().Set("Content-Disposition", "attachment; filename=users.xlsx")
|
||||
|
||||
err := fac.ExportToExcel(w, "用户列表", columns, users)
|
||||
if err != nil {
|
||||
log.Printf("Failed to export to HTTP response: %v", err)
|
||||
} else {
|
||||
fmt.Printf("Excel exported to HTTP response successfully, size: %d bytes\n", len(w.data))
|
||||
}
|
||||
}
|
||||
|
||||
// 示例3:使用格式化函数
|
||||
func example3(fac *factory.Factory) {
|
||||
users := []User{
|
||||
{ID: 1, Name: "Alice", Email: "alice@example.com", CreatedAt: time.Now(), Status: 1},
|
||||
{ID: 2, Name: "Bob", Email: "bob@example.com", CreatedAt: time.Now().Add(-24 * time.Hour), Status: 0},
|
||||
}
|
||||
|
||||
columns := []factory.ExportColumn{
|
||||
{Header: "ID", Field: "ID", Width: 10},
|
||||
{Header: "姓名", Field: "Name", Width: 20},
|
||||
{Header: "邮箱", Field: "Email", Width: 30},
|
||||
{
|
||||
Header: "创建时间",
|
||||
Field: "CreatedAt",
|
||||
Width: 20,
|
||||
Format: excel.FormatDateTimeDefault, // 使用便捷的格式化函数
|
||||
},
|
||||
{
|
||||
Header: "状态",
|
||||
Field: "Status",
|
||||
Width: 10,
|
||||
Format: func(value interface{}) string {
|
||||
// 自定义格式化函数
|
||||
if status, ok := value.(int); ok {
|
||||
if status == 1 {
|
||||
return "启用"
|
||||
}
|
||||
return "禁用"
|
||||
}
|
||||
return ""
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
err := fac.ExportToExcelFile("users_formatted.xlsx", "用户列表", columns, users)
|
||||
if err != nil {
|
||||
log.Printf("Failed to export with format: %v", err)
|
||||
} else {
|
||||
fmt.Println("Excel file exported with format successfully: users_formatted.xlsx")
|
||||
}
|
||||
}
|
||||
|
||||
// 示例4:使用ExportData接口
|
||||
func example4(fac *factory.Factory) {
|
||||
// 创建实现了ExportData接口的数据对象
|
||||
exportData := &UserExportData{
|
||||
users: []User{
|
||||
{ID: 1, Name: "Alice", Email: "alice@example.com", CreatedAt: time.Now(), Status: 1},
|
||||
{ID: 2, Name: "Bob", Email: "bob@example.com", CreatedAt: time.Now(), Status: 1},
|
||||
},
|
||||
}
|
||||
|
||||
// 使用接口方法获取列定义和数据
|
||||
columns := exportData.GetExportColumns()
|
||||
|
||||
err := fac.ExportToExcelFile("users_interface.xlsx", "用户列表", columns, exportData)
|
||||
if err != nil {
|
||||
log.Printf("Failed to export with interface: %v", err)
|
||||
} else {
|
||||
fmt.Println("Excel file exported with interface successfully: users_interface.xlsx")
|
||||
}
|
||||
}
|
||||
|
||||
// UserExportData 实现了ExportData接口的用户导出数据
|
||||
type UserExportData struct {
|
||||
users []User
|
||||
}
|
||||
|
||||
// GetExportColumns 获取导出列定义
|
||||
func (d *UserExportData) GetExportColumns() []excel.ExportColumn {
|
||||
return []excel.ExportColumn{
|
||||
{Header: "ID", Field: "ID", Width: 10},
|
||||
{Header: "姓名", Field: "Name", Width: 20},
|
||||
{Header: "邮箱", Field: "Email", Width: 30},
|
||||
{
|
||||
Header: "创建时间",
|
||||
Field: "CreatedAt",
|
||||
Width: 20,
|
||||
Format: excel.FormatDateTimeDefault,
|
||||
},
|
||||
{
|
||||
Header: "状态",
|
||||
Field: "Status",
|
||||
Width: 10,
|
||||
Format: func(value interface{}) string {
|
||||
if status, ok := value.(int); ok {
|
||||
if status == 1 {
|
||||
return "启用"
|
||||
}
|
||||
return "禁用"
|
||||
}
|
||||
return ""
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// GetExportRows 获取导出数据行
|
||||
func (d *UserExportData) GetExportRows() [][]interface{} {
|
||||
rows := make([][]interface{}, 0, len(d.users))
|
||||
for _, user := range d.users {
|
||||
row := []interface{}{
|
||||
user.ID,
|
||||
user.Name,
|
||||
user.Email,
|
||||
user.CreatedAt,
|
||||
user.Status,
|
||||
}
|
||||
rows = append(rows, row)
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// mockResponseWriter 模拟HTTP响应写入器(用于示例)
|
||||
type mockResponseWriter struct {
|
||||
header http.Header
|
||||
data []byte
|
||||
}
|
||||
|
||||
func (w *mockResponseWriter) Header() http.Header {
|
||||
if w.header == nil {
|
||||
w.header = make(http.Header)
|
||||
}
|
||||
return w.header
|
||||
}
|
||||
|
||||
func (w *mockResponseWriter) Write(data []byte) (int, error) {
|
||||
w.data = append(w.data, data...)
|
||||
return len(data), nil
|
||||
}
|
||||
|
||||
func (w *mockResponseWriter) WriteHeader(statusCode int) {
|
||||
// 模拟实现
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user