Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feat(office): Excel parse #51

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions basic/any.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package basic

type Any = interface{}
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@ module github.com/hnit-acm/hfunc

go 1.14

replace google.golang.org/grpc => google.golang.org/grpc v1.26.0

require (
github.com/360EntSecGroup-Skylar/excelize/v2 v2.3.2
github.com/boj/redistore v0.0.0-20180917114910-cd5dcc76aeff // indirect
github.com/gin-gonic/contrib v0.0.0-20201101042839-6a891bf89f19
github.com/gin-gonic/gin v1.6.3
github.com/google/go-cmp v0.3.0 // indirect
github.com/gorilla/sessions v1.2.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
golang.org/x/sync v0.0.0-20190423024810-112230192c58
google.golang.org/genproto v0.0.0-20191108220845-16a3f7862a1a // indirect
google.golang.org/grpc v1.35.0
)
127 changes: 0 additions & 127 deletions go.sum

This file was deleted.

72 changes: 72 additions & 0 deletions office/example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"fmt"
"github.com/hnit-acm/hfunc/office"
)

type ExcelOption struct {
Attr string
Value []string
}

func (e ExcelOption) NoEmptyHandler(excel *office.SheetFunc, placeholder office.PlaceholderIface, signal string) (string, error) {
excel.SetRowWithMerged(placeholder.CellVal(), e.Value)
return "", nil
}

func (e ExcelOption) EmptyHandler(excel *office.SheetFunc, placeholder office.PlaceholderIface, signal string) (string, error) {
return "空", nil
}

func main() {
excel, _ := office.OpenExcelFromFile("./test.xlsx")
sheet1 := excel.SelectSheet("Sheet1")
sheet1.SetRowWithMerged(office.UnitCell{
Col: "P",
Row: 1,
}, []interface{}{
"nieaowei", "123", "123wdas", "dsadqwewq", "dioasdoihasd",
})
sheet1.SetRowWithMerged(office.UnitCell{
Col: "P",
Row: 4,
}, []interface{}{
"nieaowei", "123", "123wdas", "dsadqwewq", "dioasdoihasd",
})
sheet1.SetRowWithMerged(office.UnitCell{
Col: "P",
Row: 7,
}, []interface{}{
"nieaowei", "123", "123wdas", "dsadqwewq", "dioasdoihasd",
})
sheet1.SetColWithMerged(office.UnitCell{
Col: "K",
Row: 1,
}, []interface{}{
"nieaowei", "123", "123wdas", "dsadqwewq", "dioasdoihasd",
})
sheet1.SetRowValueStartFrom(office.UnitCell{
Col: "L",
Row: 1,
}, [][]interface{}{
{"123", "123", "123"},
{"123", "123", "123"},
{"123", "123", "123"},
{"123", "123", "123"},
}, 3)
parseList, _ := sheet1.Parse()
for _, iface := range parseList {
sheet1.SetPlaceholder(iface, map[string]interface{}{
"nieaowei": "fyw是傻逼",
"123123": ExcelOption{
Attr: "123123",
Value: []string{"dsadsa", "1231313", "dsadqweqeasd"},
},
})
}
excel.SaveAs("./result.xlsx")
var e office.UnitCellIface = office.UnitCell{}
_, ok := e.(office.UnitCell)
fmt.Println(ok)
}
Binary file added office/example/result.xlsx
Binary file not shown.
Binary file added office/example/test.xlsx
Binary file not shown.
Binary file added office/example/test1.xlsx
Binary file not shown.
Binary file added office/example/~$result.xlsx
Binary file not shown.
Binary file added office/example/~$test1.xlsx
Binary file not shown.
35 changes: 35 additions & 0 deletions office/excel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package office

import (
"github.com/360EntSecGroup-Skylar/excelize/v2"
"io"
)

type Excel struct {
*excelize.File
currentSheet string
}

// SelectSheet 选择工作表
func (e *Excel) SelectSheet(sheetName ...string) *SheetFunc {
e.currentSheet = e.GetSheetName(0)
for _, s := range sheetName {
e.currentSheet = s
}
f := SheetFunc{
e,
}
return &f
}

// OpenExcelFromFile 从文件读取excel
func OpenExcelFromFile(filename string) (*Excel, error) {
f, err := excelize.OpenFile(filename)
return &Excel{File: f}, err
}

// OpenExcelFromReader 从读取流读取excel
func OpenExcelFromReader(reader io.Reader) (*Excel, error) {
f, err := excelize.OpenReader(reader)
return &Excel{File: f}, err
}
58 changes: 58 additions & 0 deletions office/excel_mergedCell.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package office

import "github.com/360EntSecGroup-Skylar/excelize/v2"

type MergedCellIface interface {
UnitCellIface
StartVal() UnitCellIface
EndVal() UnitCellIface
Val() interface{}
}

func IsMergedCell(cell UnitCellIface) (MergedCellIface, bool) {
val, ok := cell.(MergedCellIface)
return val, ok
}

type MergedCell struct {
Start UnitCellIface
End UnitCellIface
Value interface{}
}

func AxisToUnitCell(axis string) (UnitCell, error) {
col, row, err := excelize.SplitCellName(axis)
return UnitCell{
Col: col,
Row: row,
}, err
}

func (m MergedCell) StartVal() UnitCellIface {
return m.Start
}

func (m MergedCell) EndVal() UnitCellIface {
return m.End
}

func (m MergedCell) Val() interface{} {
return m.Value
}

func (m MergedCell) ColVal() string {
return m.StartVal().ColVal()
}

func (m MergedCell) RowVal() int {
return m.StartVal().RowVal()
}

//MergedIncludeUnit 该合并单元格是否包含某单元格
func MergedIncludeUnit(mergedCell MergedCellIface, cell UnitCellIface) bool {
if cell.ColVal() >= mergedCell.StartVal().ColVal() && cell.ColVal() <= mergedCell.EndVal().ColVal() &&
cell.RowVal() >= mergedCell.StartVal().RowVal() && cell.RowVal() <= mergedCell.EndVal().RowVal() {
return true
}
return false
}
Loading