Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to write an XLS file #81

Open
lixiaoxi-coder opened this issue Jul 16, 2021 · 4 comments
Open

How to write an XLS file #81

lixiaoxi-coder opened this issue Jul 16, 2021 · 4 comments

Comments

@lixiaoxi-coder
Copy link

We can use addSheet addRow to create a WorkBook, but how to write the content of a WorkBook to an XLS file?

@Shubhankar-Nath
Copy link

How did you solve this?
Have you tried using os.create an xls file and then to open it using xls.Open ?
That returns a workbook

@lixiaoxi-coder
Copy link
Author

How did you solve this? Have you tried using os.create an xls file and then to open it using xls.Open ? That returns a workbook

No, I have to use python's xls package with go-python.

@alok87
Copy link

alok87 commented Jan 3, 2023

@lixiaoxi-coder did you do it? #81 (comment) how was the experience? how fast or slow it is?

@shuiYe-704265085
Copy link

This library seems to only support reading xls files, so I used another library to write xls files (although the other library supports writing files, it does not support reading xls files), as shown in the following code


import (
	"fmt"
	"log"

	"github.com/extrame/xls"
	"github.com/tealeg/xlsx"
)

func main() {
	readXLS()
	// writeHandle()
}

func readXLS() {
	// 打开 XLS 文件
	xlsFile, err := xls.Open("test.xls", "utf-8")
	if err != nil {
		fmt.Println("无法打开 XLS 文件:", err)
		return
	}

	// 获取第一个工作表
	sheet := xlsFile.GetSheet(0)

	// 遍历每一行
	for i := 0; i <= int(sheet.MaxRow); i++ {
		row := sheet.Row(i)

		// 遍历每个单元格
		log.Printf("col:%d~%d\n", row.FirstCol(), row.LastCol())
		for i := row.FirstCol(); i < row.LastCol(); i++ {
			fmt.Printf("%v\t", row.Col(i))
		}

		fmt.Println() // 换行
	}
}

func writeHandle() {
	// 创建一个新的 XLS 文件
	file := xlsx.NewFile()

	// 创建一个工作表
	sheet, err := file.AddSheet("Sheet1")
	if err != nil {
		fmt.Println("无法创建工作表:", err)
		return
	}

	// 添加行和单元格
	row := sheet.AddRow()
	cell := row.AddCell()
	cell.Value = "Hello"

	// 保存文件
	err = file.Save("output.xls")
	if err != nil {
		fmt.Println("保存 XLS 文件失败:", err)
		return
	}

	fmt.Println("XLS 文件保存成功")

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants