在Go语言中写入现有的Excel文件

huangapple go评论83阅读模式
英文:

Writing on existing Excel file in 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]

代码
这是我尝试使用的代码。

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()

}
英文:

There's any way to write on an existing Excel file in Go?, i've tried with some libraries that are basically to create the excel file from blank but i need to write on an existing file.

Any help is appreciated, Thanks.

UPDATE
Using the excel package @Marc recommended, i got this error:

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
This is the code i tried to use.

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()

}

答案1

得分: 1

你应该看一下这个,它是基于go的ole绑定构建的,可以在这里找到。

CellsWrite函数应该可以满足你的需求:

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

You should have a look at this package, which is built on top of the ole bindings for go that can be found here

The CellsWrite function should do what you want:

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

答案2

得分: 0

Excelize是一个纯Go语言编写的库,提供了一组函数,可以用于读写XLSX文件。

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

英文:

Excelize is a library written in pure Go and providing a set of functions that allow you to write to and read from XLSX files.

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

huangapple
  • 本文由 发表于 2014年8月26日 06:43:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/25495445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定