💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
【PHPOffice/PhpSpreadsheet的案例】 ``` <?php require 'vendor/autoload.php'; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $file_mimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); if(isset($_FILES['file']['name']) && in_array($_FILES['file']['type'], $file_mimes)) { $arr_file = explode('.', $_FILES['file']['name']); $extension = end($arr_file); if('csv' == $extension) { $reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv(); } else { $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx(); } $spreadsheet = $reader->load($_FILES['file']['tmp_name']); $sheetData = $spreadsheet->getActiveSheet()->ToArray(); echo '<pre>'; // show data $count = count($sheetData); foreach($sheetData as $k=>$v){ $result[$k] = count($v); } $row_number = max($result) - 1; echo '<table border="1">'; for($i=0;$i<$count;$i++) { echo '<tr>'; if(is_array($sheetData[$i])) { for($j=0;$j<=$row_number;$j++) { echo '<td>'.$sheetData[$i][$j].'</td>'; } } echo '</tr>'; } echo '</table>'; echo '<hr/>'; // /show data // csv for all $sheetCsv = $sheetData; $sheetCsv = array ( array('title','content', 'view', 'create_time','update_time'), array('title1','content1', '1', '2019-01-23 01:01:01','2019-01-25 01:01:01'), array('title1','content1', '1', '2019-01-23 01:01:01','2019-01-25 01:01:01'), array('title2','content2', '2', '2019-01-23 01:01:01','2019-01-25 01:01:01'), array('title3','content3', '3', '2019-01-23 01:01:01','2019-01-25 01:01:01'), array('title4','content4', '11', '2019-01-23 01:01:01','2019-01-25 01:01:01'), array('title5','content5', '12', '2019-01-23 01:01:01','2019-01-25 01:01:01') ); // /csv for all // csv for choose $sheetCsv = array ( $sheetData[0], $sheetData[1], $sheetData[2] ); $fh = fopen('file.csv','w') or die("Can't open file.csv"); foreach ($sheetCsv as $sheetCsv) { if (fputcsv($fh, $sheetCsv) === false) { die("Can't write CSV line"); } } fclose($fh) or die("Can't close file.csv"); // /csv for choose // json $count = count($sheetData); foreach($sheetData as $k=>$v){ $result[$k] = count($v); } $row_number = max($result) - 1; echo '[';echo '<br/>'; for($i=0;$i<$count;$i++) { if(is_array($sheetData[$i])) { echo '['; for($j=0;$j<=$row_number;$j++) { echo '"' . $sheetData[$i][$j] . '",'; } echo ' "" ],'; } echo '<br/>'; } echo '[] ]'; // /json // insert data into db $connect = mysqli_connect("localhost", "root", "root", "test"); if(is_array($sheetData)) { foreach($sheetData as $row => $value) { $data1 = mysqli_real_escape_string($connect, $value[0]); $data2 = mysqli_real_escape_string($connect, $value[1]); $data3 = mysqli_real_escape_string($connect, $value[3]); $sql = "INSERT INTO demo(list1, list2, list3) VALUES ('".$data1."', '".$data2."', '".$data3."')"; mysqli_query($connect, $sql); } } // /insert data into db } ``` .