在Go中写入现有的Excel文件

时间:2014-08-25 22:43:32

标签: go

有没有办法在Go?中写一个现有的Excel文件,我尝试过一些库,这些库基本上是从空白创建excel文件,但我需要在现有文件上写。

感谢任何帮助,谢谢。

更新 使用@Marc推荐的excel包,我收到了这个错误:

panic: Ocurrió una excepción.

goroutine 16 [running]:
runtime.panic(0x4afb40, 0xc082000440)
    c:/go/src/pkg/runtime/panic.c:279 +0x11f
github.com/mattn/go-ole/oleutil.MustCallMethod(0x1cf688, 0x4eb870, 0x4, 0x2e3e38, 0x3, 0x3, 0xc082007080)
    C:/Go/path/src/github.com/mattn/go-ole/oleutil/oleutil.go:58 +0xdd
github.com/nivrrex/excel.(*Excel).Open(0x2e3f08, 0x4f3d70, 0x9, 0x0, 0x0, 0x0)
    C:/Go/path/src/github.com/nivrrex/excel/excel.go:65 +0x582
main.main()
    C:/Desarrollo/Projects/excel_writer_go/excel_writer.go:12 +0xa0

goroutine 19 [finalizer wait]:
runtime.park(0x414d40, 0x576c00, 0x575649)
    c:/go/src/pkg/runtime/proc.c:1369 +0xac
runtime.parkunlock(0x576c00, 0x575649)
    c:/go/src/pkg/runtime/proc.c:1385 +0x42
runfinq()
    c:/go/src/pkg/runtime/mgc0.c:2644 +0xdd
runtime.goexit()
    c:/go/src/pkg/runtime/proc.c:1445
exit status 2
[Finished in 4.4s with exit code 1]
[cmd: go build C:\Desarrollo\Projects\excel_writer_go\excel_writer.go & go run C:\Desarrollo\Projects\excel_writer_go\excel_writer.go]
[dir: C:\Desarrollo\Projects\excel_writer_go]
[path: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Go\bin]

CODE 这是我试图使用的代码。

package main

import (
    "fmt"
    "github.com/nivrrex/excel"
)

func main() {

    e := &excel.Excel{Visible: false, Readonly: false, Saved: true}
    filePath := "test.xlsx"
    e.Open(filePath)

    //Print
    fmt.Println(e.Cells(1, 1))
    e.Sheet(1)
    e.CellsWrite("Hello", 2, 2)
    e.Save()
    e.Close()

}

2 个答案:

答案 0 :(得分:1)

你应该看一下这个package,它是建立在可以找到的{@ 3}}之前的ole绑定之上的。

here函数应该执行您想要的操作:

func (this *Excel) CellsWrite(value string, row int, column int) (err error)

答案 1 :(得分:0)

Excelize是一个用纯Go语言编写的库,并提供了一组允许您写入和读取XLSX文件的功能。

https://github.com/360EntSecGroup-Skylar/excelize