如何使用ThinkPHP6实现Excel导入导出

 1700

随着互联网的快速发展,Excel已经成为公司和个人日常办公中重要的工具之一。因此,Excel导入导出的功能已经成为许多应用程序的必要组成部分。如何使用ThinkPHP6实现Excel导入导出呢?下面,本文将为您详细介绍。

一、ThinkPHP6系统环境验证

使用ThinkPHP6实现Excel导入导出,首先需要满足系统环境要求。在ThinkPHP6中,可以使用composer安装phpoffice/phpspreadsheet库,实现Excel处理功能。在命令行中执行以下命令进行安装:

composer require phpoffice/phpspreadsheet

安装完毕后,在controller层中引入PhpOfficePhpSpreadsheetSpreadsheet和PhpOfficePhpSpreadsheetWriterXlsx类库,这些类库将在我们的后面代码中使用。

二、Excel导出

1、导出Excel数据

使用ThinkPHP6实现Excel导出,首先需要将数据导入到Excel中。我们可以根据需要,在controller层中编写相应的代码。例如,在导出学生信息时,可以通过查询数据库获取相关信息。

use appcommonmodelStudent;
 
public function export()
{
    $students = Student::all();
}

2、设置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', '日期');

3、将数据写入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);
}

4、将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导入的方法。

1、上传Excel文件并读取数据

在导入Excel之前,我们需要先上传Excel文件。在这里,我们使用了ThinkPHP6自带的文件上传类库。上传成功之后,使用PhpOfficePhpSpreadsheetIOFactory类库中的方法,读取上传的Excel文件中的数据。

use think/acade/Filesystem;
 
$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 = [];

2、遍历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;
}

3、将Excel表格导入到数据库中

最后,我们将Excel表格中的数据导入到数据库中。在这里,我们使用ThinkPHP6自带的ORM操作,将数据保存到数据库表中。

use app/common/model/Student;
 
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导入导出功能在企业管理系统中用到非常广泛,理解和掌握相关技能将会对开发者有很大的帮助。


本文网址:https://www.zztuku.com/detail-14215.html
站长图库 - 如何使用ThinkPHP6实现Excel导入导出
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐

    电子商务网站管理员后台UI