excel

package module
v0.0.0-...-bb63fea Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2020 License: BSD-2-Clause Imports: 13 Imported by: 0

README

# excel for golang

read and write excel files in golang.

go语言读写excel文件.

dependency

github.com/go-ole/go-ole

install

go get github.com/aswjh/excel

example

package main

import (
	"runtime"
	"fmt"
	"time"
	"github.com/aswjh/excel"
)

func main() {
	runtime.GOMAXPROCS(1)
	option := excel.Option{"Visible": true, "DisplayAlerts": true, "ScreenUpdating": true}
	xl, _ := excel.New(option)      //xl, _ := excel.Open("test_excel.xls", option)
	defer xl.Quit()

	sheet, _ := xl.Sheet(1)         //xl.Sheet("sheet1")
	defer sheet.Release()
	sheet.Cells(1, 1, "hello")
	sheet.PutCell(1, 2, 2006)
	sheet.MustCells(1, 3, 3.14159)

	cell := sheet.Cell(5, 6)
	defer cell.Release()
	cell.Put("go")
	cell.Put("font", map[string]interface{}{"name": "Arial", "size": 26, "bold": true})
	cell.Put("interior", "colorindex", 6)

	sheet.PutRange("a3:c3", []string {"@1", "@2", "@3"})
	rg := sheet.Range("d3:f3")
	defer rg.Release()
	rg.Put([]string {"~4", "~5", "~6"})

	urc := sheet.MustGet("UsedRange", "Rows", "Count").(int32)
	println("str:"+sheet.MustCells(1, 2), sheet.MustGetCell(1, 2).(float64), cell.MustGet().(string), urc)

	cnt := 0
	sheet.ReadRow("A", 1, "F", 9, func(row []interface{}) (rc int) {    //"A", 1 or 1, 9 or 1 or nothing
		cnt ++
		fmt.Println(cnt, row)
		return                                                                   //-1: break
	})

	time.Sleep(2000000000)

	//Sort
	cells := excel.GetIDispatch(sheet, "Cells")
	cells.CallMethod("UnMerge")
	sort := excel.GetIDispatch(sheet, "Sort")
	sortfields := excel.GetIDispatch(sort, "SortFields")
	sortfields.CallMethod("Clear")
	sortfields.CallMethod("Add", sheet.Range("f:f").IDispatch, 0, 2)
	sort.CallMethod("SetRange", cells)
	sort.PutProperty("Header", 1)
	sort.CallMethod("Apply")

	//Chart
	shapes := excel.GetIDispatch(sheet, "Shapes")
	_chart, _ := shapes.CallMethod("AddChart", 65)
	chart := _chart.ToIDispatch()
	chart.CallMethod("SetSourceData", sheet.Range("a1:c3").IDispatch)

	//AutoFilter
	cells.CallMethod("AutoFilter")
	excel.Release(sortfields, sort, cells, chart, shapes)

	time.Sleep(3000000000)
	xl.SaveAs("test_excel.xls")    //xl.SaveAs("test_excel", "html")
}

license

The BSD 3-Clause license

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColumnAtoi

func ColumnAtoi(s string) int

func ColumnItoa

func ColumnItoa(num int) (col string)

func DoFuncs

func DoFuncs(funcs ...interface{})

func Except

func Except(info string, err *error, funcs ...interface{})

func GetIDispatch

func GetIDispatch(_idisp interface{}, args ...string) (idisp *ole.IDispatch)

func GetProperty

func GetProperty(idisp *ole.IDispatch, args ...string) (ret interface{}, err error)

get Property as interface.

func MustGetProperty

func MustGetProperty(idisp *ole.IDispatch, args ...string) interface{}

get Property as interface.

func PutProperty

func PutProperty(idisp *ole.IDispatch, args ...interface{}) (err error)

put Property.

func Release

func Release(idisps ...*ole.IDispatch)

func RftCall

func RftCall(function reflect.Value, args ...reflect.Value) (err error)

func String

func String(val interface{}) (ret string)

func ToValueArray

func ToValueArray(sac *ole.SafeArrayConversion) (values [][]interface{})

from github.com/go-ole/go-ole/safearrayconversion.go:ToValueArray

Types

type Cell

type Cell struct {
	*ole.IDispatch
}

func (Cell) Get

func (cell Cell) Get(args ...string) (interface{}, error)

get Property as interface.

func (Cell) Gets

func (cell Cell) Gets(args ...string) (string, error)

get Property as string.

func (Cell) MustGet

func (cell Cell) MustGet(args ...string) interface{}

Must get Property as interface.

func (Cell) MustGets

func (cell Cell) MustGets(args ...string) (ret string)

Must get Property as string.

func (Cell) Put

func (cell Cell) Put(args ...interface{}) error

put cell Property.

type MSO

type MSO struct {
	Option
	IuApp       *ole.IUnknown
	IdExcel     *ole.IDispatch
	IdWorkBooks *ole.IDispatch
	WorkBook    WorkBook
	Version     float64
	FILEFORMAT  map[string]int
}

func Initialize

func Initialize(opt ...Option) (mso *MSO)

func New

func New(opt ...Option) (mso *MSO, err error)

func Open

func Open(full string, opt ...Option) (mso *MSO, err error)

