think PHP导入导出excel

本地环境

think PHP5,PhpOffice/PhpSpreadsheet,composer
PHP版本7.4,这个插件的最低版本要求7.2
配置PhpSpreadsheet
官网:https://phpspreadsheet.readthedocs.io/en/stable/

composer require phpoffice/phpspreadsheet

数据库与excel表

在这里插入图片描述
在这里插入图片描述

导入文件

思路:首先读取excel文件数据,将他存放在缓存中,然后按照数据库的插入指令,插入数据

测试

public function upexceltest()
    {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        $sheet->setCellValue('A1', 'Welcome to Helloweba.');
        $writer = new Xlsx($spreadsheet);
        $path = ROOT_PATH.'public'.DS.'uploads'.DS.'excel';
        $writer->save($path.'/'.'hello.xlsx');//默认保存文件到public
        dump($writer);

    }

这段代码会产生一个excel文件

存缓存

新建phpspreadsheet实例

use think\Db;
use think\Request;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

获取数据,移动获取到的excel文件到本地目录

//正式使用excel上传
    //读取excel数据然后将数据组装成sql语句,传递进mysql中
    //第一步,读取excel信息,将数据中需要的预处理提取出来
    //第二部,将预处理数据进行封装成sql数据,使用模型传入数据库
    public function upexcel(Request $request)
    {
        // 判断请求类型是否为POST
        if ($request->isPost()) {
            // 获取上传的文件信息
            $file = $request->file('file');
            if (empty($file)) {
                $this->error('请选择要上传的文件!');
            }
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'.'/excel');
            if ($info) {
                // 获取文件路径
                $filePath = $info->getSaveName();

开始实例化导入

try {
                    // 使用PHPSpreadsheet读取Excel文件
                    $reader = IOFactory::createReader('Xlsx');
                    $reader->setReadDataOnly(true);
                    $spreadsheet = $reader->load(ROOT_PATH . 'public' . DS . 'uploads'.'/excel' . DS . $filePath);
                    $worksheet = $spreadsheet->getActiveSheet();//获取excel中的活动表
                    $highestRow = $worksheet->getHighestRow(); // 总行数
                    $highestColumn = $worksheet->getHighestColumn(); // 总列数
                    $highestColumnIndex = Coordinate::columnIndexFromString($highestColumn); // 转换为数字索引
                    $lines = $highestRow - 1; // 减1是因为Excel的行号从1开始,实际数据行应从第2行开始计算
                    if ($lines <= 0) {
                        $this->error('Excel表格中没有数据!');
                    } else {

将数据提取出来成数组

$rowData = []; // 初始化二维数组,在外部循环之前定义

                        for ($row = 2; $row <= $highestRow; ++$row) {
                            // 为每一行初始化一个新的数据数组
                            $currentRowData = [];

                            for ($col = 1; $col <= $highestColumnIndex; ++$col) {
                                // 使用较新的 getCell() 方法,但首先确认您的PhpSpreadsheet版本
                                $cellAddress = Coordinate::stringFromColumnIndex($col) . $row; // 转换列索引为列字母
                                $cell = $worksheet->getCell($cellAddress);
                                $currentRowData[] = $cell->getValue(); // 将当前单元格的值添加到该行数据中
                            }
                            // 将当前行的所有数据作为一个数组添加到 rowData
                            $rowData[] = $currentRowData;
                        }
                        // 现在,$rowData 是一个二维数组,每个内部数组代表一行的数据
//                        return json_encode($rowData); // 打印出来检查结果
                        //将二维数组转换为键值对的数据,姓名:mmm,语文:23
//                        foreach($rowData as $key=>$value){
//                            foreach ($rowData[])
//                        }

将数组提取出来组合成sql需要的数组批量导入数据库

$newData = [];
                        foreach($rowData as $row){
                            $newRow = ['name' => $row[0], 'chinese' => $row[1],'math'=>$row[2],'english'=>$row[3]];
                            $newData[] = $newRow;
                        }
                        $data = model('student')->insertAll($newData);
                        if ($data){
                            return json_encode(['code'=>200,'msg'=>'Excel数据导入成功!']);
                        }
                    }
                } catch (\Exception $e) {
                    return "导入失败";
                };
            }
        }
    }

完整代码

