Skip to content

Commit 3f51220

Browse files
author
tider
committed
t
1 parent 20173b5 commit 3f51220

File tree

2 files changed

+78
-10
lines changed

2 files changed

+78
-10
lines changed

README.md

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,71 @@
1-
## Mysql-To-Excel
1+
# MySQL-To-Excel 数据导出工具
22

3-
#### 使用说明
3+
一个用Rust编写的工具,用于从MySQL数据库分页查询数据并导出到Excel文件。
44

5-
1. 将config-sample.toml改名为config.toml,配置数据库连接信息及需要导出的sql语句和page_size。page_size的大小可以根据数据量和数据库性能自行调整,建议不要设置太高,避免把数据库爬崩。
5+
## 功能特性
66

7-
2. 去reeleases页面下载最新的mysql-to-excel执行文件。
7+
- 支持分页查询大数据量,避免内存溢出
8+
- 自动识别列名和数据格式
9+
- 显示进度条,直观查看导出进度
10+
- 生成标准Excel格式文件
811

9-
3. config.toml文件必须和mysql-to-excel放到同一个目录下,然后执行mysql-to-excel,执行完后会在同目录下生成一个data.xlsx文件。
12+
## 运行环境
13+
14+
- Rust 1.65+ (仅编译时需要)
15+
- MySQL 5.7+ 数据库
16+
17+
## 安装方式
18+
19+
### 1. 下载预编译版本
20+
21+
从Releases页面下载对应平台的预编译版本。
22+
23+
### 2. 从源码编译
24+
25+
```bash
26+
git clone https://github.com/your-repo/mysql-to-excel.git
27+
cd mysql-to-excel
28+
cargo build --release
29+
```
30+
31+
编译后的可执行文件位于 `target/release/mysql-to-excel`
32+
33+
## 配置说明
34+
35+
1. 复制 `config-sample.toml``config.toml`
36+
2. 编辑 `config.toml` 文件:
37+
38+
```toml
39+
[database]
40+
host = "数据库地址"
41+
port = 3306 # 数据库端口
42+
user = "用户名"
43+
password = "密码"
44+
db_name = "数据库名"
45+
46+
[query]
47+
sql = "SELECT * FROM your_table" # 要执行的SQL查询
48+
page_size = 100 # 每页数据量(建议100-1000)
49+
```
50+
51+
## 使用说明
52+
53+
1.`config.toml` 与可执行文件放在同一目录
54+
2. 运行程序:
55+
```bash
56+
./mysql-to-excel
57+
```
58+
3. 程序运行完成后,会在当前目录生成 `data.xlsx` 文件
59+
60+
## 输出文件格式
61+
62+
- 第一行为列名
63+
- 后续每行为查询结果数据
64+
- 自动转换MySQL数据类型为Excel格式
65+
66+
## 注意事项
67+
68+
1. 请确保SQL查询语句正确且权限足够
69+
2. 大数据量导出时,建议设置合理的page_size(100-1000)
70+
3. 程序运行期间请保持网络连接稳定
71+
4. 导出的Excel文件会覆盖同名文件,请注意备份

src/main.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,27 @@ async fn main() -> Result<(), anyhow::Error> {
9797
}
9898

9999
pb.finish_with_message(format!("导出完成! 共处理 {} 条数据", total_rows));
100-
workbook.save("./data.xlsx")?;
100+
101+
let excel_path = get_exe_dir()?.join("data.xlsx");
102+
workbook.save(excel_path)?;
101103
drop(conn);
102104
Ok(())
103105
}
104106

105-
106107
fn load_config() -> Result<Config, anyhow::Error> {
107-
let exe_path = std::env::current_exe()?;
108-
let exe_dir = exe_path.parent().ok_or_else(|| anyhow::anyhow!("Cannot get executable directory"))?;
109-
let config_path = exe_dir.join("config.toml");
108+
let config_path = get_exe_dir()?.join("config.toml");
110109
let config_str = std::fs::read_to_string(config_path)?;
111110
let config: Config = toml::from_str(&config_str)?;
112111
Ok(config)
113112
}
114113

114+
fn get_exe_dir() -> Result<std::path::PathBuf, anyhow::Error> {
115+
std::env::current_exe()?
116+
.parent()
117+
.ok_or_else(|| anyhow::anyhow!("Cannot get executable directory"))
118+
.map(|p| p.to_path_buf())
119+
}
120+
115121
fn append_to_excel(worksheet: &mut Worksheet, row: Vec<String>, row_index: u32) -> Result<(), anyhow::Error> {
116122
for (col_index, value) in row.iter().enumerate() {
117123
worksheet.write(row_index, col_index.try_into()?, value)?;

0 commit comments

Comments
 (0)