├── testfile.csv ├── go.mod ├── go.sum ├── LICENSE ├── main.go └── README.org /testfile.csv: -------------------------------------------------------------------------------- 1 | "Bob";"Alice";"Sue" 2 | "Yes";"No";"Yes" 3 | "No";"";"Yes" 4 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/tealeg/csv2xlsx 2 | 3 | go 1.12 4 | 5 | require github.com/tealeg/xlsx v1.0.5 6 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 2 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 3 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 4 | github.com/tealeg/xlsx v1.0.5 h1:+f8oFmvY8Gw1iUXzPk+kz+4GpbDZPK1FhPiQRd+ypgE= 5 | github.com/tealeg/xlsx v1.0.5/go.mod h1:btRS8dz54TDnvKNosuAqxrM1QgN1udgk9O34bDCnORM= 6 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014, Geoff Teale 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of the {organization} nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | 29 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/csv" 5 | "flag" 6 | "fmt" 7 | "os" 8 | 9 | "github.com/tealeg/xlsx" 10 | ) 11 | 12 | var xlsxPath = flag.String("o", "", "Path to the XLSX output file") 13 | var csvPath = flag.String("f", "", "Path to the CSV input file") 14 | var delimiter = flag.String("d", ";", "Delimiter for felds in the CSV input.") 15 | 16 | func usage() { 17 | fmt.Printf(`%s: -f= -o= -d= 18 | 19 | `, 20 | os.Args[0]) 21 | } 22 | 23 | func generateXLSXFromCSV(csvPath string, XLSXPath string, delimiter string) error { 24 | csvFile, err := os.Open(csvPath) 25 | if err != nil { 26 | return err 27 | } 28 | defer csvFile.Close() 29 | reader := csv.NewReader(csvFile) 30 | if len(delimiter) > 0 { 31 | reader.Comma = rune(delimiter[0]) 32 | } else { 33 | reader.Comma = rune(';') 34 | } 35 | xlsxFile := xlsx.NewFile() 36 | sheet, err := xlsxFile.AddSheet(csvPath) 37 | if err != nil { 38 | return err 39 | } 40 | fields, err := reader.Read() 41 | for err == nil { 42 | row := sheet.AddRow() 43 | for _, field := range fields { 44 | cell := row.AddCell() 45 | cell.Value = field 46 | } 47 | fields, err = reader.Read() 48 | } 49 | if err != nil { 50 | fmt.Printf(err.Error()) 51 | } 52 | return xlsxFile.Save(XLSXPath) 53 | } 54 | 55 | func main() { 56 | flag.Parse() 57 | if len(os.Args) < 3 { 58 | usage() 59 | return 60 | } 61 | flag.Parse() 62 | err := generateXLSXFromCSV(*csvPath, *xlsxPath, *delimiter) 63 | if err != nil { 64 | fmt.Printf(err.Error()) 65 | return 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /README.org: -------------------------------------------------------------------------------- 1 | * csv2xlsx 2 | ** Introduction 3 | Because [[https://github.com/tealeg/xlsx2csv][the XLSX2CSV program]] exists, it seemed that it was natural to 4 | create a csv2xlsx corollary when the underlying xlsx library grew the 5 | ability to write XLSX files. 6 | 7 | *Warning, this is old and largely unmaintained.* 8 | 9 | ** Installation 10 | 11 | In order to build and install this program you'll need the go toolchain. You can find this in the package management system of pretty much every Linux distro and in Homebrew for Mac. Failing that you can download and install it manually from here: https://go.dev/dl/ 12 | 13 | Once you have Go installed, you'll need to either clone this repository: 14 | 15 | #+BEGIN_SRC sh 16 | git clone git@github.com:tealeg/csv2xlsx.git 17 | #+END_SRC 18 | ... or download and extract a Zipped snapshot from the green "Code" button above. 19 | 20 | From within the resulting `csv2xlsx` directory issue the following command to build the project: 21 | 22 | #+BEGIN_SRC sh 23 | go build -v . 24 | #+END_SRC 25 | 26 | If all goes well you shuould find the compiled binary =csv2xlsx= has been created. 27 | 28 | ** Invocation 29 | 30 | To run =csv2xlsx= you must provide it with at least two parameters: an input file // and an output file //. For example: 31 | 32 | #+BEGIN_SRC sh 33 | ./csv2xlsx -f=MyData.csv -o=MyData.xslx 34 | #+END_SRC 35 | 36 | If your input file uses a delimiter other than a comma then you must provide that as a third paramater, thus: 37 | 38 | #+BEGIN_SRC sh 39 | ./csv2xlsx -f=MyData.csv -o=MyData.xslx -d=";" 40 | #+END_SRC 41 | 42 | 43 | ** Alternatives 44 | If you're looking for a more useful csv2xlsx convertor checkout out.. : 45 | 46 | - [[https://gitlab.com/DerLinkshaender/csv2xlsx][gitlab.com/Derlinkshaender/csv2xlsx]] 47 | --------------------------------------------------------------------------------