//正式使用excel上传
    //读取excel数据然后将数据组装成sql语句,传递进mysql中
    //第一步,读取excel信息,将数据中需要的预处理提取出来
    //第二部,将预处理数据进行封装成sql数据,使用模型传入数据库
    public function upexcel(Request $request)
    {
        // 判断请求类型是否为POST
        if ($request->isPost()) {
            // 获取上传的文件信息
            $file = $request->file('file');
            if (empty($file)) {
                $this->error('请选择要上传的文件!');
            }
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'.'/excel');
            if ($info) {
                // 获取文件路径
                $filePath = $info->getSaveName();

                try {
                    // 使用PHPSpreadsheet读取Excel文件
                    $reader = IOFactory::createReader('Xlsx');
                    $reader->setReadDataOnly(true);
                    $spreadsheet = $reader->load(ROOT_PATH . 'public' . DS . 'uploads'.'/excel' . DS . $filePath);
                    $worksheet = $spreadsheet->getActiveSheet();//获取excel中的活动表
                    $highestRow = $worksheet->getHighestRow(); // 总行数
                    $highestColumn = $worksheet->getHighestColumn(); // 总列数
                    $highestColumnIndex = Coordinate::columnIndexFromString($highestColumn); // 转换为数字索引
                    $lines = $highestRow - 1; // 减1是因为Excel的行号从1开始,实际数据行应从第2行开始计算
                    if ($lines <= 0) {
                        $this->error('Excel表格中没有数据!');
                    } else {
                        // 这里可以添加读取每行数据的逻辑,例如:
                        $rowData = []; // 初始化二维数组,在外部循环之前定义

                        for ($row = 2; $row <= $highestRow; ++$row) {
                            // 为每一行初始化一个新的数据数组
                            $currentRowData = [];

                            for ($col = 1; $col <= $highestColumnIndex; ++$col) {
                                // 使用较新的 getCell() 方法,但首先确认您的PhpSpreadsheet版本
                                $cellAddress = Coordinate::stringFromColumnIndex($col) . $row; // 转换列索引为列字母
                                $cell = $worksheet->getCell($cellAddress);
                                $currentRowData[] = $cell->getValue(); // 将当前单元格的值添加到该行数据中
                            }
                            // 将当前行的所有数据作为一个数组添加到 rowData
                            $rowData[] = $currentRowData;
                        }
                        // 现在,$rowData 是一个二维数组,每个内部数组代表一行的数据
//                        return json_encode($rowData); // 打印出来检查结果
                        //将二维数组转换为键值对的数据,姓名:mmm,语文:23
//                        foreach($rowData as $key=>$value){
//                            foreach ($rowData[])
//                        }
                        $newData = [];
                        foreach($rowData as $row){
                            $newRow = ['name' => $row[0], 'chinese' => $row[1],'math'=>$row[2],'english'=>$row[3]];
                            $newData[] = $newRow;
                        }
                        $data = model('student')->insertAll($newData);
                        if ($data){
                            return json_encode(['code'=>200,'msg'=>'Excel数据导入成功!']);
                        }
                    }
                } catch (\Exception $e) {
                    return "导入失败";
                };
            }
        }
    }

导出数据

从数据库导出数据
思路:通过sql语句查询数据,转化成二维数组,二维数组之后按照foreach循环将数据按照要求传入excel表

实例化excel表格

//导出数据,第一步,导出数据库,第二部通过数据库导出成excel数据
    public function downexcel()
    {

        //设置表数据
        $spreadsheet = new Spreadsheet();
        $worksheet = $spreadsheet->getActiveSheet();
        //设置工作表标题名称
        $worksheet->setTitle('学生成绩表');
        //将其存入excel表
        //表头
        设置单元格内容
        $worksheet->setCellValue('A1',  '学生成绩表');
        $worksheet->setCellValue('A2','姓名');
        $worksheet->setCellValue('B2',  '语文');
        $worksheet->setCellValue('C2',  '数学');
        $worksheet->setCellValue('D2', '外语');
        $worksheet->setCellValue('E2',  '总分');
        //合并单元格
        $worksheet->mergeCells('A1:E1');

        $styleArray = [
            'font' => [
                'bold' => true
            ],
            'alignment' => [
                'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
            ],
        ];
        //设置单元格样式
        $worksheet->getStyle('A1')->applyFromArray($styleArray)->getFont()->setSize(28);
        $worksheet->getStyle('A2:E2')->applyFromArray($styleArray)->getFont()->setSize(14);

查询数据库数据,并按要求写入数据库

$db = Db::query('select id,name,chinese,math,english from fa_student');
     $len = count($db);
     $j = 0;
     for($i=0;$i<$len;$i++){
         $j = $i+3;//从第三行开始
         $data = $db[$i];
         $worksheet->setCellValue('A'.$j,$data['name']);//A3,第一行
         $worksheet->setCellValue('B'.$j,$data['chinese']);//A3,第一行
         $worksheet->setCellValue('C'.$j,$data['math']);//A3,第一行
         $worksheet->setCellValue('D'.$j,$data['english']);//A3,第一行
         $worksheet->setCellValue('E'.$j,$data['chinese'] + $data['math'] + $data['english']);//
     }

设置excel格的式

 //设置外边框
      $styleArrayBody = [
          'borders' => [
              'allBorders' => [
                  'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                  'color' => ['argb' => '666666'],
              ],
          ],
          'alignment' => [
              'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
          ],
      ];
      $total_rows = $len + 2;
//添加所有边框/居中
      $worksheet->getStyle('A1:E'.$total_rows)->applyFromArray($styleArrayBody);

打印数据

       //保存数据并使用浏览器打印出来
//        $filename = '成绩表.xlsx';
//        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
//        header('Content-Disposition: attachment;filename="'.$filename.'"');
//        header('Cache-Control: max-age=0');
//        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
        //这行代码会将生成的Excel文件内容直接输出到HTTP响应体中,这意味着它将作为下载的形式提供给客户端(比如浏览器),或者如果是在命令行脚本中运行,则会输出到标准输出流。
//        $writer->save('php://output');//如果不需要浏览器打开就保存excel文件的话,请注释掉这行代码

        //保存数据直接保存为excel,好像没啥区别
        $filename = '成绩表.xlsx';
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$filename.'"');
        header('Cache-Control: max-age=0');
        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
    }

