exc golang與PHP輸出excel樣本

來源:互聯網
上載者:User
本文執行個體講述了golang與PHP輸出excel的方法。分享給大家供大家參考,具體如下:

以前輸入excel的時候utf8總是亂碼還是用其他方式把utf8轉換為gbk才能顯示,呵呵,其實是輸出了csv,後來群裡的朋友說需要utf8 BOM Excel才能正常識別utf8,今天測試了一下,很爽,比以前省了好幾行代碼.

golang實現:

複製代碼 代碼如下:

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") // 寫入UTF-8 BOM
w := csv.NewWriter(f)
w.Write([]string{"編號","姓名","年齡"})
w.Write([]string{"1","張三","23"})
w.Write([]string{"2","李四","24"})
w.Write([]string{"3","王五","25"})
w.Write([]string{"4","趙六","26"})
w.Flush()
}

php實現:

<?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);

希望本文所述對大家Go語言程式設計有所協助。

以上就介紹了exc golang與PHP輸出excel樣本,包括了exc方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.