php使用Xlswriter扩展实现Excel导出功能

1.windows

点击链接后,选择对应的版本即可:

Releases · viest/php-ext-xlswriter · GitHub

注:32位系统用x64,64位系统用x86,以及对应的php版本,需要注意的是ts和nts之分

2.Linux

实际应用场景中,xlswriter和php替换为对应版本即可,extension后面文件路径根据实际情况而定

流程

cd /usr/local
wget https://pecl.php.net/get/xlswriter-1.5.2.tgz
tar xf xlswriter-1.5.2.tgz
cd xlswriter-1.5.2
/www/server/php/74/bin/phpize
./configure --with-php-config=/www/server/php/74/bin/php-config --enable-reader
make && make install
echo "extension =/www/server/php/74/lib/php/extensions/no-debug-non-zts-20190902/xlswriter.so" >> /www/server/php/74/etc/php.ini
echo "extension =/www/server/php/74/lib/php/extensions/no-debug-non-zts-20190902/xlswriter.so" >> /www/server/php/74/etc/php-cli.ini


最后重启php即可。

示例:

        //导出
        $ExcelTmpDir='upload';
        $fileName = '表格名称';
        $dirName = date('Ymd');//目录名
        if(!opendir($ExcelTmpDir.'/'.$dirName)){
            $ret=mkdir($ExcelTmpDir.'/'.$dirName);
            if(!$ret)die('directory create faild!');
        }

        $config = [
            'path' => $ExcelTmpDir.'/'.$dirName // xlsx文件保存路径
        ];
        $fileObject  = new \Vtiful\Kernel\Excel($config);

        $fileObject = $fileObject->fileName($fileName);
        $fileHandle = $fileObject->getHandle();

        //加粗 背景蓝色
        $formatTitle = new \Vtiful\Kernel\Format($fileHandle);
        $TitleStyle = $formatTitle->background(0X8064A2)->Bold()->fontColor(0xffffff)->align(\Vtiful\Kernel\Format::FORMAT_ALIGN_VERTICAL_CENTER)->toResource();

        $title = array('序号', '年级', '院系', '专业', '班级', '学号', '姓名');

        $textFile = $fileObject->header($title);
        //设置列的宽度
        $textFile->setRow('A1:A1', 18,$TitleStyle);
        $textFile->setColumn('C:E', 20);
        $textFile->setColumn('F1:F1', 18);
        $textFile->setColumn('G1:G1', 14);
        $textFile->setColumn('H1:H1', 22);
        $textFile->setColumn('I1:I1', 14);
        $textFile->setColumn('J1:J1', 100);
        $textFile->freezePanes(1, 0);

        $index = 0;
            foreach ($dataArr as $row)
            {
                $textFile->insertText($index+1, 0, $index+1);
                $textFile->insertText($index+1, 1, $row["grade"]); // #,##0 为单元格数据样式
                $textFile->insertText($index+1, 2, $row["collegeName"]); // #,##0 为单元格数据样式
                $textFile->insertText($index+1, 3, $row["major"]); // #,##0 为单元格数据样式
                $textFile->insertText($index+1, 4, empty($row["className"])?'':(strpos($row["className"],'班')?$row["className"]:$row["className"].'班')); // #,##0 为单元格数据样式
                $textFile->insertText($index+1, 5, $row["studentID"]); // #,##0 为单元格数据样式
                $textFile->insertText($index+1, 6, $row["studentName"]); // #,##0 为单元格数据样式
                $index++;
            }


        $textFile->output();
        header("Location: /".$ExcelTmpDir.'/'.$dirName.'/'.$fileName);

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cq林志炫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值