-
-
Save BjoernSchilberg/b95bd57082f445dd3db3b05a927e44b3 to your computer and use it in GitHub Desktop.
Go Excelize Perf Test
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
module github.com/mlh758/exc_test | |
go 1.12 | |
require github.com/360EntSecGroup-Skylar/excelize v1.4.1 |
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
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() | |
} |
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
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