完整代码

    //导出数据,第一步,导出数据库,第二部通过数据库导出成excel数据
    public function downexcel()
    {

        //设置表数据
        $spreadsheet = new Spreadsheet();
        $worksheet = $spreadsheet->getActiveSheet();
        //设置工作表标题名称
        $worksheet->setTitle('学生成绩表');
        //将其存入excel表
        //表头
        设置单元格内容
        $worksheet->setCellValue('A1',  '学生成绩表');
        $worksheet->setCellValue('A2','姓名');
        $worksheet->setCellValue('B2',  '语文');
        $worksheet->setCellValue('C2',  '数学');
        $worksheet->setCellValue('D2', '外语');
        $worksheet->setCellValue('E2',  '总分');
        //合并单元格
        $worksheet->mergeCells('A1:E1');

        $styleArray = [
            'font' => [
                'bold' => true
            ],
            'alignment' => [
                'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
            ],
        ];
        //设置单元格样式
        $worksheet->getStyle('A1')->applyFromArray($styleArray)->getFont()->setSize(28);
        $worksheet->getStyle('A2:E2')->applyFromArray($styleArray)->getFont()->setSize(14);
        $db = Db::query('select id,name,chinese,math,english from fa_student');
        $len = count($db);
        $j = 0;
        for($i=0;$i<$len;$i++){
            $j = $i+3;//从第三行开始
            $data = $db[$i];
            $worksheet->setCellValue('A'.$j,$data['name']);//A3,第一行
            $worksheet->setCellValue('B'.$j,$data['chinese']);//A3,第一行
            $worksheet->setCellValue('C'.$j,$data['math']);//A3,第一行
            $worksheet->setCellValue('D'.$j,$data['english']);//A3,第一行
            $worksheet->setCellValue('E'.$j,$data['chinese'] + $data['math'] + $data['english']);//
        }

        //设置外边框
        $styleArrayBody = [
            'borders' => [
                'allBorders' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                    'color' => ['argb' => '666666'],
                ],
            ],
            'alignment' => [
                'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
            ],
        ];
        $total_rows = $len + 2;
//添加所有边框/居中
        $worksheet->getStyle('A1:E'.$total_rows)->applyFromArray($styleArrayBody);

        //保存数据并使用浏览器打印出来
//        $filename = '成绩表.xlsx';
//        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
//        header('Content-Disposition: attachment;filename="'.$filename.'"');
//        header('Cache-Control: max-age=0');
//        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
        //这行代码会将生成的Excel文件内容直接输出到HTTP响应体中,这意味着它将作为下载的形式提供给客户端(比如浏览器),或者如果是在命令行脚本中运行,则会输出到标准输出流。
//        $writer->save('php://output');//如果不需要浏览器打开就保存excel文件的话,请注释掉这行代码

        //保存数据直接保存为excel,好像没啥区别
        $filename = '成绩表.xlsx';
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$filename.'"');
        header('Cache-Control: max-age=0');
        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
    }

原作者连接
https://blog.csdn.net/ice_mocha/article/details/116460057
更详细

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用ThinkPHP框架和PHPExcel类库导出Excel表格的步骤: 1.下载并安装PHPExcel类库,将下载的PHPExcel文件夹放入项目的vendor目录下。 2.创建一个控制器,例如ExcelController,并在控制器中添加一个export方法。 3.在export方法中,实例化PHPExcel类库,并设置Excel表格的属性,例如表格标题、表格列名等。 4.从数据库中获取需要导出的数据,并将数据填充到Excel表格中。 5.将Excel表格输出到浏览器,让用户可以下载。 以下是一个示例代码: ```php <?php namespace app\index\controller; use think\Controller; use PHPExcel; use PHPExcel_IOFactory; class ExcelController extends Controller { public function export() { // 实例化PHPExcel类库 $objPHPExcel = new PHPExcel(); // 设置Excel属性 $objPHPExcel->getProperties()->setCreator("ThinkPHP") ->setLastModifiedBy("ThinkPHP") ->setTitle("Office 2007 XLSX Test Document") ->setSubject("Office 2007 XLSX Test Document") ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.") ->setKeywords("office 2007 openxml php") ->setCategory("Test result file"); // 设置表格标题 $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', '姓名') ->setCellValue('B1', '年龄') ->setCellValue('C1', '性别'); // 从数据库中获取数据 $data = db('user')->select(); // 将数据填充到Excel表格中 $i = 2; foreach ($data as $item) { $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A' . $i, $item['name']) ->setCellValue('B' . $i, $item['age']) ->setCellValue('C' . $i, $item['gender']); $i++; } // 输出Excel表格到浏览器 header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="user.xlsx"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save('php://output'); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值