Golang and PHP excel output examples, golangexcel examples
This article describes how to output excel using golang and PHP. We will share this with you for your reference. The details are as follows:
When I used to input an excel file, utf8 was always garbled or I used other methods to convert utf8 to gbk for display, later, my friends in the group said that utf8 BOM Excel was required to recognize utf8 normally. Today I tested utf8, which is better than I used to save several lines of code.
Golang implementation:
Copy codeThe Code is as follows: package main
Import (
"OS"
"Encoding/csv"
)
Func main (){
F, err: = OS. Create ("haha2.xls ")
If err! = Nil {
Panic (err)
}
Defer f. Close ()
F. WriteString ("\ xEF \ xBB \ xBF") // write the UTF-8 BOM
W: = csv. NewWriter (f)
W. Write ([] string {"no.", "name", "Age "})
W. Write ([] string {"1", "Zhang San", "23 "})
W. Write ([] string {"2", "Li Si", "24 "})
W. Write ([] string {"3", "Wang Wu", "25 "})
W. Write ([] string {"4", "Zhao ", "26 "})
W. Flush ()
}
Php implementation:
<? Php $ datas = array (1, "Zhang San", 23), array (2, "Li Si", 24), array (3, "Wang Wu", 25 ), array (4, "Zhao six", 26),); header ("Content-type: application/vnd. ms-excel "); header (" Content-Disposition: filename = ". date ('ymdhis '). ". xls "); $ fp = fopen ('php: // output', 'w'); fwrite ($ fp," \ xEF \ xBB \ xBF "); $ head = array ("no.", "name", "Age"); fputcsv ($ fp, $ head); foreach ($ datas as $ r) {fputcsv ($ fp, $ r);} fclose ($ fp );
I hope this article will help you with the Go language programming.