Skip to content

Instantly share code, notes, and snippets.

@mlh758
Last active June 19, 2019 20:32
Show Gist options
  • Save mlh758/0a8daf41d63af7198beb3ed25ccb0ac2 to your computer and use it in GitHub Desktop.
Save mlh758/0a8daf41d63af7198beb3ed25ccb0ac2 to your computer and use it in GitHub Desktop.
Go Excelize Perf Test
module github.com/mlh758/exc_test
go 1.12
require github.com/360EntSecGroup-Skylar/excelize v1.4.1
package main
import (
"encoding/csv"
"fmt"
"io"
"log"
"os"
"github.com/360EntSecGroup-Skylar/excelize"
)
const testFileName = "testFile.csv"
const rowCount = 10000
func main() {
makeCSV(rowCount)
csvBody, err := os.Open(testFileName)
if err != nil {
log.Print("unable to open test file")
os.Exit(1)
}
WriteExcel(csvBody)
}
func WriteExcel(csvBody *os.File) {
reader := csv.NewReader(csvBody)
xlsx := excelize.NewFile()
row := 0
for {
row++
line, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
log.Printf("Error Reading CSV: %+v ", err)
os.Exit(1)
}
for cellIndex, cellValue := range line {
col, _ := excelize.ColumnNumberToName(cellIndex + 1)
xlsx.SetCellStr("Sheet1", fmt.Sprintf("%s%d", col, row), cellValue)
}
}
xlsx.SaveAs("testOutput.xlsx")
}
func makeCSV(rowCount int) {
f, _ := os.Create(testFileName)
headers := []string{"A", "B", "C", "D", "E"}
testRow := []string{"Some Data", "More Data", "Another string here", "Why not a number?", "723129123"}
w := csv.NewWriter(f)
w.Write(headers)
for i := 0; i < rowCount; i++ {
w.Write(testRow)
}
w.Flush()
f.Close()
}
package main
import (
"os"
"testing"
)
func BenchmarkWriteExcel(b *testing.B) {
makeCSV(50000)
csvBody, err := os.Open(testFileName)
if err != nil {
b.Error("unable to open test file")
return
}
b.ResetTimer()
WriteExcel(csvBody)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment