Skip to content

Commit 0267caf

Browse files
author
苏青安
committed
增加数据导出 DataExporter 类
1 parent 7d53fc4 commit 0267caf

File tree

2 files changed

+253
-35
lines changed

2 files changed

+253
-35
lines changed

README.md

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ composer require hejunjie/utils
1212
```
1313

1414
## 用途 & 初衷
15+
1516
这是一个典型的「开发者懒癌工具库」。
1617

1718
在日常项目中,你可能也经常遇到这些情况:
@@ -32,18 +33,18 @@ composer require hejunjie/utils
3233

3334
### 字符串操作
3435

35-
| 方法 | 说明 |
36-
| :------------------------------ | :------------------------------- |
37-
| Str::containsAny() | 检查字符串中是否存在数组中的内容 |
38-
| Str::padString() | 补充特定字符串,使其达到指定长度 |
39-
| Str::replaceFirst() | 替换字符串中第一次出现的搜索值 |
40-
| Str::generateRandomString() | 生成随机字符串 |
41-
| Str::getRandomSurname() | 获取随机姓氏 |
42-
| Str::truncateString() | 截断字符串 |
43-
| Str::maskString() | 字符串掩码 |
44-
| Str::removeWhitespace() | 移除字符串中的所有空白字符 |
45-
| Str::stringEncrypt() | 字符串加密(AES-128-CBC) |
46-
| Str::stringDecrypt() | 字符串解密(AES-128-CBC) |
36+
| 方法 | 说明 |
37+
| :------------------------------- | :------------------------------- |
38+
| Str::containsAny() | 检查字符串中是否存在数组中的内容 |
39+
| Str::padString() | 补充特定字符串,使其达到指定长度 |
40+
| Str::replaceFirst() | 替换字符串中第一次出现的搜索值 |
41+
| Str::generateRandomString() | 生成随机字符串 |
42+
| Str::getRandomSurname() | 获取随机姓氏 |
43+
| Str::truncateString() | 截断字符串 |
44+
| Str::maskString() | 字符串掩码 |
45+
| Str::removeWhitespace() | 移除字符串中的所有空白字符 |
46+
| Str::stringEncrypt() | 字符串加密(AES-128-CBC) |
47+
| Str::stringDecrypt() | 字符串解密(AES-128-CBC) |
4748
| Str::formatDurationFromSeconds() | 根据秒数转换为可读性时间 |
4849

4950
### 数组操作
@@ -56,23 +57,22 @@ composer require hejunjie/utils
5657
| Arr::groupByField() | 根据二维数组中的指定字段进行分组 |
5758
| Arr::csvToArray() | 读取 CSV 文件并返回数组格式 |
5859
| Arr::arrayToCsv() | 数组转换为 CSV 格式的字符串 |
59-
| Arr::xmlParse() | xml解析为数组 |
60-
| Arr::arrayToXml() | 数组转换为xml |
60+
| Arr::xmlParse() | xml 解析为数组 |
61+
| Arr::arrayToXml() | 数组转换为 xml |
6162

6263
### 文件操作
6364

64-
| 方法 | 说明 |
65-
| :--------------------------------------- | :------------------------- |
66-
| FileUtils::readFile() | 读取文件内容 |
67-
| FileUtils::writeToFile() | 将内容写入文件 |
68-
| FileUtils::getFileExtension() | 获取文件扩展名 |
69-
| FileUtils::joinPaths() | 拼接多个路径 |
70-
| FileUtils::getFileNameWithoutExtension() | 获取文件名(不带扩展名) |
71-
| FileUtils::fileDelete() | 删除文件或目录 |
72-
| FileUtils::writeUniqueLinesToFile() | 获取文件中的唯一行(去重) |
73-
| FileUtils::getCommonLinesFromFiles() | 从多个文件中获取交集行 |
74-
| FileUtils::extractColumnFromCsvFiles() | 从多个csv文件中快速提取列 |
75-
65+
| 方法 | 说明 |
66+
| :--------------------------------------- | :-------------------------- |
67+
| FileUtils::readFile() | 读取文件内容 |
68+
| FileUtils::writeToFile() | 将内容写入文件 |
69+
| FileUtils::getFileExtension() | 获取文件扩展名 |
70+
| FileUtils::joinPaths() | 拼接多个路径 |
71+
| FileUtils::getFileNameWithoutExtension() | 获取文件名(不带扩展名) |
72+
| FileUtils::fileDelete() | 删除文件或目录 |
73+
| FileUtils::writeUniqueLinesToFile() | 获取文件中的唯一行(去重) |
74+
| FileUtils::getCommonLinesFromFiles() | 从多个文件中获取交集行 |
75+
| FileUtils::extractColumnFromCsvFiles() | 从多个 csv 文件中快速提取列 |
7676

7777
### 网络请求操作
7878

@@ -81,7 +81,6 @@ composer require hejunjie/utils
8181
| HttpClient::sendGetRequest() | 使用 cURL 发送 GET 请求 |
8282
| HttpClient::sendPostRequest() | 使用 cURL 发送 POST 请求 |
8383

84-
8584
### 图片操作
8685

8786
| 方法 | 说明 |
@@ -92,6 +91,18 @@ composer require hejunjie/utils
9291
| Img::compressImage() | 压缩图片到指定大小(单位 KB),支持多种格式转换为 JPEG |
9392
| Img::resizeImage() | 调整图片分辨率,保持宽高比 |
9493

94+
### 导出操作
95+
96+
| 方法 | 说明 |
97+
| :----------------------------- | :------------ |
98+
| DataExporter::exportTxt() | 导出 TXT |
99+
| DataExporter::exportMarkdown() | 导出 Markdown |
100+
| DataExporter::exportCsv() | 导出 CSV |
101+
| DataExporter::exportJson() | 导出 JSON |
102+
| DataExporter::exportSql() | 导出 SQL |
103+
| DataExporter::exportHtml() | 导出 HTML |
104+
| DataExporter::exportXml() | 导出 XML |
105+
95106
## 🔧 更多工具包(可独立使用,也可统一安装)
96107

97108
本项目最初是从 [hejunjie/tools](https://github.com/zxc7563598/php-tools) 拆分而来,如果你想一次性安装所有功能组件,也可以使用统一包:
@@ -119,11 +130,3 @@ composer require hejunjie/tools
119130
---
120131

121132
该库后续将持续更新,添加更多实用功能。欢迎大家提供建议和反馈,我会根据大家的意见实现新的功能,共同提升开发效率。
122-
123-
124-
125-
126-
127-
128-
129-

src/DataExporter.php

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
<?php
2+
3+
namespace Hejunjie\Utils;
4+
5+
use SimpleXMLElement;
6+
7+
/**
8+
* 数据导出类
9+
*
10+
* @package Hejunjie\Utils
11+
*/
12+
class DataExporter
13+
{
14+
/**
15+
* 导出 TXT
16+
*
17+
* @param mixed $data 数据 [ [ 'key1' => 'value1', 'key2' => 'value2' ], [ 'key1' => 'value1', 'key2' => 'value2' ] ]
18+
* @param string $filename 文件名称
19+
* @param string $savePath 导出路径
20+
*
21+
* @return string
22+
*/
23+
public static function exportTxt($data, $filename = 'export', $savePath = '/tmp/')
24+
{
25+
if (!is_dir($savePath)) {
26+
mkdir($savePath, 0777, true);
27+
}
28+
$filePath = $savePath . $filename . '.txt';
29+
$content = "";
30+
// 获取表头
31+
$columns = array_keys($data[0]);
32+
// 写入表头
33+
$content .= implode("\t", $columns) . "\n";
34+
// 写入数据
35+
foreach ($data as $row) {
36+
$content .= implode("\t", $row) . "\n"; // 每列用制表符隔开,每行换行
37+
}
38+
file_put_contents($filePath, $content);
39+
// 返回文件路径
40+
return $filePath;
41+
}
42+
43+
/**
44+
* 导出 Markdown
45+
*
46+
* @param mixed $data 数据 [ [ 'key1' => 'value1', 'key2' => 'value2' ], [ 'key1' => 'value1', 'key2' => 'value2' ] ]
47+
* @param string $filename 文件名称
48+
* @param string $savePath 导出路径
49+
*
50+
* @return string
51+
*/
52+
public static function exportMarkdown($data, $filename = 'export', $savePath = '/tmp/')
53+
{
54+
if (!is_dir($savePath)) {
55+
mkdir($savePath, 0777, true);
56+
}
57+
$filePath = $savePath . $filename . '.md';
58+
$markdown = "# 导出数据\n\n";
59+
// 获取表头
60+
$columns = array_keys($data[0]);
61+
// 添加表格头部
62+
$markdown .= "| " . implode(" | ", $columns) . " |\n";
63+
$markdown .= "| " . str_repeat(" --- |", count($columns)) . "\n"; // 表格分隔线
64+
// 添加表格数据
65+
foreach ($data as $row) {
66+
$markdown .= "| " . implode(" | ", $row) . " |\n";
67+
}
68+
file_put_contents($filePath, $markdown);
69+
// 返回文件路径
70+
return $filePath;
71+
}
72+
73+
/**
74+
* 导出 CSV
75+
*
76+
* @param mixed $data 数据 [ [ 'key1' => 'value1', 'key2' => 'value2' ], [ 'key1' => 'value1', 'key2' => 'value2' ] ]
77+
* @param string $filename 文件名称
78+
* @param string $savePath 导出路径
79+
*
80+
* @return string
81+
*/
82+
public static function exportCsv($data, $filename = 'export', $savePath = '/tmp/')
83+
{
84+
if (!is_dir($savePath)) {
85+
mkdir($savePath, 0777, true);
86+
}
87+
$filePath = $savePath . $filename . '.csv';
88+
$output = fopen($filePath, 'w');
89+
// 获取表头
90+
$columns = array_keys($data[0]);
91+
// 写入表头
92+
fputcsv($output, $columns);
93+
// 写入数据
94+
foreach ($data as $row) {
95+
fputcsv($output, $row);
96+
}
97+
fclose($output);
98+
// 返回文件路径
99+
return $filePath;
100+
}
101+
102+
/**
103+
* 导出 JSON
104+
*
105+
* @param mixed $data 数据 [ [ 'key1' => 'value1', 'key2' => 'value2' ], [ 'key1' => 'value1', 'key2' => 'value2' ] ]
106+
* @param string $filename 文件名称
107+
* @param string $savePath 导出路径
108+
*
109+
* @return string
110+
*/
111+
public static function exportJson($data, $filename = 'export', $savePath = '/tmp/')
112+
{
113+
if (!is_dir($savePath)) {
114+
mkdir($savePath, 0777, true);
115+
}
116+
$filePath = $savePath . $filename . '.json';
117+
file_put_contents($filePath, json_encode($data, JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES + JSON_PRESERVE_ZERO_FRACTION + JSON_PRETTY_PRINT));
118+
// 返回文件路径
119+
return $filePath;
120+
}
121+
122+
/**
123+
* 导出 SQL
124+
*
125+
* @param mixed $data 数据 [ [ 'key1' => 'value1', 'key2' => 'value2' ], [ 'key1' => 'value1', 'key2' => 'value2' ] ]
126+
* @param string $tableName 数据库表名
127+
* @param string $filename 文件名称
128+
* @param string $savePath 导出路径
129+
*
130+
* @return string
131+
*/
132+
public static function exportSql($data, $tableName = 'data', $filename = 'export', $savePath = '/tmp/')
133+
{
134+
if (!is_dir($savePath)) {
135+
mkdir($savePath, 0777, true);
136+
}
137+
$filePath = $savePath . $filename . '.sql';
138+
$sql = "INSERT INTO `$tableName` (" . implode(", ", array_keys($data[0])) . ") VALUES\n";
139+
foreach ($data as $row) {
140+
$sql .= "(" . implode(", ", array_map(function ($value) {
141+
return "'" . addslashes($value) . "'"; // 防止注入,添加转义
142+
}, $row)) . "),\n";
143+
}
144+
$sql = rtrim($sql, ",\n") . ";"; // 去除最后的逗号
145+
file_put_contents($filePath, $sql);
146+
// 返回文件路径
147+
return $filePath;
148+
}
149+
150+
/**
151+
* 导出 HTML
152+
*
153+
* @param mixed $data 数据 [ [ 'key1' => 'value1', 'key2' => 'value2' ], [ 'key1' => 'value1', 'key2' => 'value2' ] ]
154+
* @param string $filename 文件名称
155+
* @param string $savePath 导出路径
156+
*
157+
* @return string
158+
*/
159+
public static function exportHtml($data, $filename = 'export', $savePath = '/tmp/')
160+
{
161+
if (!is_dir($savePath)) {
162+
mkdir($savePath, 0777, true);
163+
}
164+
$filePath = $savePath . $filename . '.html';
165+
$html = "<table border='1'><thead><tr>";
166+
// 获取表头
167+
$columns = array_keys($data[0]);
168+
foreach ($columns as $column) {
169+
$html .= "<th>$column</th>";
170+
}
171+
$html .= "</tr></thead><tbody>";
172+
// 写入数据
173+
foreach ($data as $row) {
174+
$html .= "<tr>";
175+
foreach ($row as $value) {
176+
$html .= "<td>$value</td>";
177+
}
178+
$html .= "</tr>";
179+
}
180+
$html .= "</tbody></table>";
181+
file_put_contents($filePath, $html);
182+
// 返回文件路径
183+
return $filePath;
184+
}
185+
186+
/**
187+
* 导出 XML
188+
*
189+
* @param mixed $data 数据 [ [ 'key1' => 'value1', 'key2' => 'value2' ], [ 'key1' => 'value1', 'key2' => 'value2' ] ]
190+
* @param string $filename 文件名称
191+
* @param string $savePath 导出路径
192+
*
193+
* @return string
194+
*/
195+
public static function exportXml($data, $filename = 'export', $savePath = '/tmp/')
196+
{
197+
if (!is_dir($savePath)) {
198+
mkdir($savePath, 0777, true);
199+
}
200+
$filePath = $savePath . $filename . '.xml';
201+
// 创建XML对象
202+
$xml = new SimpleXMLElement('<root/>');
203+
// 循环数据并添加到XML中
204+
foreach ($data as $row) {
205+
$rowElement = $xml->addChild('row');
206+
foreach ($row as $key => $value) {
207+
$rowElement->addChild($key, htmlspecialchars($value)); // 使用 htmlspecialchars 处理特殊字符
208+
}
209+
}
210+
// 保存XML内容到文件
211+
$xml->asXML($filePath);
212+
// 返回文件路径
213+
return $filePath;
214+
}
215+
}

0 commit comments

Comments
 (0)