Files
zmkgC/third/excel/template.go
2025-07-07 20:11:59 +08:00

303 lines
10 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package excel
import (
"regexp"
"strconv"
"strings"
"github.com/xuri/excelize/v2"
)
var (
GlobalStyle int // 全局样式
GlobalStyleBold int // 基于GlobalStyle的 加粗
GlobalStyleBoldSize int // 基于GlobalStyle的 加粗 加字体大小
)
var Style = &excelize.Style{
// 边框
Border: []excelize.Border{
{Type: "left", Color: "000000", Style: 1},
{Type: "top", Color: "000000", Style: 1},
{Type: "bottom", Color: "000000", Style: 1},
{Type: "right", Color: "000000", Style: 1},
},
// 居中
Alignment: &excelize.Alignment{
Vertical: "center", // 上下居中
Horizontal: "center", // 左右居中
WrapText: true, // 自动换行
},
}
const (
excelName = "Book1.xlsx"
sheetName = "Sheet1"
)
func styleFunc(f *excelize.File) {
baseStyle := excelize.Style{
Border: []excelize.Border{
{Type: "left", Color: "000000", Style: 1},
{Type: "top", Color: "000000", Style: 1},
{Type: "bottom", Color: "000000", Style: 1},
{Type: "right", Color: "000000", Style: 1},
},
Alignment: &excelize.Alignment{
Vertical: "center",
Horizontal: "center",
WrapText: true,
},
}
font := excelize.Font{
Color: "#262626",
Bold: true,
}
// 创建全局样式
GlobalStyle, _ = f.NewStyle(&baseStyle)
// 创建加粗样式
baseStyle.Font = &font
GlobalStyleBold, _ = f.NewStyle(&baseStyle)
// 创建加粗加字体大小样式
font.Size = 12
baseStyle.Font = &font
GlobalStyleBoldSize, _ = f.NewStyle(&baseStyle)
}
// @Title cellMerge
// @Description 合并单元格
// @Author 铁憨憨[cory] 2024-10-18 14:20:40
// @Param f
// @Param qie 单元格切片
// @Param qie 影响行height高度相关
// @Param height 单元格高度(0隐藏当前行 -1取消自定义高度 其他单元格高度)
// @Param style 样式
// @Param str 单元格值
func cellMerge(f *excelize.File, qie []string, height float64, style int, str string) {
if len(qie) > 1 {
f.MergeCell(sheetName, qie[0], qie[1]) // 合并单元格
}
f.SetCellStyle(sheetName, qie[0], qie[len(qie)-1], style) // 设置样式
if height != -1 {
re := regexp.MustCompile("[0-9]+")
rowStr := strings.Join(re.FindAllString(qie[0], -1), "")
if rowNum, err := strconv.Atoi(rowStr); err == nil {
f.SetRowHeight(sheetName, rowNum, height) // 设置行高
}
}
f.SetCellValue(sheetName, qie[0], str) // 设置单元格的值
}
// 创建一个基础的 Excel 模板返回 *excelize.File
func CreateExcelTemplate() (*excelize.File, error) {
f := excelize.NewFile()
defer f.Close()
// 初始化样式
styleFunc(f)
// 初始化单元格布局
businessFunc(f)
index, err := f.NewSheet(sheetName)
if err != nil {
return nil, err
}
f.SetActiveSheet(index)
return f, nil
}
func businessFunc(f *excelize.File) {
// 一、
one := []string{"A1", "L1"}
cellMerge(f, one, 64, GlobalStyleBoldSize, "兴义市捧乍猪场坪农业光伏电站项目EPC总承包项目周报")
// 二、
two := []string{"A2", "B2"}
cellMerge(f, two, 30, GlobalStyle, "项目名称:")
two2 := []string{"C2", "G2"}
cellMerge(f, two2, -1, GlobalStyle, "兴义市捧乍猪场坪农业光伏电站项目EPC总承包项目")
two3 := []string{"H2"}
cellMerge(f, two3, -1, GlobalStyleBold, "周报期数")
two4 := []string{"I2"}
cellMerge(f, two4, -1, GlobalStyle, "53")
two5 := []string{"J2", "J3"}
cellMerge(f, two5, -1, GlobalStyle, "日期:")
two6 := []string{"K2", "L3"}
cellMerge(f, two6, -1, GlobalStyle, "2024.10.05-2024.10.11")
// 三、
three := []string{"A3"}
cellMerge(f, three, -1, GlobalStyle, "序号")
three2 := []string{"B3", "I3"}
cellMerge(f, three2, -1, GlobalStyle, "工程实施主要情况")
// 四、
four := []string{"A4"}
cellMerge(f, four, -1, GlobalStyle, "1")
four2 := []string{"B4"}
cellMerge(f, four2, -1, GlobalStyle, "项目基本信息")
four3 := []string{"C4"}
cellMerge(f, four3, -1, GlobalStyle, "装机容量:")
four4 := []string{"D4", "E4"}
cellMerge(f, four4, -1, GlobalStyle, "开工日期2022-08-15")
four5 := []string{"F4", "H4"}
cellMerge(f, four5, -1, GlobalStyle, "计划并网日期:")
four6 := []string{"I4"}
cellMerge(f, four6, -1, GlobalStyle, "总进度完成:")
four7 := []string{"J4", "L4"}
cellMerge(f, four7, -1, GlobalStyle, "96.5%")
// 五、
five := []string{"A5"}
cellMerge(f, five, -1, GlobalStyle, "2")
five2 := []string{"B5"}
cellMerge(f, five2, -1, GlobalStyle, "本周完成主要形象进度")
five3 := []string{"C5", "L5"}
cellMerge(f, five3, -1, GlobalStyle, "")
// 六、
six := []string{"A6"}
cellMerge(f, six, -1, GlobalStyle, "3")
six2 := []string{"B6"}
cellMerge(f, six2, -1, GlobalStyle, "下周计划主要形象进度")
six3 := []string{"C6", "L6"}
cellMerge(f, six3, -1, GlobalStyle, "")
// 七、
seven := []string{"A7"}
cellMerge(f, seven, -1, GlobalStyle, "4")
seven2 := []string{"B7"}
cellMerge(f, seven2, -1, GlobalStyle, "质量(施工质量、设备材料到货验收)情况")
seven3 := []string{"C7", "L7"}
cellMerge(f, seven3, -1, GlobalStyle, "")
// 八、
eight := []string{"A8"}
cellMerge(f, eight, -1, GlobalStyle, "5")
eight2 := []string{"B8"}
cellMerge(f, eight2, -1, GlobalStyle, "人员到位情况及施工器具")
eight3 := []string{"C8", "L8"}
cellMerge(f, eight3, -1, GlobalStyle, "")
// 九、
nine := []string{"A9", "A23"}
cellMerge(f, nine, -1, GlobalStyle, "6")
nine2 := []string{"B9", "B23"}
cellMerge(f, nine2, -1, GlobalStyle, "")
nine3 := []string{"C9"}
cellMerge(f, nine3, -1, GlobalStyle, "名称")
nine4 := []string{"D9"}
cellMerge(f, nine4, -1, GlobalStyle, "位置")
nine5 := []string{"E9"}
cellMerge(f, nine5, -1, GlobalStyle, "单位")
nine6 := []string{"F9"}
cellMerge(f, nine6, -1, GlobalStyle, "设计数量")
nine7 := []string{"G9"}
cellMerge(f, nine7, -1, GlobalStyle, "本周完成情况")
nine8 := []string{"H9"}
cellMerge(f, nine8, -1, GlobalStyle, "累计完成量")
nine9 := []string{"I9"}
cellMerge(f, nine9, -1, GlobalStyle, "累计完成率")
nine10 := []string{"J9", "K9"}
cellMerge(f, nine10, -1, GlobalStyle, "下周计划完成")
nine11 := []string{"L9"}
cellMerge(f, nine11, -1, GlobalStyle, "备注")
// 十
ten := []string{"B10", "B23"}
cellMerge(f, ten, -1, GlobalStyle, "施工进度(根据项目类型及进度情况自行分项,不用太细,能体现整体进度即可)")
ten2 := []string{"C10"}
cellMerge(f, ten2, -1, GlobalStyle, "基础")
ten3 := []string{"D10"}
cellMerge(f, ten3, -1, GlobalStyle, "光伏区")
ten4 := []string{"E10"}
cellMerge(f, ten4, -1, GlobalStyle, "个")
ten5 := []string{"F10"}
cellMerge(f, ten5, -1, GlobalStyle, "45450")
ten6 := []string{"G10"}
cellMerge(f, ten6, -1, GlobalStyle, "1415")
ten7 := []string{"H10"}
cellMerge(f, ten7, -1, GlobalStyle, "15445")
ten8 := []string{"I10"}
cellMerge(f, ten8, -1, GlobalStyle, "90")
ten9 := []string{"J10", "K10"}
cellMerge(f, ten9, -1, GlobalStyle, "545")
ten10 := []string{"L10"}
cellMerge(f, ten10, -1, GlobalStyle, "备注")
// 二十四、
twentyFour := []string{"A24", "A41"}
cellMerge(f, twentyFour, -1, GlobalStyle, "7")
twentyFour2 := []string{"B24", "B41"}
cellMerge(f, twentyFour2, -1, GlobalStyle, "主要设备材料到货情况(列出主要材料即可)")
twentyFour3 := []string{"C24"}
cellMerge(f, twentyFour3, -1, GlobalStyle, "设备材料名称")
twentyFour4 := []string{"D24"}
cellMerge(f, twentyFour4, -1, GlobalStyle, "")
twentyFour5 := []string{"E24"}
cellMerge(f, twentyFour5, -1, GlobalStyle, "单位")
twentyFour6 := []string{"F24"}
cellMerge(f, twentyFour6, -1, GlobalStyle, "设计数量")
twentyFour7 := []string{"G24"}
cellMerge(f, twentyFour7, -1, GlobalStyle, "本周到货量")
twentyFour8 := []string{"H24"}
cellMerge(f, twentyFour8, -1, GlobalStyle, "累计到货量")
twentyFour9 := []string{"I24"}
cellMerge(f, twentyFour9, -1, GlobalStyle, "累计到货率")
twentyFour10 := []string{"J24", "K24"}
cellMerge(f, twentyFour10, -1, GlobalStyle, "计划到货日期")
twentyFour11 := []string{"L24"}
cellMerge(f, twentyFour11, -1, GlobalStyle, "备注")
// 二十五
twentyFive2 := []string{"C25"}
cellMerge(f, twentyFive2, -1, GlobalStyle, "角钢")
twentyFive3 := []string{"D25"}
cellMerge(f, twentyFive3, -1, GlobalStyle, "")
twentyFive4 := []string{"E25"}
cellMerge(f, twentyFive4, -1, GlobalStyle, "")
twentyFive5 := []string{"F25"}
cellMerge(f, twentyFive5, -1, GlobalStyle, "")
twentyFive6 := []string{"G25"}
cellMerge(f, twentyFive6, -1, GlobalStyle, "")
twentyFive7 := []string{"H25"}
cellMerge(f, twentyFive7, -1, GlobalStyle, "")
twentyFive8 := []string{"I25"}
cellMerge(f, twentyFive8, -1, GlobalStyle, "90")
twentyFive9 := []string{"J25", "K25"}
cellMerge(f, twentyFive9, -1, GlobalStyle, "")
twentyFive10 := []string{"L25"}
cellMerge(f, twentyFive10, -1, GlobalStyle, "")
// 四十二
fortyTwo := []string{"A42"}
cellMerge(f, fortyTwo, -1, GlobalStyle, "8")
fortyTwo2 := []string{"B42"}
cellMerge(f, fortyTwo2, -1, GlobalStyle, "存在问题及需要协调的事项")
fortyTwo3 := []string{"C42", "L42"}
cellMerge(f, fortyTwo3, -1, GlobalStyle, "无")
// 四十三
fortyThree := []string{"A43"}
cellMerge(f, fortyThree, 90, GlobalStyle, "9")
fortyThree2 := []string{"B43"}
cellMerge(f, fortyThree2, -1, GlobalStyle, "照片")
fortyThree3 := []string{"C43", "E43"}
cellMerge(f, fortyThree3, -1, GlobalStyle, "")
fortyThree4 := []string{"F43", "H43"}
cellMerge(f, fortyThree4, -1, GlobalStyle, "")
fortyThree5 := []string{"I43", "L43"}
cellMerge(f, fortyThree5, -1, GlobalStyle, "")
// 四十四
fortyFour := []string{"A44"}
cellMerge(f, fortyFour, -1, GlobalStyle, "10")
fortyFour2 := []string{"B44"}
cellMerge(f, fortyFour2, -1, GlobalStyle, "填报:")
fortyFour3 := []string{"C44", "L44"}
cellMerge(f, fortyFour3, -1, GlobalStyleBold, "审核:")
// 整体样式,不需要动
f.SetColWidth(sheetName, "A", "A", 8)
f.SetColWidth(sheetName, "B", "B", 11)
f.SetColWidth(sheetName, "C", "C", 26)
f.SetColWidth(sheetName, "D", "D", 26)
f.SetColWidth(sheetName, "H", "H", 13)
f.SetCellStyle(sheetName, "A1", "B44", GlobalStyleBold) // 注意B44到时候需要取excel最大行数
}