|
| 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