Documentation ¶
Index ¶
- Variables
- func ConvertNumberToFloat64(v interface{}) (float64, bool)
- func CopyCellStyle(from, to spreadsheet.Cell)
- func CopyRowStyle(from, to spreadsheet.Row)
- func GetCellString(c spreadsheet.Cell) string
- func GetSharedString(c spreadsheet.Cell, id int) (string, error)
- func ParseBool(v string, defaultValue bool) bool
- func ParseJavaTimeFormat(layout string) string
- func RowCells(r spreadsheet.Row) []spreadsheet.Cell
- type MergeColsMode
- type Option
- type OptionFn
- type PlaceholderPart
- type PlaceholderValue
- type Title
- type TitleField
- type WriteOption
- type WriteOptionFn
- type Xlsx
- func (x *Xlsx) Close() error
- func (x *Xlsx) Download(w http.ResponseWriter, filename string) error
- func (x *Xlsx) Read(slicePtr interface{}) error
- func (x *Xlsx) Save(w io.Writer) error
- func (x *Xlsx) SaveToFile(file string) error
- func (x *Xlsx) Write(beans interface{}, writeOptionFns ...WriteOptionFn) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrFailToLocationTitleRow = errors.New("unable to location title row") ErrNoExcelRead = errors.New("no excel read") )
var ErrUnknownExcelError = fmt.Errorf("unknown excel file format")
ErrUnknownExcelError defines the the unknown excel file format error.
Functions ¶
func ConvertNumberToFloat64 ¶
ConvertNumberToFloat64 converts a number value to float64. If the value is not a number, it returns 0, false.
func CopyCellStyle ¶
func CopyCellStyle(from, to spreadsheet.Cell)
func CopyRowStyle ¶
func CopyRowStyle(from, to spreadsheet.Row)
func GetCellString ¶
func GetCellString(c spreadsheet.Cell) string
GetCellString returns the string in a cell if it's an inline or string table string. Otherwise it returns an empty string.
func GetSharedString ¶
func GetSharedString(c spreadsheet.Cell, id int) (string, error)
GetSharedString retrieves a string from the shared strings table by index. nolint:goerr113
func ParseJavaTimeFormat ¶
ParseJavaTimeFormat converts the time format in java to golang.
func RowCells ¶
func RowCells(r spreadsheet.Row) []spreadsheet.Cell
RowCells returns a slice of cells. The cells can be manipulated, but appending to the slice will have no effect.
Types ¶
type MergeColsMode ¶
type MergeColsMode int
const ( // DoNotMerge do not mergeTitled. DoNotMerge MergeColsMode = iota // MergeCols mergeTitled columns separately. // like: // a, b, 1 // a, b, 2 // c, b, 3 // will merged to : // a, b, 1 // -, -, 2 // c, -, 3 MergeCols // MergeColsAlign mergeTitled columns align left merging. // like: // a, b, 1 // a, b, 2 // c, b, 3 // will merged to : // a, b, 1 // -, -, 2 // c, b, 3 MergeColsAlign )
type Option ¶
type Option struct {
TemplateWorkbook, Workbook *spreadsheet.Workbook
Validations map[string][]string
}
Option defines the option for the xlsx processing.
type OptionFn ¶
type OptionFn func(*Option)
OptionFn defines the func to change the option.
func WithExcel ¶
func WithExcel(excel interface{}) OptionFn
WithExcel defines the input excel file for reading. The excel can be type of any of followings: 1. a string for direct excel file name 2. a []byte for the content of excel which loaded in advance, like use packr2 to read. 3. a io.Reader.
func WithTemplate ¶
func WithTemplate(template interface{}) OptionFn
WithTemplate defines the template excel file for writing template. The template can be type of any of followings: 1. a string for direct template excel file name 2. a []byte for the content of template excel which loaded in advance, like use packr2 to read. 3. a io.Reader.
Example ¶
package main import ( "fmt" "github.com/io1o/xlsx" ) func main() { x, _ := xlsx.New(xlsx.WithTemplate("testdata/template.xlsx")) defer x.Close() _ = x.Write([]memberStat{ {Total: 100, New: 50, Effective: 50}, {Total: 200, New: 60, Effective: 140}, }) err := x.SaveToFile("testdata/out_demo2.xlsx") fmt.Println("Write", err == nil) } type memberStat struct { Total int `title:"会员总数" sheet:"会员"` New int `title:"其中:新增"` Effective int `title:"其中:有效"` }
Output: Write true
func WithUpload ¶
WithUpload defines the input excel file for reading.
func WithValidations ¶
WithValidations defines the validations for the cells.
type PlaceholderPart ¶
PlaceholderPart is a placeholder sub Part after parsing.
type PlaceholderValue ¶
type PlaceholderValue struct { Content string Parts []PlaceholderPart }
PlaceholderValue represents a placeholder value.
func ParsePlaceholder ¶
func ParsePlaceholder(content string) PlaceholderValue
ParsePlaceholder parses placeholders in the content.
func (*PlaceholderValue) HasPlaceholders ¶
func (p *PlaceholderValue) HasPlaceholders() bool
HasPlaceholders tells that the PlaceholderValue has any placeholders.
func (*PlaceholderValue) Interpolate ¶
func (p *PlaceholderValue) Interpolate(vars map[string]string) string
Interpolate interpolates placeholders with vars.
type Title ¶
type TitleField ¶
type TitleField struct { Column string Title Title StructField reflect.StructField }
type WriteOption ¶
type WriteOption struct { SheetName string MergeColsMode MergeColsMode }
type WriteOptionFn ¶
type WriteOptionFn func(*WriteOption)
func WithMergeColsMode ¶
func WithMergeColsMode(v MergeColsMode) WriteOptionFn
func WithSheetName ¶
func WithSheetName(v string) WriteOptionFn
type Xlsx ¶
type Xlsx struct {
// contains filtered or unexported fields
}
Xlsx is the structure for xlsx processing.
Example ¶
package main import ( "encoding/json" "fmt" "io/ioutil" "time" "github.com/io1o/xlsx" ) // ReadBytes reads bytes from the file. func ReadBytes(filename string) []byte { b, _ := ioutil.ReadFile(filename) return b } func main() { type ( HostInfo struct { ServerName string `title:"主机名称" json:"serverName"` ServerHostname string `title:"主机hostname" json:"serverHostname"` ServerIP string `title:"主机IP" json:"serverIp"` ServerUserRtx string `json:"serverUserRtx"` Status string `json:"status"` // 状态:0正常 1删除 InstanceID string `title:"实例ID" json:"instanceId"` Region string `title:"服务器可用区" json:"region"` CreateTime time.Time `json:"createTime"` // 创建时间 UpdateTime time.Time `json:"updateTime"` // 修改时间 ServerUserFullName string `title:"主机负责人(rtx)" json:"serverUserFullName"` } Rsp struct { Status int `json:"status"` Message string `json:"message"` Data []HostInfo `json:"data"` } ) var r Rsp err := json.Unmarshal(ReadBytes("testdata/hostinfos.json"), &r) fmt.Println("Unmarshal", err == nil) x, _ := xlsx.New(xlsx.WithTemplate("testdata/hostinfos_template.xlsx")) defer x.Close() err = x.Write(r.Data, xlsx.WithSheetName("FirstSheet")) fmt.Println("Write", err == nil) r.Data[0].ServerName += "第2页啦" _ = x.Write(r.Data, xlsx.WithSheetName("SecondSheet")) err = x.SaveToFile("testdata/out_hostinfos.xlsx") fmt.Println("SaveToFile", err == nil) }
Output: Unmarshal true Write true SaveToFile true
func New ¶
New creates a new instance of Xlsx.
Example ¶
package main import ( "fmt" "github.com/io1o/xlsx" ) func main() { x, _ := xlsx.New() defer x.Close() _ = x.Write([]memberStat{ {Total: 100, New: 50, Effective: 50}, {Total: 200, New: 60, Effective: 140}, }) err := x.SaveToFile("testdata/out_demo1.xlsx") // See: https://golang.org/pkg/testing/#hdr-Examples fmt.Println("Write", err == nil) } type memberStat struct { Total int `title:"会员总数" sheet:"会员"` New int `title:"其中:新增"` Effective int `title:"其中:有效"` }
Output: Write true
func (*Xlsx) Download ¶
func (x *Xlsx) Download(w http.ResponseWriter, filename string) error
Download downloads the excels file in the http response.
func (*Xlsx) SaveToFile ¶
SaveToFile writes the workbook out to a file.
func (*Xlsx) Write ¶
func (x *Xlsx) Write(beans interface{}, writeOptionFns ...WriteOptionFn) error
Write Writes beans to the underlying xlsx.