一: go excel写操作
1.go使用gooxml编写excel:背景,字体,合并,边框,行列尺寸
1.安装goxml
go get baliance.com/gooxml/
2.使用参考:
gooxml 官网 :https://baliance.com/gooxml/
gooxml git :https://github.com/baliance/gooxml
2.示例
package main
import (
"log"
"fmt"
"encoding/json"
"baliance.com/gooxml/color"
"baliance.com/gooxml/schema/soo/sml"
"baliance.com/gooxml/spreadsheet"
"baliance.com/gooxml/measurement"
"github.com/Jeffail/gabs"
)
func JsonData()(datas *[]byte,err error){
var jsondata map[string]string = map[string]string{"1":"yi","2":"二",}
fmt.Println("jaon====",jsondata)
jsonresult ,err := json.Marshal(jsondata)
if err != nil {
fmt.Println("json fail")
}
return &jsonresult,nil
}
func main() {
//初始化表格
ss := spreadsheet.New()
sheet := ss.AddSheet()
//设置表格样式:列宽
// sheet.Column(1).SetWidth(36 * measurement.Point)
sheet.Column(1).SetWidth(36 * measurement.Point)
//设置字体
//字体设置
font := ss.StyleSheet.AddFont()
font.SetBold(true)
//设置背景颜色
green := ss.StyleSheet.AddDifferentialStyle()
green.Fill().SetPatternFill().SetBgColor(color.Lavender)
//添加行
row := sheet.AddRow()
//设置行高
row.SetHeight(30 * measurement.Point)
row.AddCell().SetString("第一行A1")
row = sheet.AddRow()
row.AddCell().SetString("第二行A2")
row = sheet.AddRow()
row.AddCell().SetString("第三行A3")
row.AddCell().SetString("第三行B3")
row.AddCell().SetString("第三行C3")
//合并单元格
sheet.AddMergedCells("A3", "B3")
centered := ss.StyleSheet.AddCellStyle()
//居中
centered.SetHorizontalAlignment(sml.ST_HorizontalAlignmentCenter)
centered.SetVerticalAlignment(sml.ST_VerticalAlignmentCenter)
//字体加粗
centered.SetFont(font)
sheet.Cell("A3").SetStyle(centered)
//添加背景颜色
ColorStr := "A3:C3"
{
cfmt := sheet.AddConditionalFormatting([]string{ColorStr})
r := cfmt.AddRule()
r.SetType(sml.ST_CfTypeCellIs)
// 单元格的值过滤条件表达式
r.SetOperator(sml.ST_ConditionalFormattingOperatorGreaterThan)
r.SetConditionValue("9")
//颜色green ,已定义好
r.SetStyle(green)
}
//gabs解析json数据
data,_ := JsonData()
jsonP,err:= gabs.ParseJSON(*data)
if err != nil {
fmt.Println(err)
}
fmt.Println(jsonP)
fmt.Println("++++",data)
//获取josn,增加单元格
//v1 := jsonP.S("v1").Data().(string)
//v2, err := jsonP.S("vv1").Children()
ChildrenOne:= jsonP.S("1").Data().(string)
row.AddCell().SetString(ChildrenOne)
//保存表格
if err := ss.Validate(); err != nil {
log.Fatalf("error validating sheet: %s", err)
}
ss.SaveToFile("jsonexcel.xlsx")
}
3.结果
本文含有隐藏内容,请 开通VIP 后查看