go一段代码读取Excel
时间: 2023-04-09 17:03:11 浏览: 58
好的,我可以回答这个问题。以下是一个使用 Go 读取 Excel 文件的示例代码:
```go
package main
import (
"fmt"
"github.com/tealeg/xlsx"
)
func main() {
// 打开 Excel 文件
xlFile, err := xlsx.OpenFile("example.xlsx")
if err != nil {
fmt.Println(err)
return
}
// 遍历所有工作表
for _, sheet := range xlFile.Sheets {
fmt.Printf("Sheet name: %s\n", sheet.Name)
// 遍历所有行
for _, row := range sheet.Rows {
// 遍历所有单元格
for _, cell := range row.Cells {
// 输出单元格的值
fmt.Printf("%s\t", cell.Value)
}
fmt.Println()
}
}
}
```
这个示例使用了第三方库 `github.com/tealeg/xlsx` 来读取 Excel 文件。你需要先使用 `go get` 命令安装这个库。在代码中,我们首先打开 Excel 文件,然后遍历所有工作表、所有行和所有单元格,输出它们的值。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)