excelize是Go语言编写的用于操作Office Excel文件基础库,可以使用它来读取,写入,支持XLSX/XLSM.XLTM等多种文档格式。
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows
PS C:\windows\system32> $env:GOPROXY = "https://proxy.golang.com.cn,direct"
PS C:\windows\system32> go get github.com/xuri/excelize
go: downloading github.com/xuri/excelize v1.4.1
go get: github.com/xuri/excelize@v1.4.1: parsing go.mod:
module declares its path as: github.com/360EntSecGroup-Skylar/excelize
but was required as: github.com/xuri/excelize
PS C:\windows\system32>
报错:
[Running] go run "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go\main.go"
main.go:6:2: no required module provides package github.com/xuri/excelize/v2: go.mod file not found in current directory or any parent directory; see 'go help modules'
[Done] exited with code=1 in 0.804 seconds
解决方法:
命令go env,检查GO111MODULE.
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go> go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\a-xiaobodou\AppData\Local\go-build
set GOENV=C:\Users\a-xiaobodou\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.17.7
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\A-XIAO~1\AppData\Local\Temp\go-build4105070101=/tmp/go-build -gno-record-gcc-switches
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go> go env -w GO111MODULE=auto
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go> go env
set GO111MODULE=auto
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\a-xiaobodou\AppData\Local\go-build
set GOENV=C:\Users\a-xiaobodou\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.17.7
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\A-XIAO~1\AppData\Local\Temp\go-build55044005=/tmp/go-build -gno-record-gcc-switches
PS C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go>
有报错:
[Running] go run "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go\main.go"
main.go:6:2: cannot find package "github.com/xuri/excelize/v2" in any of:
C:\Program Files\Go\src\github.com\xuri\excelize\v2 (from $GOROOT)
C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go\src\github.com\xuri\excelize\v2 (from $GOPATH)
[Done] exited with code=1 in 0.46 seconds
寻找excelize文件夹,在C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go\pkg\mod里把github.com复制,在C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go\src粘贴。还是不行。复印到C:\Program Files\Go\src粘贴之后。如下:
[Running] go run "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go\main.go"
C:\Program Files\Go\src\github.com\xuri\excelize\sheet.go:26:2: cannot find package "github.com/mohae/deepcopy" in any of:
C:\Program Files\Go\src\vendor\github.com\mohae\deepcopy (vendor tree)
C:\Program Files\Go\src\github.com\mohae\deepcopy (from $GOROOT)
C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go\src\github.com\mohae\deepcopy (from $GOPATH)
main.go:6:2: non-canonical import path "github.com/xuri/excelize/": should be "github.com/xuri/excelize"
[Done] exited with code=1 in 0.559 seconds
再次输入命令行:go get github.com\mohae\deepcopy。
再次出现报错:
main.go:6:2: cannot find package "github.com/xuri/excelize/v2" in any of:
C:\Program Files\Go\src\github.com\xuri\excelize\v2 (from $GOROOT)
C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go\src\github.com\xuri\excelize\v2 (from $GOPATH)
[Done] exited with code=1 in 2.521 seconds
把“/v2”删除以后,再次出现报错,如下:
[Running] go run "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go\main.go"
# command-line-arguments
.\main.go:16:14: f.Close undefined (type *excelize.File has no field or method Close)
.\main.go:21:12: assignment mismatch: 2 variables but f.GetCellValue returns 1 value
.\main.go:28:12: assignment mismatch: 2 variables but f.GetRows returns 1 value
[Done] exited with code=2 in 10.315 seconds
创建Excel 文件,打开表格写了内容,还是不行。
在pkg文件里删除两个文件go.mod和go.sum
[Running] go run "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go\main.go"
main.go:6:2: no required module provides package github.com/xuri/excelize/v2; to add it:
go get github.com/xuri/excelize/v2
[Done] exited with code=1 in 0.553 seconds
重新输入命令行:
PS C:\windows\system32> go get github.com/xuri/excelize/v2
go: downloading github.com/xuri/excelize/v2 v2.6.0
go: downloading github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
go: downloading github.com/richardlehane/mscfb v1.0.4
go: downloading github.com/xuri/efp v0.0.0-20220407160117-ad0f7a785be8
go: downloading github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22
go: downloading golang.org/x/crypto v0.0.0-20220408190544-5352b0902921
go: downloading golang.org/x/net v0.0.0-20220407224826-aac1ed45d8e3
go: downloading golang.org/x/text v0.3.7
go: downloading github.com/richardlehane/msoleps v1.0.1
go: downloading golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e
go get: added github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
go get: added github.com/richardlehane/mscfb v1.0.4
go get: added github.com/richardlehane/msoleps v1.0.1
go get: added github.com/xuri/efp v0.0.0-20220407160117-ad0f7a785be8
go get: added github.com/xuri/excelize/v2 v2.6.0
go get: added github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22
go get: upgraded golang.org/x/crypto v0.0.0-20211215165025-cf75a172585e => v0.0.0-20220408190544-5352b0902921
go get: upgraded golang.org/x/net v0.0.0-20220106012031-21a9c9cfe9c3 => v0.0.0-20220407224826-aac1ed45d8e3
go get: upgraded golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744 => v0.0.0-20211216021012-1d35b9e2eb4e
go get: upgraded golang.org/x/text v0.3.7-0.20210503195748-5c7c50ebbd4f => v0.3.7
PS C:\windows\system32>
最后运行成功了。
可是,修改Excel之后,出现运行失败。
[Running] go run "c:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go\main.go"
main.go:6:2: cannot find package "github.com/xuri/excelize/v2" in any of:
C:\Program Files\Go\src\github.com\xuri\excelize\v2 (from $GOROOT)
C:\Users\a-xiaobodou\OneDrive - Microsoft\Projects\Go\src\github.com\xuri\excelize\v2 (from $GOPATH)
[Done] exited with code=1 in 1.514 seconds