phalapi2.x 使用composer 使用 第三方excel 实现导入导出

本文介绍了如何在PhalAPI2.x框架中使用Composer来集成phpspreadsheet库,实现Excel文件的导入导出功能。首先,详细讲解了PhalAPI2.x框架的安装和Composer的配置,接着阐述了通过命令行或PHPStorm的Terminal两种方式安装phpspreadsheet插件的过程。最后,提到了在项目中引用该库并提供了导入导出的相关代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.安装  phalapi2.x 框架,直接下载一个空框架  

2.安装composer    官网入门介绍里 有个exe文件直接下载 安装其他比较麻烦, 安装完成后运行cmd  输入 composer -v  成功的话会显示composer信息

3.导入第三方插件 的2方法 (phpexcel  已经被抛弃    phpoffice/phpspreadsheet可用)

      一。cmd里面直接cd到项目路径  然后运行 composer require phpoffice/phpspreadsheet

     二。phpstorm 里打开Terminal     在对应项目路径 运行  composer require phpoffice/phpspreadsheet

 

4.引用

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

 

5.导入导出部分代码

public function getRules() {
       return array(
           'index' => array(
               'username' => array('name' => 'username',  'desc' => '用户名字'),
           ),
           'importExcel' => array(
               'excel' => array(
                   'name' => 'upfile',
                   'type' => 'file',
               )
           ),
       );
}
/**
 * 导入excel接口
 * @desc 把excel文件导入到数据库
 * @return int result  导入结果
 */
public function importExcel() {
    $model = new ModelCard();
    $filename = $this->excel['tmp_name'];

    $excel = IOFactory::load($filename);

    $sheet = $excel->getSheet(0);
    $highestRow = $sheet->getHighestRow();
    //$import_data = array();

    //循环导入
    $count = 0;
    for($i = 2; $i <= $highestRow; $i++) {
        $data = array(
            'card_code'   => $sheet->getCellByColumnAndRow(1, $i)->getValue(),
            'card_pwd'   => (string)$sheet->getCellByColumnAndRow(2, $i)->getValue(),
            'gift_id'   => $sheet->getCellByColumnAndRow(3, $i)->getValue(),
        );
        $result = $model->insert($data);
       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值