读取excel文件代码:
import(
"fmt"
"github.com/tealeg/xlsx"
)
var (
inFile = "/Users/chain/Downloads/student1.xlsx"
)
func Import(){
// 打开文件
xlFile, err := xlsx.OpenFile(inFile)
if err != nil {
fmt.Println(err.Error())
return
}
// 遍历sheet页读取
for _, sheet := range xlFile.Sheets {
fmt.Println("sheet name: ", sheet.Name)
//遍历行读取
for _, row := range sheet.Rows {
// 遍历每行的列读取
for _, cell := range row.Cells {
text := cell.String()
fmt.Printf("%20s", text)
}
fmt.Print("\n")
}
}
fmt.Println("\n\nimport success")
}
测试代码:
import(
"testing"
)
func TestImport(t *testing.T){
Import()
}
Write
import(
"strconv"
"fmt"
"github.com/tealeg/xlsx"
)
var (
inFile = "/Users/chain/Downloads/student1.xlsx"
outFile = "/Users/chain/Downloads/out_student.xlsx"
)
type Student struct{
Name string
age int
Phone string
Gender string
Mail string
}
func Export(){
file := xlsx.NewFile()
sheet, err := file.AddSheet("student_list")
if err != nil {
fmt.Printf(err.Error())
}
stus := getStudents()
//add data
for _, stu := range stus{
row := sheet.AddRow()
nameCell := row.AddCell()
nameCell.Value = stu.Name
ageCell := row.AddCell()
ageCell.Value = strconv.Itoa(stu.age)
phoneCell := row.AddCell()
phoneCell.Value = stu.Phone
genderCell := row.AddCell()
genderCell.Value = stu.Gender
mailCell := row.AddCell()
mailCell.Value = stu.Mail
}
err = file.Save(outFile)
if err != nil {
fmt.Printf(err.Error())
}
fmt.Println("\n\nexport success")
}
func getStudents()[]Student{
students := make([]Student, 0)
for i := 0; i < 10; i++{
stu := Student{}
stu.Name = "name" + strconv.Itoa(i + 1)
stu.Mail = stu.Name + "@chairis.cn"
stu.Phone = "1380013800" + strconv.Itoa(i)
stu.age = 20
stu.Gender = "男"
students = append(students, stu)
}
return students
}
测试代码:
import(
"testing"
)
func TestExport(t *testing.T){
Export()
}
https://www.imooc.com/article/45864
操作Excelize
安装Excelize:
go get github.com/Luxurioust/excelize
创建XLSX文件:
package main
import (
"fmt"
"os"
"github.com/Luxurioust/excelize"
)
func main() {
xlsx, err := excelize.CreateFile()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
xlsx.NewSheet(2, "Sheet2")
xlsx.NewSheet(3, "Sheet3")
xlsx.SetCellInt("Sheet2", "A23", 10)
xlsx.SetCellStr("Sheet3", "B20", "Hello")
err = xlsx.WriteTo("/home/Workbook.xlsx")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
写XLSX文件:
package main
import (
"fmt"
"os"
"github.com/Luxurioust/excelize"
)
func main() {
xlsx, err := excelize.OpenFile("/home/Workbook.xlsx")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
xlsx.SetCellInt("Sheet2", "B2", 100)
xlsx.SetCellStr("Sheet2", "C11", "Hello")
xlsx.NewSheet(3, "TestSheet")
xlsx.SetCellInt("Sheet3", "A23", 10)
xlsx.SetCellStr("Sheet3", "b230", "World")
xlsx.SetActiveSheet(2)
err = xlsx.Save()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
读XLSX文件:
package main
import (
"fmt"
"os"
"github.com/Luxurioust/excelize"
)
func main() {
xlsx, err := excelize.OpenFile("/home/Workbook.xlsx")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
cell := xlsx.GetCellValue("Sheet2", "D11")
fmt.Println(cell)
}
http://blog.topspeedsnail.com/archives/9937
https://blog.csdn.net/wangshubo1989/article/details/78181493/
下载制定版本的第三方库
https://github.com/360EntSecGroup-Skylar/excelize/releases/tag/v1.4.1
直接下载源码压缩文件,解压后放入对应的目录
重启编译器
go语言读取excel数据
https://xuri.me/excelize/zh-hans/base/installation.html#read
go自带函数c.Ctx.Output.Download()下载xlsx模版,传入参数xlsx的路径,与模版名
// 下载账单
func (c *BillController) DownloadTemplate() {
olog.Debug("下载账单")
var input UploadTypeInput
r := otool.ParseRequestParams(&input, &c.Controller)
if r.IsOk() {
filePath := fmt.Sprintf("static/%s_bill_template.xlsx", input.ResourceType)
templateName := fmt.Sprintf("%s账单模板.xlsx", input.ResourceType)
c.Ctx.Output.Download(filePath, templateName)
}
}
非常重要的官方文档
https://xuri.me/excelize/zh-hans/base/installation.html