Skip to content

Commit

Permalink
A: v0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
awesee committed Jan 15, 2021
1 parent d6d76b6 commit f7342ba
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 31 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/openset/csv-xlsx

go 1.15

require github.com/360EntSecGroup-Skylar/excelize v1.4.1
require github.com/360EntSecGroup-Skylar/excelize/v2 v2.3.2
38 changes: 34 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
github.com/360EntSecGroup-Skylar/excelize v1.4.1 h1:l55mJb6rkkaUzOpSsgEeKYtS6/0gHwBYyfo5Jcjv/Ks=
github.com/360EntSecGroup-Skylar/excelize v1.4.1/go.mod h1:vnax29X2usfl7HHkBrX5EvSCJcmH3dT9luvxzu8iGAE=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/360EntSecGroup-Skylar/excelize/v2 v2.3.2 h1:MHu5KWWt28FzRGQgc4Ryj/lZT/W/by4NvsnstbWwkkY=
github.com/360EntSecGroup-Skylar/excelize/v2 v2.3.2/go.mod h1:xc0ybJZXcn084ZaIvQv+LfCDQjMWfxkBa2K9nLXYJtI=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.3-0.20181224173747-660f15d67dbb/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/richardlehane/mscfb v1.0.3 h1:rD8TBkYWkObWO0oLDFCbwMeZ4KoalxQy+QgniCj3nKI=
github.com/richardlehane/mscfb v1.0.3/go.mod h1:YzVpcZg9czvAuhk9T+a3avCpcFPMUWm7gK3DypaEsUk=
github.com/richardlehane/msoleps v1.0.1 h1:RfrALnSNXzmXLbGct/P2b4xkFz4e8Gmj/0Vj9M9xC1o=
github.com/richardlehane/msoleps v1.0.1/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/xuri/efp v0.0.0-20201016154823-031c29024257 h1:6ldmGEJXtsRMwdR2KuS3esk9wjVJNvgk05/YY2XmOj0=
github.com/xuri/efp v0.0.0-20201016154823-031c29024257/go.mod h1:uBiSUepVYMhGTfDeBKKasV4GpgBlzJ46gXUBAqV8qLk=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee h1:4yd7jl+vXjalO5ztz6Vc1VADv+S/80LGJmyl1ROJ2AI=
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/image v0.0.0-20201208152932-35266b937fa6 h1:nfeHNc1nAqecKCy2FCy4HY+soOOe5sDLJ/gZLbx6GYI=
golang.org/x/image v0.0.0-20201208152932-35266b937fa6/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0 h1:5kGOVHlq0euqwzgTC9Vu15p6fV1Wi0ArVi8da2urnVg=
golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
63 changes: 37 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"path/filepath"
"strings"

"github.com/360EntSecGroup-Skylar/excelize"
"github.com/360EntSecGroup-Skylar/excelize/v2"
)

const VERSION = "0.1.2"
const VERSION = "0.2.0"

func main() {
args := os.Args
Expand All @@ -24,10 +24,10 @@ func main() {
for _, filename := range args[1:] {
src, err := os.Open(filename)
check(err)
ext := strings.ToLower(filepath.Ext(filename))
ext := filepath.Ext(filename)
name := strings.TrimSuffix(filename, ext)
basename := filepath.Base(filename)
switch ext {
switch strings.ToLower(ext) {
case ".csv":
csvToXlsx(src, name)
case ".xlsx":
Expand All @@ -40,43 +40,65 @@ func main() {
}

func csvToXlsx(src io.Reader, name string) {
maxRow := 0xFFFFF + 1
csvFile := csv.NewReader(src)
csvFile.FieldsPerRecord = -1
xlsxFile := excelize.NewFile()
for row := 0; ; row++ {
maxRows := excelize.TotalRows
var streamWriter *excelize.StreamWriter
streamWriterFlush := func() {
if streamWriter != nil {
check(streamWriter.Flush())
}
}
for rowID := 0; ; rowID++ {
record, err := csvFile.Read()
if err == io.EOF {
break
}
check(err)
sheet := fmt.Sprintf("Sheet%d", row/maxRow+1)
if row > 0 && row%maxRow == 0 {
if rowID%maxRows == 0 {
streamWriterFlush()
sheet := fmt.Sprintf("Sheet%d", rowID/maxRows+1)
xlsxFile.NewSheet(sheet)
streamWriter, err = xlsxFile.NewStreamWriter(sheet)
check(err)
}
row := make([]interface{}, len(record))
for k, v := range record {
xlsxFile.SetCellValue(sheet, axis(k, row%maxRow+1), v)
row[k] = v
}
axis, err := excelize.CoordinatesToCellName(1, rowID%maxRows+1)
check(err)
check(streamWriter.SetRow(axis, row))
}
xlsxFile.SetActiveSheet(1)
err := xlsxFile.SaveAs(name + ".xlsx")
check(err)
streamWriterFlush()
check(xlsxFile.SaveAs(name + ".xlsx"))
}

func xlsxToCsv(src io.Reader, name string) {
xlsxFile, err := excelize.OpenReader(src)
check(err)
for _, sheet := range xlsxFile.GetSheetMap() {
for _, sheet := range xlsxFile.GetSheetList() {
filename := name
if sheet != "Sheet1" {
filename += "_" + sheet
}
csvFile, err := os.Create(filename + ".csv")
check(err)
csvWriter := csv.NewWriter(csvFile)
for _, row := range xlsxFile.GetRows(sheet) {
err = csvWriter.Write(row)
rows, err := xlsxFile.Rows(sheet)
check(err)
totalCols := 0
for rows.Next() {
row, err := rows.Columns()
check(err)
if totalCols < 1 {
totalCols = len(row)
}
for i := totalCols - len(row); i > 0; i-- {
row = append(row, "")
}
check(csvWriter.Write(row))
}
csvWriter.Flush()
}
Expand All @@ -87,14 +109,3 @@ func check(err error) {
log.Fatal(err)
}
}

func axis(col, row int) string {
return fmt.Sprintf("%s%d", convertToTitle(col), row)
}

func convertToTitle(n int) string {
if n < 26 {
return fmt.Sprintf("%c", n+'A')
}
return convertToTitle(n/26-1) + fmt.Sprintf("%c", n%26+'A')
}

0 comments on commit f7342ba

Please sign in to comment.