Code Monkey home page Code Monkey logo

excel's Introduction

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

excel's People

Contributors

aswjh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

excel's Issues

Go get error on linux system

user@debian:~$ go get -v github.com/aswjh/excel
github.com/aswjh/excel (download)
github.com/go-ole/go-ole (download)
github.com/go-ole/go-ole
github.com/go-ole/go-ole/oleutil
github.com/aswjh/excel
# github.com/aswjh/excel
go/src/github.com/aswjh/excel/excel.go:20:22: undefined: syscall.LoadDLL
go/src/github.com/aswjh/excel/excel.go:841:32: not enough arguments in call to convertHresultToError
go/src/github.com/aswjh/excel/excel.go:852:33: not enough arguments in call to convertHresultToError

system: debian 9

please use go-ole/go-ole

Hi it looks like mattn/go-ole is no more, and the import needs to be go-ole/go-ole, otherwise i get errors.

Add example to show how to read an excel file.

I am confused of using this lib to open a *.xls file, because it returns @Open: when I call the Open() func.

func process(file string) (err error) {
    option := excel.Option{"Visible": true, "DisplayAlerts": true}
    mso, err := excel.Open(file, option)
    if err != nil {
        log.Printf("open excel error: %v\n", err)
        return
    }
    fmt.Println(mso.Version)
    fmt.Println(mso.FILEFORMAT)
    return
}

Output is :

2016/10/09 09:51:15 open excel error: @Open:

So, any idea of adding some demo codes in README.md to show how to open an excel file?

Reading cell formatted as "Currency"

Hi hope all is well.
When i try to read a cell that is formatted as "Currency" i got nothing.

In the end i added this to the excel.String function (copied excel.String modfied the copy)

    case excel.VARIANT:

        if val.(excel.VARIANT).VT == ole.VT_CY {
            _val := *((*int64)(unsafe.Pointer(&val.(excel.VARIANT).Val)))
            ret = strconv.FormatFloat(float64(_val)*float64(0.0001), 'f', -1, 64)

        }

There is probably a better way of doing this. Just want to let you know.

panic before Quit: CoInitialize 尚未被呼叫

just run the sample code, with a simple xlsx

func main() {
    option := excel.Option{"Visible": true, "DisplayAlerts": true}
    //xl, _ := excel.New(option)      //
    xl, _ := excel.Open("old.xlsx", option)
    defer xl.Quit()

    sheet, _ := xl.Sheet(1)         //xl.Sheet("sheet1")
    sheet.Cells(1, 1, "hello")
    sheet.PutCell(1, 2, 2006)
    sheet.MustCells(1, 3, 3.14159)
    println("str:"+sheet.MustCells(1, 2), sheet.MustGetCell(1, 2).(float64))

    cell := sheet.MustCell(5, 6)
    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"})
    sheet.Range("d3:f3").Put([]string {"~4", "~5", "~6"})

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

old.xlsx

how can i read range values?

r := oleutil.MustGetProperty(sheet.Range("B3:D10").Idisp,"Value").ToArray()

//println(r)
//

for ka, va := range r.ToValueArray() {
	fmt.Printf("key[%s] value[%s]\n", ka, va)
}

got Nil values

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.