Skip to content

Instantly share code, notes, and snippets.

@dryaf
Last active May 3, 2017 13:30
Show Gist options
  • Save dryaf/352156a1784e88c9f307b9a30376113b to your computer and use it in GitHub Desktop.
Save dryaf/352156a1784e88c9f307b9a30376113b to your computer and use it in GitHub Desktop.
csv with custom sperator to xlsx converter
package main
import (
"bufio"
"fmt"
"log"
"os"
"github.com/tealeg/xlsx"
"strings"
"path/filepath"
)
func main() {
var file_xls *xlsx.File
var sheet *xlsx.Sheet
var row *xlsx.Row
var err error
app_dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
file_csv, err := os.Open(app_dir + "/input.csv")
if err != nil {
log_usage()
log.Fatal(err)
}
defer file_csv.Close()
scanner := bufio.NewScanner(file_csv)
file_xls = xlsx.NewFile()
sheet, err = file_xls.AddSheet("GameText")
if err != nil {
fmt.Printf(err.Error())
}
row = sheet.AddRow()
for scanner.Scan() {
str_cols := strings.Split(scanner.Text(), "|")
row = sheet.AddRow()
str_id := ""
for i := 0; i < len(str_cols); i++ {
if i == 0 {
str_id = str_cols[i]
}
cell := row.AddCell()
if i == 1 {
cell.Value = str_cols[i] + str_id
} else {
cell.Value = str_cols[i]
}
}
}
if err := scanner.Err(); err != nil {
log_usage()
log.Fatal(err)
}
err = file_xls.Save(app_dir + "/output.xlsx")
if err != nil {
fmt.Printf(err.Error())
log_usage()
}
if err == nil {
fmt.Println("I created output.xlsx from input.csv")
}
}
func log_usage() {
fmt.Println("-------------------------------------------------------------------")
fmt.Println(" you need to have a file called 'input.csv' in the same folder")
fmt.Println(" instead of a comma the sperator must be '|'")
fmt.Println(" it will create/overwrite the file 'output.xlsx'")
fmt.Println("-------------------------------------------------------------------")
}
@dryaf
Copy link
Author

dryaf commented May 3, 2017

delete empty first row, right click second row - convert to header row. now you are able to sort in numbers,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment