php - 表格导出导出

Xlswriter:

  • Xlswriter官网

  • xlswriter是一个 PHP C 扩展,可用于在 Excel 2007+ XLSX 文件中读取数据,插入多个工作表,写入文本、数字、公式、日期、图表、图片和超链接。

它具备以下特性:

  1. 写入
    100%兼容的Excel XLSX文件
    完整的Excel格式
    合并单元格
    定义工作表名称
    过滤器
    图表
    数据验证和下拉列表
    工作表PNG/JPEG图像
    用于写入大文件的内存优化模式
    适用于Linux,FreeBSD,OpenBSD,OS X,Windows
    编译为32位和64位
    FreeBSD许可证
    唯一的依赖是zlib

  2. 读取
    完整读取数据
    光标读取数据
    按数据类型读取
    安装
    1、 composer require viest/php-ext-xlswriter-ide-helper:dev-master


基本操作:

基本代码
例如:

封装下载方法

/**
     * 下载
     * @param $header
     * @param $data
     * @param $fileName
     * @param $type
     * @return bool
     * @throws
     */
    public static function download($header, $data, $fileName)
    {
        $config     = [
            'path' => self::getTmpDir() . '/',
        ];

        $now        = date('YmdHis');
        $fileName   = $fileName . $now . '.xlsx';
        $xlsxObject = new \Vtiful\Kernel\Excel($config);

        // Init File
        $fileObject = $xlsxObject->fileName($fileName);

        // 设置样式
        //$fileHandle = $fileObject->getHandle();
        //$format     = new \Vtiful\Kernel\Format($fileHandle);
//        $style      = $format->bold()->background(
//            \Vtiful\Kernel\Format::COLOR_YELLOW
//        )->align(\Vtiful\Kernel\Format::FORMAT_ALIGN_VERTICAL_CENTER)->toResource();

        // Writing data to a file ......
        $fileObject->header($header)
            ->data($data);
//            ->freezePanes(1, 0)
//            ->setRow('A1', 20);

        // Outptu
        $filePath = $fileObject->output();

        // 下载
        header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        header('Content-Disposition: attachment;filename="' . $fileName . '"');
        header('Cache-Control: max-age=0');
        header('Content-Length: ' . filesize($filePath));
        header('Content-Transfer-Encoding: binary');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        ob_clean();
        flush();
        if (copy($filePath, 'php://output') === false) {
            throw new RuntimeException('导出失败');
        }

        // Delete temporary file
        @unlink($filePath);

        return true;
    }

    /**
     * 获取临时文件夹
     * @return false|string
     */
    private static function getTmpDir()
    {
        // 目录可以自定义
        // return \Yii::$app->params['downloadPath'];

        $tmp = ini_get('upload_tmp_dir');

        if ($tmp !== False && file_exists($tmp)) {
            return realpath($tmp);
        }
        return realpath(sys_get_temp_dir());
    }

    /**
     * 读取文件
     * @param $path
     * @param $fileName
     * @return array
     */
    public static function readFile($path,$fileName)
    {
        // 读取测试文件
        $config = ['path' => $path];
        $excel  = new \Vtiful\Kernel\Excel($config);
        $data   = $excel->openFile($fileName)
            ->openSheet()
            ->getSheetData();
        return $data;
    }

调用

public function export()
    {
        $fields = [
            'sku_code'   => 'SKU编码',
            'bar_code'   => '条形码',
        ];

        $s = ['ad'=>'sdf','b'=>'fsdf'];

        for ($i=0;$i<=10;$i++){
            $formatData[] =$s;
        }

        $formatData =  array_map(function ($i){return array_values($i);},$formatData);
        Export::download(array_values($fields), $formatData, 'sku_single_code');

    }```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

荔枝学习

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

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

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

打赏作者

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

抵扣说明:

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

余额充值