How to use PHP to implement data import and export functions
Importing and exporting data is one of the common functions in web application development. Through the import function, external data sources can be imported into the application's database; through the export function, the data in the application can be exported to a format for external use (such as Excel, CSV, etc.).
This article will introduce how to use PHP to implement data import and export functions, and will include specific code examples.
1. Data import function
To implement the data import function, the following steps are usually required:
<form method="post" enctype="multipart/form-data" action="import.php"> <input type="file" name="file" required> <input type="submit" value="导入"> </form>
$uploadPath = './uploads/'; // 上传文件保存路径 $uploadedFile = $uploadPath . basename($_FILES['file']['name']); // 拼接上传文件的路径 if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadedFile)) { echo '文件上传成功'; } else { echo '文件上传失败'; }
$data = []; // 存储解析后的数据,格式为数组 // 使用第三方类库 PHPExcel 解析 Excel 文件 require_once './PHPExcel/PHPExcel.php'; $excelReader = PHPExcel_IOFactory::createReaderForFile($uploadedFile); $excelReader->setReadDataOnly(true); $excelObj = $excelReader->load($uploadedFile); $worksheet = $excelObj->getActiveSheet(); $highestRow = $worksheet->getHighestRow(); $highestColumn = $worksheet->getHighestColumn(); for ($row = 2; $row <= $highestRow; $row++) { $rowData = $worksheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, null, true, false)[0]; // 解析每一行的数据,存入数组 $data[] = [ 'field1' => $rowData[0], 'field2' => $rowData[1], // ... ]; } // 将数据存入数据库 foreach ($data as $row) { // 执行插入数据库的操作,这里以 MySQL 数据库作为示例 $sql = "INSERT INTO table_name (field1, field2) VALUES ('{$row['field1']}', '{$row['field2']}')"; // ... // 执行 sql 语句 }
2. Data export function
To implement the data export function, several steps are also required:
// 查询数据库,获取需要导出的数据,这里以 MySQL 数据库作为示例 $sql = "SELECT * FROM table_name"; $result = mysqli_query($conn, $sql); $data = []; while ($row = mysqli_fetch_assoc($result)) { // 将每一行的数据存入数组 $data[] = $row; }
// 使用第三方类库 PHPExcel 生成 Excel 文件 require_once './PHPExcel/PHPExcel.php'; $excelObj = new PHPExcel(); $excelObj->setActiveSheetIndex(0); $excelObj->getActiveSheet()->setTitle('Sheet1'); $row = 1; foreach ($data as $rowData) { // 将数据填充到每一行的单元格 $col = 0; foreach ($rowData as $value) { $excelObj->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value); $col++; } $row++; } $excelWriter = PHPExcel_IOFactory::createWriter($excelObj, 'Excel5'); $exportFile = './exports/export.xls'; // 导出文件保存路径 $excelWriter->save($exportFile);
The above are the basic steps and code examples for using PHP to implement data import and export functions. Through these examples, you can further expand and optimize according to your actual needs to meet specific business needs.
I hope this article will be helpful for you to learn and practice the data import and export functions!
The above is the detailed content of How to use PHP to implement data import and export functions. For more information, please follow other related articles on the PHP Chinese website!