如何用ThinkPHP6实现Excel高效导入导出功能?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1141个文字,预计阅读时间需要5分钟。
随着互联网的快速发展,Excel已经成为企业和个人日常办公中不可或缺的工具之一。因此,Excel的导入导出功能已成为了许多应用软件的必要组成部分。下面简要介绍如何使用ThinkPHP6实现Excel的导入导出功能:
1. 安装PHPExcel扩展库:首先,需要在项目中安装PHPExcel库。可以通过Composer进行安装: bash composer require phpoffice/phpexcel
2. 创建Excel文件:在ThinkPHP6中,可以使用PHPExcel库创建Excel文件。以下是一个简单的示例:
php use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet=new Spreadsheet(); $sheet=$spreadsheet->getActiveSheet();
// 设置 $sheet->setCellValue('A1', '姓名'); $sheet->setCellValue('B1', '年龄'); $sheet->setCellValue('C1', '性别');
// 添加数据 $sheet->setCellValue('A2', '张三'); $sheet->setCellValue('B2', 25); $sheet->setCellValue('C2', '男');
// 保存Excel文件 $writer=new Xlsx($spreadsheet); $writer->save('example.xlsx');
3. 读取Excel文件:同样,可以使用PHPExcel库读取Excel文件。以下是一个简单的示例:
php use PhpOffice\PhpSpreadsheet\IOFactory;
$spreadsheet=IOFactory::load('example.xlsx'); $sheetData=$spreadsheet->getActiveSheet()->toArray();
// 输出数据 print_r($sheetData);
4. 整合到ThinkPHP6控制器中:将上述代码整合到ThinkPHP6的控制器中,即可实现Excel的导入导出功能。
php public function excel() { // 创建Excel文件 $this->createExcel();
// 读取Excel文件 $data=$this->readExcel(); // 处理数据... }
通过以上步骤,您可以使用ThinkPHP6实现Excel的导入导出功能。需要注意的是,实际应用中可能需要根据具体需求对PHPExcel库进行扩展和修改。
随着互联网的快速发展,Excel已经成为公司和个人日常办公中重要的工具之一。因此,Excel导入导出的功能已经成为许多应用程序的必要组成部分。如何使用ThinkPHP6实现Excel导入导出呢?下面,本文将为您详细介绍。
一、ThinkPHP6系统环境验证
使用ThinkPHP6实现Excel导入导出,首先需要满足系统环境要求。在ThinkPHP6中,可以使用composer安装phpoffice/phpspreadsheet库,实现Excel处理功能。在命令行中执行以下命令进行安装:
composer require phpoffice/phpspreadsheet
安装完毕后,在controller层中引入PhpOfficePhpSpreadsheetSpreadsheet和PhpOfficePhpSpreadsheetWriterXlsx类库,这些类库将在我们的后面代码中使用。
二、Excel导出
- 导出Excel数据
使用ThinkPHP6实现Excel导出,首先需要将数据导入到Excel中。我们可以根据需要,在controller层中编写相应的代码。例如,在导出学生信息时,可以通过查询数据库获取相关信息。
use appcommonmodelStudent; public function export() { $students = Student::all(); }
- 设置Excel表格的头部
Excel的表格头部是非常重要的一部分。为了使Excel表格的头部清晰明了,我们可以编写代码来生成表格的头部信息。在下面的代码中,我们使用了循环来实现。
$spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setTitle('学生信息表'); $sheet->setCellValue('A1', '序号'); $sheet->setCellValue('B1', '学号'); $sheet->setCellValue('C1', '姓名'); $sheet->setCellValue('D1', '性别'); $sheet->setCellValue('E1', '日期');
- 将数据写入Excel表格
接下来,将获取的数据写入Excel表格中。循环遍历数据,将每个学生的信息依次写入表格中。
foreach ($students as $key => $item) { $num = $key + 2; $sheet->setCellValue('A' . $num, $num - 1); $sheet->setCellValue('B' . $num, $item->number); $sheet->setCellValue('C' . $num, $item->name); $sheet->setCellValue('D' . $num, $item->sex); $sheet->setCellValue('E' . $num, $item->date); }
- 将Excel表格输出并保存到本地
最后,将生成的Excel表格输出并保存到本地。在下面的代码中,我们使用writer来将Excel表格保存为.xlsx格式并输出到浏览器中。
$writer = new Xlsx($spreadsheet); $filename = "学生信息表.xlsx"; header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename=' . $filename); header('Cache-Control: max-age=0'); $writer->save('php://output'); exit();
三、Excel导入
除了导出Excel,ThinkPHP6还可以实现Excel的导入功能。接下来,我们将为您介绍ThinkPHP6中实现Excel导入的方法。
- 上传Excel文件并读取数据
在导入Excel之前,我们需要先上传Excel文件。在这里,我们使用了ThinkPHP6自带的文件上传类库。上传成功之后,使用PhpOfficePhpSpreadsheetIOFactory类库中的方法,读取上传的Excel文件中的数据。
use thinkacadeFilesystem; $uploadFile = request()->file('file'); $saveName = Filesystem::disk('public')->putFile('excel', $uploadFile); $filePath = Filesystem::disk('public')->path($saveName); $spreadsheet = PhpOfficePhpSpreadsheetIOFactory::load($filePath); $sheet = $spreadsheet->getActiveSheet(); $highestRow = $sheet->getHighestRow(); $data = [];
- 遍历Excel表格并得到数据
获取到Excel表格数据后,我们需要将数据遍历一遍,使用数组保存Excel表格的每一行数据。
for($i = 2; $i <= $highestRow; $i++){ $item['number'] = $sheet->getCellByColumnAndRow(1, $i)->getValue(); $item['name'] = $sheet->getCellByColumnAndRow(2, $i)->getValue(); $item['sex'] = $sheet->getCellByColumnAndRow(3, $i)->getValue(); $item['date'] = date('Y-m-d H:i:s',$sheet->getCellByColumnAndRow(4, $i)->getValue() - 25569)*86400; $data[] = $item; }
- 将Excel表格导入到数据库中
最后,我们将Excel表格中的数据导入到数据库中。在这里,我们使用ThinkPHP6自带的ORM操作,将数据保存到数据库表中。
use appcommonmodelStudent; Db::startTrans(); try { Student::insertAll($data); Db::commit(); } catch (Exception $e) { Db::rollback(); return json(['code' => '500', 'msg' => '操作失败']); } return json(['code' => '200', 'msg' => '操作成功']);
总结
本文详细介绍了如何使用ThinkPHP6实现Excel导入导出功能。通过使用PhpOfficePhpSpreadsheet类库,我们可以轻松地完成Excel相关的操作。Excel导入导出功能在企业管理系统中用到非常广泛,理解和掌握相关技能将会对开发者有很大的帮助。
本文共计1141个文字,预计阅读时间需要5分钟。
随着互联网的快速发展,Excel已经成为企业和个人日常办公中不可或缺的工具之一。因此,Excel的导入导出功能已成为了许多应用软件的必要组成部分。下面简要介绍如何使用ThinkPHP6实现Excel的导入导出功能:
1. 安装PHPExcel扩展库:首先,需要在项目中安装PHPExcel库。可以通过Composer进行安装: bash composer require phpoffice/phpexcel
2. 创建Excel文件:在ThinkPHP6中,可以使用PHPExcel库创建Excel文件。以下是一个简单的示例:
php use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet=new Spreadsheet(); $sheet=$spreadsheet->getActiveSheet();
// 设置 $sheet->setCellValue('A1', '姓名'); $sheet->setCellValue('B1', '年龄'); $sheet->setCellValue('C1', '性别');
// 添加数据 $sheet->setCellValue('A2', '张三'); $sheet->setCellValue('B2', 25); $sheet->setCellValue('C2', '男');
// 保存Excel文件 $writer=new Xlsx($spreadsheet); $writer->save('example.xlsx');
3. 读取Excel文件:同样,可以使用PHPExcel库读取Excel文件。以下是一个简单的示例:
php use PhpOffice\PhpSpreadsheet\IOFactory;
$spreadsheet=IOFactory::load('example.xlsx'); $sheetData=$spreadsheet->getActiveSheet()->toArray();
// 输出数据 print_r($sheetData);
4. 整合到ThinkPHP6控制器中:将上述代码整合到ThinkPHP6的控制器中,即可实现Excel的导入导出功能。
php public function excel() { // 创建Excel文件 $this->createExcel();
// 读取Excel文件 $data=$this->readExcel(); // 处理数据... }
通过以上步骤,您可以使用ThinkPHP6实现Excel的导入导出功能。需要注意的是,实际应用中可能需要根据具体需求对PHPExcel库进行扩展和修改。
随着互联网的快速发展,Excel已经成为公司和个人日常办公中重要的工具之一。因此,Excel导入导出的功能已经成为许多应用程序的必要组成部分。如何使用ThinkPHP6实现Excel导入导出呢?下面,本文将为您详细介绍。
一、ThinkPHP6系统环境验证
使用ThinkPHP6实现Excel导入导出,首先需要满足系统环境要求。在ThinkPHP6中,可以使用composer安装phpoffice/phpspreadsheet库,实现Excel处理功能。在命令行中执行以下命令进行安装:
composer require phpoffice/phpspreadsheet
安装完毕后,在controller层中引入PhpOfficePhpSpreadsheetSpreadsheet和PhpOfficePhpSpreadsheetWriterXlsx类库,这些类库将在我们的后面代码中使用。
二、Excel导出
- 导出Excel数据
使用ThinkPHP6实现Excel导出,首先需要将数据导入到Excel中。我们可以根据需要,在controller层中编写相应的代码。例如,在导出学生信息时,可以通过查询数据库获取相关信息。
use appcommonmodelStudent; public function export() { $students = Student::all(); }
- 设置Excel表格的头部
Excel的表格头部是非常重要的一部分。为了使Excel表格的头部清晰明了,我们可以编写代码来生成表格的头部信息。在下面的代码中,我们使用了循环来实现。
$spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setTitle('学生信息表'); $sheet->setCellValue('A1', '序号'); $sheet->setCellValue('B1', '学号'); $sheet->setCellValue('C1', '姓名'); $sheet->setCellValue('D1', '性别'); $sheet->setCellValue('E1', '日期');
- 将数据写入Excel表格
接下来,将获取的数据写入Excel表格中。循环遍历数据,将每个学生的信息依次写入表格中。
foreach ($students as $key => $item) { $num = $key + 2; $sheet->setCellValue('A' . $num, $num - 1); $sheet->setCellValue('B' . $num, $item->number); $sheet->setCellValue('C' . $num, $item->name); $sheet->setCellValue('D' . $num, $item->sex); $sheet->setCellValue('E' . $num, $item->date); }
- 将Excel表格输出并保存到本地
最后,将生成的Excel表格输出并保存到本地。在下面的代码中,我们使用writer来将Excel表格保存为.xlsx格式并输出到浏览器中。
$writer = new Xlsx($spreadsheet); $filename = "学生信息表.xlsx"; header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename=' . $filename); header('Cache-Control: max-age=0'); $writer->save('php://output'); exit();
三、Excel导入
除了导出Excel,ThinkPHP6还可以实现Excel的导入功能。接下来,我们将为您介绍ThinkPHP6中实现Excel导入的方法。
- 上传Excel文件并读取数据
在导入Excel之前,我们需要先上传Excel文件。在这里,我们使用了ThinkPHP6自带的文件上传类库。上传成功之后,使用PhpOfficePhpSpreadsheetIOFactory类库中的方法,读取上传的Excel文件中的数据。
use thinkacadeFilesystem; $uploadFile = request()->file('file'); $saveName = Filesystem::disk('public')->putFile('excel', $uploadFile); $filePath = Filesystem::disk('public')->path($saveName); $spreadsheet = PhpOfficePhpSpreadsheetIOFactory::load($filePath); $sheet = $spreadsheet->getActiveSheet(); $highestRow = $sheet->getHighestRow(); $data = [];
- 遍历Excel表格并得到数据
获取到Excel表格数据后,我们需要将数据遍历一遍,使用数组保存Excel表格的每一行数据。
for($i = 2; $i <= $highestRow; $i++){ $item['number'] = $sheet->getCellByColumnAndRow(1, $i)->getValue(); $item['name'] = $sheet->getCellByColumnAndRow(2, $i)->getValue(); $item['sex'] = $sheet->getCellByColumnAndRow(3, $i)->getValue(); $item['date'] = date('Y-m-d H:i:s',$sheet->getCellByColumnAndRow(4, $i)->getValue() - 25569)*86400; $data[] = $item; }
- 将Excel表格导入到数据库中
最后,我们将Excel表格中的数据导入到数据库中。在这里,我们使用ThinkPHP6自带的ORM操作,将数据保存到数据库表中。
use appcommonmodelStudent; Db::startTrans(); try { Student::insertAll($data); Db::commit(); } catch (Exception $e) { Db::rollback(); return json(['code' => '500', 'msg' => '操作失败']); } return json(['code' => '200', 'msg' => '操作成功']);
总结
本文详细介绍了如何使用ThinkPHP6实现Excel导入导出功能。通过使用PhpOfficePhpSpreadsheet类库,我们可以轻松地完成Excel相关的操作。Excel导入导出功能在企业管理系统中用到非常广泛,理解和掌握相关技能将会对开发者有很大的帮助。