func (*MSO) ActivateWorkBook

func (mso *MSO) ActivateWorkBook(id interface{}) (wb WorkBook, err error)

func (*MSO) ActiveWorkBook

func (mso *MSO) ActiveWorkBook() (WorkBook, error)

func (*MSO) AddSheet

func (mso *MSO) AddSheet(args ...string) (Sheet, error)

func (*MSO) AddWorkBook

func (mso *MSO) AddWorkBook() (WorkBook, error)

func (*MSO) CountSheets

func (mso *MSO) CountSheets() int

func (*MSO) CountWorkBooks

func (mso *MSO) CountWorkBooks() int

func (*MSO) OpenWorkBook

func (mso *MSO) OpenWorkBook(full string) (WorkBook, error)

func (*MSO) Pick

func (mso *MSO) Pick(workx string, id interface{}) (ret *ole.IDispatch, err error)

func (*MSO) Quit

func (mso *MSO) Quit() (err error)

func (*MSO) Save

func (mso *MSO) Save() []error

func (*MSO) SaveAs

func (mso *MSO) SaveAs(args ...interface{}) []error

func (*MSO) SelectSheet

func (mso *MSO) SelectSheet(id interface{}) (sheet Sheet, err error)

func (*MSO) SetOption

func (mso *MSO) SetOption(args ...interface{}) (err error)

func (*MSO) Sheet

func (mso *MSO) Sheet(id interface{}) (Sheet, error)

func (*MSO) Sheets

func (mso *MSO) Sheets() (sheets []Sheet)

func (*MSO) WorkBooks

func (mso *MSO) WorkBooks() (wbs WorkBooks)

type Option

type Option map[string]interface{}

type Range

type Range struct {
	*ole.IDispatch
}

func (Range) Get

func (rg Range) Get(args ...string) (interface{}, error)

get range Property as interface.

func (Range) MustGet

func (rg Range) MustGet(args ...string) interface{}

func (Range) Put

func (rg Range) Put(args ...interface{}) error

put range Property.

type Sheet

type Sheet struct {
	*ole.IDispatch
}

func (Sheet) Cell

func (sheet Sheet) Cell(r int, c int) Cell

get cell pointer.

func (Sheet) Cells

func (sheet Sheet) Cells(r int, c int, vals ...interface{}) (ret string, err error)

get cell Property as string, put cell Property.

func (Sheet) Delete

func (sheet Sheet) Delete() (err error)

func (Sheet) Get

func (sheet Sheet) Get(args ...string) (ret interface{}, err error)

get sheet Property

func (Sheet) GetCell

func (sheet Sheet) GetCell(r int, c int, args ...string) (ret interface{}, err error)

get cell Property as interface.

func (Sheet) GetRange

func (sheet Sheet) GetRange(rang string) (ret interface{}, err error)

get range Property as interface.

func (Sheet) MustCells

func (sheet Sheet) MustCells(r int, c int, vals ...interface{}) (ret string)

Must get cell Property as string, Must put cell Property.

func (Sheet) MustGet

func (sheet Sheet) MustGet(args ...string) interface{}

Must get sheet Property

func (Sheet) MustGetCell

func (sheet Sheet) MustGetCell(r int, c int, args ...string) interface{}

Must get cell Property as interface.

func (Sheet) MustGetRange

func (sheet Sheet) MustGetRange(rang string) interface{}

func (Sheet) Name

func (sheet Sheet) Name(args ...string) (name string)

func (Sheet) PutCell

func (sheet Sheet) PutCell(r int, c int, args ...interface{}) (err error)

put cell Property.

func (Sheet) PutRange

func (sheet Sheet) PutRange(rang string, args ...interface{}) (err error)

put range Property.

func (Sheet) Range

func (sheet Sheet) Range(rang string) Range

get range pointer.

func (Sheet) ReadRow

func (sheet Sheet) ReadRow(args ...interface{})

ReadRow("A", 1, "F", 9 or "A", 1 or 1, 9 or 1 or nothing, procfunc)

func (Sheet) Select

func (sheet Sheet) Select() (err error)

type VARIANT

type VARIANT struct {
	*ole.VARIANT
}

func (VARIANT) Value

func (va VARIANT) Value() (val interface{})

get val of MS VARIANT

type WorkBook

type WorkBook struct {
	*ole.IDispatch
	*MSO
}

func (WorkBook) Activate

func (wb WorkBook) Activate() (err error)

func (WorkBook) AddSheet

func (wb WorkBook) AddSheet(args ...string) (Sheet, error)

func (WorkBook) Close

func (wb WorkBook) Close() (err error)

func (WorkBook) Name

func (wb WorkBook) Name() string

func (WorkBook) Save

func (wb WorkBook) Save() (err error)

func (WorkBook) SaveAs

func (wb WorkBook) SaveAs(args ...interface{}) (err error)

type WorkBooks

type WorkBooks []WorkBook

func (WorkBooks) Close

func (wbs WorkBooks) Close() (errs []error)

func (WorkBooks) Save

func (wbs WorkBooks) Save() (errs []error)

func (WorkBooks) SaveAs

func (wbs WorkBooks) SaveAs(args ...interface{}) (errs []error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL