This article illustrates the method of Golang and PHP output Excel. Share to everyone for your reference, specific as follows:
Previously entered Excel when UTF8 is always garbled or other ways to UTF8 converted to GBK to show, oh, is actually output CSV, and then the group of friends that need to UTF8 BOM Excel to normal identification UTF8, today tested a bit, very cool, than previously saved several lines of code .
Golang implementation:
Copy Code code 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 UTF-8 BOM
W: = csv. Newwriter (f)
W.write ([]string{"number", "Name", "Age"})
W.write ([]string{"1", "John", "23"})
W.write ([]string{"2", "Dick", "24"})
W.write ([]string{"3", "Harry", "25"})
W.write ([]string{"4", "Zhao Liu", "26"})
W.flush ()
}
PHP Implementation:
<?php
$datas = array (
array (1, "John",),
array (2, "Dick",), Array (
3, "Harry",),
Array (4, "Zhao Liu", ),
);
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 ("number", "Name", "Age");
Fputcsv ($fp, $head);
foreach ($datas as $r) {
fputcsv ($fp, $r);
}
Fclose ($FP);
I hope this article will help you with your go language program.