Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Geoffrey Teale
committed
Sep 13, 2014
0 parents
commit 72ebb1f
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"Bob";"Alice";"Sue" | ||
"Yes";"No";"Yes" | ||
"No";"";"Yes" |