PHP_XLSXWriter 数据库输出辅助类

为了减轻后期开发的麻烦,写了这个类,用以简化输出配置,自动合并表头样式

PHP_XLSXWriter : https://github.com/mk-j/PHP_XLSXWriter

先看例子(test.php):
vendor('XLSXWriter.Helper');
$ds=M('kaoqin_tongji')->where("BMID=111 and YueFen='2021-01'")->select();
$writer = new \XLSXWriterHelper();
$writer->writeToStdOutX('统计导出', [['-', '统计导出', [
    ['XH', '序号', 8],
    ['BM', '部门', 16],
    ['XM', '姓名', 12],
    ['YCQ', '应出勤', 8],
    ['SCQ', '实际出勤', 10],
    ['SDK', '实际打卡', 10],
    ['CC', '出差天数', 10],
    ['JX', '计薪天数', 10],
    ['ZMJB', '周末加班', 10],
    ['JRJB', '节日加班', 10],
    ['-', '请休假', [
        ['NJ', '年假', 6],
        ['HJ', '婚假', 6],
        ['PCJ', '陪产假', 8],
        ['SAJ', '丧假', 6],
        ['CJ', '产假', 6],
        ['GSJ', '工伤假', 8],
        ['SJ', '事假', 6],
        ['BJ', '病假', 6],
    ]],
    ['-', '夜值', [
        ['YZ1', '夜值A', 8],
        ['YZ2', '夜值B', 8],
    ]],
]]], $ds);

导出效果

辅助类 Helper.php:
<?php

require_once 'XLSXWriter.class.php';

class XLSXWriterHelper
{
    private function getHdsWidths($hds)
    {
        $re = [];
        foreach ($hds as $hid => $h) {
            foreach ($h as $lid => $l) {
                if (array_key_exists(1, $l) && (int) $l[1] > 0) {
                    $re[$lid] = (int) $l[1];
                }
            }
        }

        return $re;
    }

    private function getHdsRowFs($hds)
    {
        $re = [];
        foreach ($hds as $hid => $h) {
            foreach ($h as $lid => $l) {
                if (array_key_exists(2, $l)) {
                    $re[$lid] = $l[2];
                }
            }
        }

        return $re;
    }

    private function getHdsRowDs($hds)
    {
        $re = [];
        $lm = 0;
        foreach ($hds as $hid => $h) {
            $ls = max(array_keys($h));
            if ($ls > $lm) {
                $lm = $ls;
            }
        }
        foreach ($hds as $hid => $h) {
            $re[$hid] = array_pad([], $lm, '');
            foreach ($h as $lid => $l) {
                $re[$hid][$lid] = $l[0];
            }
        }

        return $re;
    }

    private function getHdsMergeInfo($hds)
    {
        $re = [];
        foreach ($hds as $hid => $h) {
            foreach ($h as $lid => $l) {
                if (array_key_exists('merge', $l)) {
                    $re[] = $l['merge'];
                } else {
                    if (!empty($l[0]) && $hid < (count($hds) - 1) && $hds[$hid + 1][$lid][0] == '') {
                        $re[] = [$hid, $lid, $hid + 1, $lid];
                    }
                }
            }
        }

        return $re;
    }

    private function config2HDS($config, &$hds, $h = 0, $l = 0)
    {
        $hs = [];
        $i = -1;
        foreach ($config as $v) {
            ++$i;
            if (!array_key_exists($h, $hds)) {
                $hds[$h] = [];
            }

            if ($v[0] == '-') {
                $w = self::config2HDS($v[2], $hds, $h + 1, $l + $i);
                $hds[$h][$l + $i] = [$v[1], 'merge' => [$h, $l + $i, $h, $l + $i + $w]];
                for ($k = 1; $k <= $w; ++$k) {
                    $hds[$h][$l + $i + $k] = [''];
                }
                $i += $w;
            } else {
                $hds[$h][$l + $i] = [$v[1], $v[2], $v[0]];
            }
        }

        return $i;
    }

    public static function writeToStdOutX($filename = '导出', $config = [], $ds = [])
    {
        ob_end_clean();
        ob_start();
        header('Content-Disposition:attachment;filename='.$filename.'.xlsx');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Transfer-Encoding: binary');
        header('Cache-Control: must-revalidate');
        header('Pragma: no-cache');
        header('Expires: 0');
        $writer = new \XLSXWriter();
        $writer->setTempDir('./Uploadfile/temp');
        $style = ['border' => 'left,right,top,bottom', 'border-style' => 'thin', 'valign' => 'center', 'wrap_text' => 'true', 'height' => 20];

        $hds = [];
        self::config2HDS($config, $hds);
        $hd = self::getHdsRowDs($hds);
        $hw = self::getHdsWidths($hds);
        $hf = self::getHdsRowFs($hds);
        $hm = self::getHdsMergeInfo($hds);
        $hs = [
            'suppress_row' => true,
            'widths' => $hw,
        ];
        $writer->writeSheetHeader('Sheet1', array_pad([], count($hw), 'string'), $col_options = $hs);
        foreach ($hd as $dhi => $dh) {
            $writer->writeSheetRow('Sheet1', $dh, array_merge($style, $dhi == 0 ? ['halign' => 'center',  'font-style' => 'bold'] : ['halign' => 'center']));
        }

        $data = [];
        $i = 0;

        foreach ($ds as $d) {
            $line = [++$i];

            foreach ($hf as $hk => $v) {
                $line[$hk] = $d[$v];
            }
            $data[] = $line;
        }

        foreach ($data as $d) {
            $writer->writeSheetRow('Sheet1', $d, $style);
        }
        foreach ($hm as $mg) {
            $writer->markMergedCell('Sheet1', $start_row = $mg[0], $start_col = $mg[1], $end_row = $mg[2], $end_col = $mg[3]);
        }
        $writer->writeToStdOut();
        die;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHP_XLSXWriter是一个轻量级的PHP库,用于生成XLSX格式的电子表格文件。使用它可以代替PHPExcel。 下面是一个生成带有样式的电子表格文件的示例: ```php require_once 'xlsxwriter.class.php'; // 创建一个新的工作表 $writer = new XLSXWriter(); $writer->writeSheetHeader('Sheet1', array('Name', 'Age', 'Gender', 'Country'), array( 'widths' => array(20, 10, 15, 20), 'freeze_rows' => 1, 'font' => 'Arial', 'font-size' => 12, 'font-style' => 'bold', 'fill' => '#DCE6F1', 'border' => 'left,right,top,bottom', 'border-style' => 'thin', 'border-color' => '#000000' )); // 添加数据行 $writer->writeSheetRow('Sheet1', array('John Doe', 32, 'Male', 'USA'), array( 'font' => 'Arial', 'font-size' => 11, 'border' => 'left,right,bottom', 'border-style' => 'thin', 'border-color' => '#000000' )); $writer->writeSheetRow('Sheet1', array('Jane Smith', 28, 'Female', 'Canada'), array( 'font' => 'Arial', 'font-size' => 11, 'border' => 'left,right,bottom', 'border-style' => 'thin', 'border-color' => '#000000' )); // 输出电子表格文件 $writer->writeToFile('example.xlsx'); ``` 在上面的示例中,我们使用`writeSheetHeader`方法来定义表头,并且指定了一些样式属性,例如宽度、冻结行、字体、填充颜色和边框。 然后,我们使用`writeSheetRow`方法来添加数据行,并且为每行指定了一些样式属性,例如字体、边框等。 最后,我们使用`writeToFile`方法将生成的电子表格文件保存到磁盘上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值