Home > Backend Development > PHP Tutorial > exc golang and PHP output excel example

exc golang and PHP output excel example

WBOY
Release: 2016-07-28 08:25:27
Original
868 people have browsed it

The example in this article describes the method of outputting excel with golang and PHP. Share it with everyone for your reference, the details are as follows:

When I input excel in the past, utf8 was always garbled, so I had to use other methods to convert utf8 to gbk before it could be displayed. Haha, it actually output csv. Later, friends in the group said that utf8 BOM was needed. Only Excel can recognize utf8 normally. I tested it today and it was great. It saved several lines of code than before.

golang implementation:

Copy the code The 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("xEFxBBxBF") // Write UTF-8 BOM
w := csv.NewWriter(f)
w.Write([]string{"number", "name", "age"})
w.Write([]string{"1","Zhang San","23"})
w.Write([]string{"2","李思","24"})
w.Write( []string{"3","王五","25"})
w.Write([]string{"4","Zhao Liu","26"})
w.Flush()
}

php implementation:

<?php
$datas = array(
    array(1, "张三", 23), 
    array(2, "李四", 24),
    array(3, "王五", 25),
    array(4, "赵六", 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("编号", "姓名", "年龄");
fputcsv($fp, $head);
foreach ($datas as $r) {
  fputcsv($fp, $r);
}
fclose($fp);

Copy after login

I hope this article will be helpful to everyone in Go language programming.

The above introduces exc golang and PHP output excel examples, including exc content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
exc
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template