网站首页 > 文章精选 正文
1、MySQL
方式1:
select * into outfile 'E:\\xxx\\test.csv'
fields terminated by ','
lines terminated by '\n'
from (select *from test) b;
方式2:
mysql -uroot test -e "sql语句" -N -s | sed -e 's/^/"/g;s/$/"\n/g' > /tmp/test.csv
#或者
mysql -uroot test -e "sql语句" > /tmp/test.csv
2、PHP
查询数据,通过逗号拼接字段数据,\n分割每一行数据
<?php
$host = 'localhost';
$user = '用户名';
$password = '密码';
$database = 'test';
$port = '端口';
$conn = mysqli_init();
$conn->connect($host, $user, $password, $database, $port);
// SQL字段
$sql = 'select id,`name`,course,score from score';
$query = $conn->query($sql);
$data = [];
while ($row = $query->fetch_array(MYSQLI_NUM)) {
$data[] = $row;
}
$content = "id,name,course,score\n";
foreach ($data as $d) {
$content .= implode(',', $d) . "\n";
}
file_put_contents('file.csv', $content);
// 如果数据量大的话,请将数据循环写入文件,
// 而不是通过file_put_contents一次性写入文件
3、Go
下载github.com/tealeg/xlsx包,然后进行数据处理,另存为xlsx后缀的文件
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/tealeg/xlsx"
"strconv"
"time"
)
// mysql操作:https://www.jianshu.com/p/9b5cd762e256
// excel操作;https://studygolang.com/articles/5259
const (
DRIVER = "mysql"
USERNAME = "用户名"
PASSWORD = "密码"
HOST = "localhost"
PORT = "端口"
DATABASE = "test"
CHARSET = "utf8"
)
var db *sql.DB
var err error
type Score struct {
Id int64
Name string
Course string
Score int64
}
// db连接
func DbConn() {
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s", USERNAME, PASSWORD, HOST, PORT, DATABASE, CHARSET)
db, err = sql.Open(DRIVER, dsn)
if err != nil {
panic("数据源配置不正确:" + err.Error())
}
db.SetMaxOpenConns(100) // 最大连接数
db.SetMaxIdleConns(20) // 闲置连接数
db.SetConnMaxLifetime(100 * time.Second) // 最大连接周期
if err = db.Ping(); err != nil {
panic("数据库连接失败:" + err.Error())
}
}
var data = make([]Score, 0)
// 从db获取数据
func GetDataFromDb() {
sqlstr := "select * from score"
rows, _ := db.Query(sqlstr)
var result Score
for rows.Next() {
rows.Scan(&result.Id, &result.Name, &result.Course, &result.Score)
data = append(data, result)
}
//fmt.Println(data)
//fmt.Printf("%+v", data)
}
// 导出到csv文件
func ExportToCsvFile() {
file := xlsx.NewFile()
sheet, _ := file.AddSheet("Sheet1")
//row := sheet.AddRow()
//row.SetHeightCM(1) //设置每行的高度
//cell := row.AddCell()
//cell.Value = "hello"
//cell = row.AddCell()
//cell.Value = "world"
row := sheet.AddRow()
cell := row.AddCell()
cell.Value = "Id"
cell = row.AddCell()
cell.Value = "Name"
cell = row.AddCell()
cell.Value = "Course"
cell = row.AddCell()
cell.Value = "Score"
for _, item := range data {
row = sheet.AddRow()
cell = row.AddCell()
cell.Value = strconv.FormatInt(item.Id, 10)
cell = row.AddCell()
cell.Value = item.Name
cell = row.AddCell()
cell.Value = item.Course
cell = row.AddCell()
cell.Value = strconv.FormatInt(item.Score, 10)
}
err := file.Save("file.xlsx")
if err != nil {
panic(err)
}
}
func main() {
DbConn() //数据库连接
GetDataFromDb() // 从数据库获取数据
ExportToCsvFile() // 导出到csv文件
}
猜你喜欢
- 2025-07-14 Go并发编程之WaitGroup(go语言 并发)
- 2025-07-14 golang企业微信告警(企业微信告警推送)
- 2025-07-14 2.8 Go语言中的for循环,break和continue
- 2025-07-14 Go语言Context包:最常用包之一的使用指南
- 2025-07-14 Go语言零到一:动态数组——切片(go语言的切片)
- 2025-07-14 2025-06-26:转换数组。用go语言,给你一个整数数组 nums,它被视
- 2025-07-14 go sync.Pool简介(go system)
- 2025-07-14 2025-07-13:统计特殊子序列的数目。用go语言,给定一个只包含正
- 2025-07-14 Go语言数据库编程:GORM 的基本使用
- 2025-07-14 2025-06-28:长度可被 K 整除的子数组的最大元素和。用go语言,给
- 最近发表
- 标签列表
-
- newcoder (56)
- 字符串的长度是指 (45)
- drawcontours()参数说明 (60)
- unsignedshortint (59)
- postman并发请求 (47)
- python列表删除 (50)
- 左程云什么水平 (56)
- 编程题 (64)
- postgresql默认端口 (66)
- 数据库的概念模型独立于 (48)
- 产生系统死锁的原因可能是由于 (51)
- 数据库中只存放视图的 (62)
- 在vi中退出不保存的命令是 (53)
- 哪个命令可以将普通用户转换成超级用户 (49)
- noscript标签的作用 (48)
- 联合利华网申 (49)
- swagger和postman (46)
- 结构化程序设计主要强调 (53)
- 172.1 (57)
- apipostwebsocket (47)
- 唯品会后台 (61)
- 简历助手 (56)
- offshow (61)
- mysql数据库面试题 (57)
- fmt.println (52)