89 lines
1.3 KiB
Go
89 lines
1.3 KiB
Go
package export
|
|
|
|
import (
|
|
"os"
|
|
"regexp"
|
|
"strconv"
|
|
)
|
|
|
|
type FileOpts struct {
|
|
fileName string
|
|
limit int
|
|
row int
|
|
}
|
|
|
|
func (f *FileOpts) slice() bool {
|
|
f.row++
|
|
if f.row > f.limit+1 { // +1 排除标题行
|
|
f.row = 0
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
type File struct {
|
|
FileOpts
|
|
|
|
param map[string]string
|
|
index int
|
|
}
|
|
|
|
func NewFile(name string, limit int, param map[string]string) *File {
|
|
return &File{
|
|
FileOpts: FileOpts{
|
|
fileName: name,
|
|
limit: limit,
|
|
},
|
|
param: param,
|
|
}
|
|
}
|
|
func (f *File) SetIndex(index int) *File {
|
|
f.index = index
|
|
return f
|
|
}
|
|
func (f *File) SetRow(row int) *File {
|
|
f.row = row
|
|
return f
|
|
}
|
|
|
|
func (f *File) NextFile() *File {
|
|
f.index++
|
|
return f
|
|
}
|
|
|
|
func (f *File) FileName() string {
|
|
m := regexp.MustCompile("({[a-zA-Z0-9]+})")
|
|
name := m.ReplaceAllFunc([]byte(f.fileName), func(b []byte) []byte {
|
|
field := string(b[1 : len(b)-1])
|
|
|
|
if val, ok := f.param[field]; ok {
|
|
return []byte(val)
|
|
}
|
|
return b
|
|
})
|
|
|
|
ex := regexp.MustCompile("(\\..*)")
|
|
name = ex.ReplaceAllFunc(name, func(b []byte) []byte {
|
|
i := []byte("_" + strconv.Itoa(f.index))
|
|
ret := make([]byte, len(b)+len(i))
|
|
copy(ret, i)
|
|
copy(ret[len(i):], b)
|
|
return ret
|
|
})
|
|
|
|
return string(name)
|
|
}
|
|
|
|
func (f *File) IsFileExist() bool {
|
|
_, err := os.Stat(f.FileName())
|
|
if err == nil {
|
|
return true
|
|
}
|
|
|
|
if os.IsNotExist(err) {
|
|
return false
|
|
}
|
|
|
|
return false
|
|
}
|