Skip to content

Commit

Permalink
Use excelize rather than tealeg/xlsx which is no longer maintained.
Browse files Browse the repository at this point in the history
  • Loading branch information
aotimme committed Dec 29, 2022
1 parent 1a26aef commit 5fa7352
Show file tree
Hide file tree
Showing 3 changed files with 1,042 additions and 38 deletions.
61 changes: 32 additions & 29 deletions cmd/xlsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"
"strings"

"github.com/tealeg/xlsx"
"github.com/xuri/excelize/v2"
)

type XlsxSubcommand struct {
Expand Down Expand Up @@ -56,71 +56,74 @@ func (sub *XlsxSubcommand) Run(args []string) {
}

func ConvertXlsxFull(filename, dirname string) {
xlsxFile, err := xlsx.OpenFile(filename)
f, err := excelize.OpenFile(filename)
if err != nil {
ExitWithError(err)
}
err = os.Mkdir(dirname, os.ModeDir|0755)
if err != nil {
ExitWithError(err)
}
for _, sheet := range xlsxFile.Sheets {
ConvertXlsxSheetToDirectory(dirname, sheet)
for _, sheetName := range f.GetSheetList() {
ConvertXlsxSheetToDirectory(f, dirname, sheetName)
}
}

func ConvertXlsxSheetToDirectory(dirname string, sheet *xlsx.Sheet) {
filename := fmt.Sprintf("%s/%s.csv", dirname, sheet.Name)
func ConvertXlsxSheetToDirectory(f *excelize.File, dirname string, sheetName string) {
filename := fmt.Sprintf("%s/%s.csv", dirname, sheetName)

file, err := os.Create(filename)
if err != nil {
ExitWithError(err)
}
defer file.Close()
outputCsv := NewOutputCsvFromFile(file)
WriteSheetToOutputCsv(sheet, outputCsv)
writeRowsToOutputCsv(outputCsv, f, sheetName)
}

func ConvertXlsxSheet(filename, sheetName string) {
xlsxFile, err := xlsx.OpenFile(filename)
f, err := excelize.OpenFile(filename)
if err != nil {
ExitWithError(err)
}

sheetNames := make([]string, len(xlsxFile.Sheets))
for i, sheet := range xlsxFile.Sheets {
sheetNames[i] = sheet.Name
}
sheetNames := f.GetSheetList()
// Use `GetIndexForColumn` so the sheet can be specified
// by name or by index.
sheetIndex := GetIndexForColumn(sheetNames, sheetName)
if sheetIndex == -1 {
ExitWithError(errors.New("Could not find sheet from sheet name"))
if sheetIndex == -1 {
ExitWithError(errors.New("Could not find sheet from sheet name"))
}
}

sheet := xlsxFile.Sheets[sheetIndex]
trueSheetName := sheetNames[sheetIndex]
outputCsv := NewOutputCsv()
WriteSheetToOutputCsv(sheet, outputCsv)
writeRowsToOutputCsv(outputCsv, f, trueSheetName)
}

func WriteSheetToOutputCsv(sheet *xlsx.Sheet, outputCsv *OutputCsv) {
for _, row := range sheet.Rows {
csvRow := make([]string, 0)
for _, cell := range row.Cells {
// We only care about the string value of the cell,
// so just ignore any error.
cellValue, _ := cell.FormattedValue()
csvRow = append(csvRow, cellValue)
func writeRowsToOutputCsv(outputCsv *OutputCsv, f *excelize.File, sheetName string) {
rows, err := f.Rows(sheetName)
if err != nil {
ExitWithError(err)
}
for rows.Next() {
row, err := rows.Columns()
if err != nil {
ExitWithError(err)
}
outputCsv.Write(csvRow)
outputCsv.Write(row)
}
if err = rows.Close(); err != nil {
ExitWithError(err)
}
}

func ListXlxsSheets(filename string) {
xlsxFile, err := xlsx.OpenFile(filename)
f, err := excelize.OpenFile(filename)
if err != nil {
ExitWithError(err)
}

for i, sheet := range xlsxFile.Sheets {
fmt.Printf("%d: %s\n", i+1, sheet.Name)
for i, sheetName := range f.GetSheetList() {
fmt.Printf("%d: %s\n", i+1, sheetName)
}
}
14 changes: 10 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ module github.com/aotimme/gocsv
go 1.14

require (
github.com/Masterminds/sprig/v3 v3.1.0
github.com/alphagov/router v0.0.0-20161125164013-5d98b0d9fc19
github.com/mattn/go-sqlite3 v1.10.0
github.com/tealeg/xlsx v1.0.5
github.com/Masterminds/sprig/v3 v3.2.3
github.com/alphagov/router v0.0.0-20221221092104-2672e1cfdb5e
github.com/huandu/xstrings v1.4.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/mattn/go-sqlite3 v1.14.16
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/xuri/excelize/v2 v2.6.1
golang.org/x/crypto v0.4.0 // indirect
)
Loading

0 comments on commit 5fa7352

Please sign in to comment.