search
HomeDatabaseMysql TutorialHow to combine Go with Gin to export Mysql data to Excel table

    1. To achieve the goal

    Golang uses excelize to export the table to the browser for downloading or save it locally.
    The subsequent import will also be written here

    2. The library used

    go get github.com/xuri/excelize/v2

    3. Project directory

    go-excel
    ├─ app
    │  ├─ excelize
    │  │  └─ excelize.go
    │  ├─ model
    │  │  └─ sysUser.go
    │  └─ service
    │     └─ userService.go
    ├─ common
    │  └─ mysql.go
    ├─ go.mod
    ├─ go.sum
    ├─ main.go
    └─ setting.json

    4. Main code writing


    viper is used to read the configuration file

    4.1. excelize.go(main Tools)

    The ExportExcelByStruct function was copied directly from the Internet. It took a while to study his writing method, so I also wrote it for everyone to learn

    import (
    	"fmt"
    	"math/rand"
    	"net/url"
    	"reflect"
    	"strconv"
    	"time"
    
    	"github.com/gin-gonic/gin"
    	"github.com/xuri/excelize/v2"
    )
    
    var (
    	defaultSheetName = "Sheet1" //默认Sheet名称
    	defaultHeight    = 25.0     //默认行高度
    )
    
    type lzExcelExport struct {
    	file      *excelize.File
    	sheetName string //可定义默认sheet名称
    }
    
    func NewMyExcel() *lzExcelExport {
    	return &lzExcelExport{file: createFile(), sheetName: defaultSheetName}
    }
    
    //导出基本的表格
    func (l *lzExcelExport) ExportToPath(params []map[string]string, data []map[string]interface{}, path string) (string, error) {
    	l.export(params, data)
    	name := createFileName()
    	filePath := path + "/" + name
    	err := l.file.SaveAs(filePath)
    	return filePath, err
    }
    
    //导出到浏览器。此处使用的gin框架 其他框架可自行修改ctx
    func (l *lzExcelExport) ExportToWeb(params []map[string]string, data []map[string]interface{}, c *gin.Context) {
    	l.export(params, data)
    	buffer, _ := l.file.WriteToBuffer()
    	//设置文件类型
    	c.Header("Content-Type", "application/vnd.ms-excel;charset=utf8")
    	//设置文件名称
    	c.Header("Content-Disposition", "attachment; filename="+url.QueryEscape(createFileName()))
    	_, _ = c.Writer.Write(buffer.Bytes())
    }
    
    //设置首行
    func (l *lzExcelExport) writeTop(params []map[string]string) {
    	topStyle, _ := l.file.NewStyle(`{"font":{"bold":true},"alignment":{"horizontal":"center","vertical":"center"}}`)
    	var word = 'A'
    	//首行写入
    	for _, conf := range params {
    		title := conf["title"]
    		width, _ := strconv.ParseFloat(conf["width"], 64)
    		line := fmt.Sprintf("%c1", word)
    		//设置标题
    		_ = l.file.SetCellValue(l.sheetName, line, title)
    		//列宽
    		_ = l.file.SetColWidth(l.sheetName, fmt.Sprintf("%c", word), fmt.Sprintf("%c", word), width)
    		//设置样式
    		_ = l.file.SetCellStyle(l.sheetName, line, line, topStyle)
    		word++
    	}
    }
    
    //写入数据
    func (l *lzExcelExport) writeData(params []map[string]string, data []map[string]interface{}) {
    	lineStyle, _ := l.file.NewStyle(`{"alignment":{"horizontal":"center","vertical":"center"}}`)
    	//数据写入
    	var j = 2 //数据开始行数
    	for i, val := range data {
    		//设置行高
    		_ = l.file.SetRowHeight(l.sheetName, i+1, defaultHeight)
    		//逐列写入
    		var word = 'A'
    		for _, conf := range params {
    			valKey := conf["key"]
    			line := fmt.Sprintf("%c%v", word, j)
    			isNum := conf["is_num"]
    
    			//设置值
    			if isNum != "0" {
    				valNum := fmt.Sprintf("'%v", val[valKey])
    				_ = l.file.SetCellValue(l.sheetName, line, valNum)
    			} else {
    				_ = l.file.SetCellValue(l.sheetName, line, val[valKey])
    			}
    
    			//设置样式
    			_ = l.file.SetCellStyle(l.sheetName, line, line, lineStyle)
    			word++
    		}
    		j++
    	}
    	//设置行高 尾行
    	_ = l.file.SetRowHeight(l.sheetName, len(data)+1, defaultHeight)
    }
    
    func (l *lzExcelExport) export(params []map[string]string, data []map[string]interface{}) {
    	l.writeTop(params)
    	l.writeData(params, data)
    }
    
    func createFile() *excelize.File {
    	f := excelize.NewFile()
    	// 创建一个默认工作表
    	sheetName := defaultSheetName
    	index := f.NewSheet(sheetName)
    	// 设置工作簿的默认工作表
    	f.SetActiveSheet(index)
    	return f
    }
    
    func createFileName() string {
    	name := time.Now().Format("2006-01-02-15-04-05")
    	rand.Seed(time.Now().UnixNano())
    	return fmt.Sprintf("excle-%v-%v.xlsx", name, rand.Int63n(time.Now().Unix()))
    }
    
    //excel导出(数据源为Struct) []interface{}
    func (l *lzExcelExport) ExportExcelByStruct(titleList []string, data []interface{}, fileName string, sheetName string, c *gin.Context) error {
    	l.file.SetSheetName("Sheet1", sheetName)
    	header := make([]string, 0)
    	for _, v := range titleList {
    		header = append(header, v)
    	}
    	rowStyleID, _ := l.file.NewStyle(`{"font":{"color":"#666666","size":13,"family":"arial"},"alignment":{"vertical":"center","horizontal":"center"}}`)
    	_ = l.file.SetSheetRow(sheetName, "A1", &header)
    	_ = l.file.SetRowHeight("Sheet1", 1, 30)
    	length := len(titleList)
    	headStyle := Letter(length)
    	var lastRow string
    	var widthRow string
    	for k, v := range headStyle {
    
    		if k == length-1 {
    
    			lastRow = fmt.Sprintf("%s1", v)
    			widthRow = v
    		}
    	}
    	if err := l.file.SetColWidth(sheetName, "A", widthRow, 30); err != nil {
    		fmt.Print("错误--", err.Error())
    	}
    	rowNum := 1
    	for _, v := range data {
    
    		t := reflect.TypeOf(v)
    		fmt.Print("--ttt--", t.NumField())
    		value := reflect.ValueOf(v)
    		row := make([]interface {
    		}, 0)
    		for l := 0; l < t.NumField(); l++ {
    
    			val := value.Field(l).Interface()
    			row = append(row, val)
    		}
    		rowNum++
    		err := l.file.SetSheetRow(sheetName, "A"+strconv.Itoa(rowNum), &row)
    		_ = l.file.SetCellStyle(sheetName, fmt.Sprintf("A%d", rowNum), fmt.Sprintf("%s", lastRow), rowStyleID)
    		if err != nil {
    			return err
    		}
    	}
    	disposition := fmt.Sprintf("attachment; filename=%s.xlsx", url.QueryEscape(fileName))
    	c.Writer.Header().Set("Content-Type", "application/octet-stream")
    	c.Writer.Header().Set("Content-Disposition", disposition)
    	c.Writer.Header().Set("Content-Transfer-Encoding", "binary")
    	c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Disposition")
    	return l.file.Write(c.Writer)
    }
    
    // Letter 遍历a-z
    func Letter(length int) []string {
    	var str []string
    	for i := 0; i < length; i++ {
    		str = append(str, string(rune(&#39;A&#39;+i)))
    	}
    	return str
    }

    4.2. userService.go (accepting requests)

    The exported functions have been tested and are ok. They can be used directly. Just change the data to your own.
    I also included the things to note. Written, lightning protection! !

    import (
    	"go-excel/app/excelize"
    	"go-excel/app/model"
    	config "go-excel/common"
    	"github.com/gin-gonic/gin"
    )
    
    
    
    //获取所有用户数据-excel
    func GetAllUserExportToWeb(ctx *gin.Context) {
    	var users []model.TUser
    	db := config.GetDB()
    	db.Find(&users)
    
    	//定义首行标题
    	dataKey := make([]map[string]string, 0)
    	dataKey = append(dataKey, map[string]string{
    		"key":    "id",
    		"title":  "索引",
    		"width":  "20",
    		"is_num": "0",
    	})
    	dataKey = append(dataKey, map[string]string{
    		"key":    "username",
    		"title":  "用户名",
    		"width":  "20",
    		"is_num": "0",
    	})
    	dataKey = append(dataKey, map[string]string{
    		"key":    "remark",
    		"title":  "备注",
    		"width":  "20",
    		"is_num": "0",
    	})
    
    	//填充数据
    	data := make([]map[string]interface{}, 0)
    	if len(users) > 0 {
    		for _, v := range users {
    			data = append(data, map[string]interface{}{
    				"id":       v.ID,
    				"username": v.Username,
    				"remark":   v.Remark,
    			})
    		}
    	}
    	ex := excelize.NewMyExcel()
      
    	// ex.ExportToWeb(dataKey, data, ctx)
    
    	//保存到D盘
    	ex.ExportToPath(dataKey, data, "D:/")
    }
    
    //excel 导出
    func GetUserExcelByMap(ctx *gin.Context) {
    	var users []model.TUser
    	db := config.GetDB()
    	db.Find(&users)
    
    	titles := []string{"ID", "用户名", "备注"}
    
    	ex := excelize.NewMyExcel()
    
    	var datas []interface{}
    	for _, v := range users {
    		//这里最好新建一个struct 和titles一致,不然users里面的多余的字段也会写进去
    		datas = append(datas, model.TUser{
    			ID:       v.ID,
    			Username: v.Username,
    			Remark:   v.Remark,
    		})
    	}
    	ex.ExportExcelByStruct(titles, datas, "用户数据", "用户", ctx)
    }

    4.2. Test results

    GetAllUserExportToWeb

    How to combine Go with Gin to export Mysql data to Excel table

    GetUserExcelByMap

    How to combine Go with Gin to export Mysql data to Excel table

    The above is the detailed content of How to combine Go with Gin to export Mysql data to Excel table. For more information, please follow other related articles on the PHP Chinese website!

    Statement
    This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
    Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

    InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

    MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

    Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

    Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

    MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

    MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

    MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

    MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

    MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

    MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

    MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

    The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

    SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

    MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

    The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

    Dreamweaver Mac version

    Dreamweaver Mac version

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    PhpStorm Mac version

    PhpStorm Mac version

    The latest (2018.2.1) professional PHP integrated development tool

    WebStorm Mac version

    WebStorm Mac version

    Useful JavaScript development tools