Golang and PHP excel output examples, golangexcel examples

Source: Internet
Author: User

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.

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.