The premise that

There is no mature XLS framework for the current GO language. I tried many XLS frameworks, but they all had problems (incomplete reading or garbled characters). But you have to read the XLS table file. With no way and no time to study these frameworks, XLS was automatically converted to XLSX format in another language for reading operations. Python3 is used here.

1. Python3 environment

1.1 installation python3

Linux Installation Tutorial

Windows Installation Tutorial

Windows 64 – bit installation package download is slow, I play blue cloud

Python – 3.7.7 – amd64. Exe download: wws.lanzous.com/i68fAnhyz9i password: but3

1.2 Related Frameworks

Depending on your system environment variable Settings, use PIP or PIP3, and be sure to use the PIP corresponding to PYTHon3.

pip3 install pyexcel pyexcel-xls pyexcel-xlsx
Copy the code

2. Python code (XLS to XLSX)

Save the following code to the xls2xlsx.py file.

import pyexcel as p
import sys
xlsFile = sys.argv[1]
p.save_book_as(file_name=xlsFile,
               library='pyexcel-xls',
               skip_hidden_row_and_column=True,
               dest_file_name=xlsFile + "x")
Copy the code

3. Go call code

Check the file format in advance, not in the function.

// path: XLS file path
func Xls2Xlsx(path *string) error {
	// Delete an existing XLSX file with the same name
	if _, err := os.Stat(*path + "x"); err == nil {
		os.Remove(*path + "x")}// Execute commands (blocking calls)
	err := exec.Command("python3"."./xls2xlsx.py", *path).Run()
	iferr ! =nil {
		fmt.Println(err.Error())
		return err
	}
	// Check whether the XLSX file is successfully generated
	if _, err := os.Stat(*path + "x"); err == nil {
		*path = *path + "x"
	} else {
		return errors.New("Format conversion failed. Please manually convert to XLSX format.")}return nil
}
Copy the code

Next, with XLSX file, zao how to zao ~!

conclusion

Python3 environment installation and PIP download problems, very painful. Encounter problem much search, can not solve, can leave a message to ask me.