initial commit
This commit is contained in:
parent
c268475c9d
commit
35394b056d
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
*.exe
|
5
go.mod
Normal file
5
go.mod
Normal file
@ -0,0 +1,5 @@
|
||||
module code.stevenpolley.net/steven/csv2xls
|
||||
|
||||
go 1.22.6
|
||||
|
||||
require github.com/tealeg/xlsx v1.0.5
|
9
go.sum
Normal file
9
go.sum
Normal file
@ -0,0 +1,9 @@
|
||||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/tealeg/xlsx v1.0.5 h1:+f8oFmvY8Gw1iUXzPk+kz+4GpbDZPK1FhPiQRd+ypgE=
|
||||
github.com/tealeg/xlsx v1.0.5/go.mod h1:btRS8dz54TDnvKNosuAqxrM1QgN1udgk9O34bDCnORM=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
67
main.go
Normal file
67
main.go
Normal file
@ -0,0 +1,67 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/tealeg/xlsx"
|
||||
)
|
||||
|
||||
var xlsxPath = flag.String("o", "", "Path to the XLSX output file")
|
||||
var csvPath = flag.String("f", "", "Path to the CSV input file")
|
||||
var delimiter = flag.String("d", ",", "Delimiter for felds in the CSV input.")
|
||||
|
||||
func usage() {
|
||||
fmt.Printf(`%s: -f=<CSV Input File> -o=<XLSX Output File> -d=<Delimiter>
|
||||
|
||||
`,
|
||||
os.Args[0])
|
||||
}
|
||||
|
||||
func generateXLSXFromCSV(csvPath string, XLSXPath string, delimiter string) error {
|
||||
csvFile, err := os.Open(csvPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer csvFile.Close()
|
||||
reader := csv.NewReader(csvFile)
|
||||
if len(delimiter) > 0 {
|
||||
reader.Comma = rune(delimiter[0])
|
||||
} else {
|
||||
reader.Comma = rune(';')
|
||||
}
|
||||
xlsxFile := xlsx.NewFile()
|
||||
sheet, err := xlsxFile.AddSheet(csvPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fields, err := reader.Read()
|
||||
for err == nil {
|
||||
row := sheet.AddRow()
|
||||
for _, field := range fields {
|
||||
cell := row.AddCell()
|
||||
cell.Value = field
|
||||
}
|
||||
fields, err = reader.Read()
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Printf(err.Error())
|
||||
}
|
||||
return xlsxFile.Save(XLSXPath)
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
if len(os.Args) < 3 {
|
||||
usage()
|
||||
return
|
||||
}
|
||||
flag.Parse()
|
||||
err := generateXLSXFromCSV(*csvPath, *xlsxPath, *delimiter)
|
||||
if err != nil {
|
||||
fmt.Printf(err.Error())
|
||||
return
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user