Golang/php output Excel

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Previously input Excel when UTF8 always garbled or use other ways to convert UTF8 to GBK to show, hehe, in fact, is the output of CSV, later group of friends said need Utf8bom Excel to normal identification UTF8, today tested a bit, very cool, Save several lines of code than before.

BOM Information Reference:

http://zh.wikipedia.org/wiki/bit sequence notation

Golang implementation:

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 Into UTF-8 BOM w: = csv.    Newwriter (f) w.write ([]string{"number", "Name", "Age"}) W.write ([]string{"1", "Zhang San", "23°c"}) w.write ([]string{"2", "John Doe", "24"}) W.write ([]string{"3", "Harry", "+"}) w.write ([]string{"4", "Zhao Liu", "+"}) W.flush ()} 

PHP Implementation:

<?php$datas = Array (Array (1, "Zhang San", +), Array (2, "John Doe", ","), Array (3, "Harry", +), array (4,     "Zhao Liu", 26),); Header ("Content-type:application/vnd.ms-excel"), Header ("Content-disposition:filename=". Date (' Ymdhis '). ".  XLS "); $fp = fopen (' Php://output ', ' W '), Fwrite ($fp, Pack ("H6", "EFBBBF")), $head = Array ("number", "Name", "Age"), Fputcsv ($fp, $head); foreach ($datas as $r) {fputcsv ($fp, $r);} Fclose ($FP);
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.