Skip to content

Commit

Permalink
Initial check-in
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoffrey Teale committed Sep 13, 2014
0 parents commit 72ebb1f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.org
@@ -0,0 +1,5 @@
* csv2xlsx
** Introduction
Because [[https://github.com/tealeg/xlsx2csv][the XLSX2CSV program]] exists, it seemed that it was natural to
create a csv2xlsx corollary when the underlying xlsx library grew the
ability to write XLSX files.
66 changes: 66 additions & 0 deletions main.go
@@ -0,0 +1,66 @@
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 := xlsxFile.AddSheet(csvPath)
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
}
}
3 changes: 3 additions & 0 deletions testfile.csv
@@ -0,0 +1,3 @@
"Bob";"Alice";"Sue"
"Yes";"No";"Yes"
"No";"";"Yes"

0 comments on commit 72ebb1f

Please sign in to comment.