package export

import (
	"encoding/csv"
	"os"
)

type CsvExport struct {
}

func (e *CsvExport) ExportData(fileName string, datas [][]string, comma ...rune) error {
	// 打开文件
	file, err := os.Create(fileName)
	if err != nil {
		return err
	}
	defer file.Close()

	// 写入UTF-8 BOM，避免使用Microsoft Excel打开乱码
	file.WriteString("\xEF\xBB\xBF")

	// 创建csv writer
	writer := csv.NewWriter(file)
	if len(comma) > 0 {
		writer.Comma = comma[0]
	}
	defer writer.Flush()

	// 写入数据
	for _, data := range datas {
		err = writer.Write(data)
		if err != nil {
			return err
		}
	}

	return nil
}
