LoginSignup
3
2

More than 5 years have passed since last update.

goで絵文字表示可能なエクセルで開けるcsvファイルの出力(utf16le, タブ区切り)

Posted at

UTF16LEのBOM付きでエンコード、タブ区切りで出力。

main.go
package main

import (
    "fmt"
    "encoding/csv"
    "bytes"
    "io/ioutil"
    "golang.org/x/text/transform"
    "golang.org/x/text/encoding/unicode"
    "os"
)

func main() {
    buf := new(bytes.Buffer)
    w := csv.NewWriter(buf)
    w.Comma = '\t'
    w.Write([]string{"name", "type"})
    w.Write([]string{"blacksmith🔨", "villager"})
    w.Write([]string{"creeper💣", "enemy"})
    w.Write([]string{"zonbie😡", "enemy"})
    w.Flush()
    utf16byte, err := ioutil.ReadAll(transform.NewReader(
        buf,
        unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewEncoder()))
    if err != nil {
        fmt.Println(err)
    }
    file, err := os.OpenFile("utf16le.csv", os.O_WRONLY|os.O_CREATE, 0600)
    file.Write(utf16byte)
}
3
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2