Golang excel 패키지 내보내기 기능

1909 단어
package myutils

import (
	"feinail/of_capital/config"
	"fmt"
	"github.com/jinzhu/gorm"
	"github.com/tealeg/xlsx"
	"strconv"
	"time"
)

func ExcelExportData(db *gorm.DB, config config.Config, header []interface{}, data [][]interface{}, sheetName string) string {
	timeStr := time.Now().Format("2006-01-02 15:04:05")
	var file *xlsx.File
	var sheet *xlsx.Sheet
	var row *xlsx.Row
	var cell *xlsx.Cell

	style := &xlsx.Style{}
	style.Fill = *xlsx.NewFill("solid", "EFEFDE", "EFEFDE")
	style.Border = xlsx.Border{RightColor: "FF"}
	file = xlsx.NewFile()
	sheet, _ = file.AddSheet(sheetName)
	row = sheet.AddRow()
	for _, head := range header { //looping from 0 to the length of the array
		cell = row.AddCell()
		cell.Value = head.(string)
		cell.SetStyle(style)
	}
	for _, obj := range data {
		fmt.Println(obj)
		row = sheet.AddRow()
		for _, value := range obj {
			switch value.(type) {
			case string:
				value = value
				break
			case int:
				value = strconv.Itoa(value.(int))
				break
			case float64:
				value = strconv.FormatFloat(value.(float64), 'f', -1, 64)
				break
			}

			cell = row.AddCell()
			cell.Value = value.(string)
		}
	}
	url := config.ExcelPath + timeStr + ".xlsx"
	file.Save(url)
	return url
}

func To2Array(params []interface{}) [][]interface{} {
	var content_arr [][]interface{}
	for _, param := range params {
		var col []interface{}
		for _, p := range param.([]interface{}) {
			col = append(col, p.(interface{}))
		}
		content_arr = append(content_arr, col)
	}
	return content_arr
}






content := ReqParams["content"].([]interface{})
content_arr2 := myutils.To2Array(content)
rs := myutils.ExcelExportData(db, config, ReqParams["header"].([]interface{}), content_arr2, ReqParams["sheet_name"].(string))



좋은 웹페이지 즐겨찾기