The effect

demand

In order to achieve report effect, their own requirements.

Basically is train of thought, train of thought realized other effect also OK.

Figure out how late everyone is and how early they leave every day of the year.

Train of thought

Implement in PHP language.

First, the report style is implemented in HTML,

The XLS download is then generated using the PHP header function.

knowledge

  • Column merge and row merge in a table

  • PHP takes every day of the year for display

  • PHP header function

  • Smarty template function

  • Smarty custom functions

  • .

The PHP code

public function export() {// Get the date of 2016$time_start = strtotime('2016-01-01');
    $time_end   = strtotime('2016-12-31');

    $month_arr = [];
    $month_arr['month'=] []'the 2016-01';
    $month_arr['numbers'][] = date(t,$time_start); // Obtain the number of dayswhile (($time_start = strtotime('+1 month'.$time_start)) < =$time_end) {
        $month_arr['month'][]   = date('Y-m'.$time_start); // get incremental months$month_arr['numbers'][] = date(t,$time_start); // Get days}function check_week($time = [])
    {
        if (empty($time['day']) {return ' ';
        }
        $w = intval(date('w' , strtotime($time['day')));if( $w= = = 0 | |$w= = = 6) {return ' '.date('d', strtotime($time['day'])).'</td>';
        }
        return '<td>'.date('d', strtotime($time['day'])).'</td>'; } // Register a function with the template$this->smarty->registerPlugin('function'.'check_week'.'check_week'); // The simulation data is as follows:$list[0] ['name'] = 'Tom';
    $list[1] ['name'] = 'Joan';

    $list[0] ['sex'] = 'male';
    $list[1] ['sex'] = 'woman';

    $list[0] ['age'] = '30';
    $list[1] ['age'] = 'and'; // Set to be late$list[0] ['late'] = [
        '2016-01-08'.'2016-01-09'.'2016-02-09'.'2016-03-09'.'2016-04-09'.'2016-05-09'
    ];

    $list[1] ['late'] = [
        '2016-02-12'.'2016-03-15'.'2016-04-13'.'2016-05-19'.'2016-05-19']; // Set to leave early$list[0] ['leave'] = [
        '2016-03-09'.'2016-04-11'.'2016-05-15'.'2016-06-18'.'2016-07-21'.'2016-08-23'.'2016-09-22'.'2016-10-20'.'2016-11-17'.'2016-12-14',];$list[1] ['leave'] = [
        '2016-05-09'.'2016-06-11'.'2016-07-13'.'2016-08-15'.'2016-09-17'.'2016-10-19'.'2016-11-20'.'2016-12-23'.'2016-03-18'.'2016-02-19'.'2016-01-23',];$file_name   = "The report -".date("YmdHis",time());
    $file_suffix = "xls";
    header("Content-Type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=$file_name.$file_suffix");
    $this->_assign('list'.$list);
    $this->_assign('month'.$month_arr);
    $this->_display();
}
Copy the code

The HTML code

<html xmlns:o="urn:schemas-microsoft-com:office:office"
      xmlns:x="urn:schemas-microsoft-com:office:excel"
      xmlns="http://www.w3.org/TR/REC-html40">

<head>
    <meta http-equiv=Content-Type content="text/html; charset=utf-8">
    <meta name=ProgId content=Excel.Sheet>
    <meta name=Generator content="Microsoft Excel 11">
</head>

<body>
<table border=1 cellpadding=0 cellspacing=0 width="100%">
    <tr>
        <td style="vertical-align:middle;" align="center" rowspan="2"< span style= "max-width: 100%; clear: both; min-height: 1em"vertical-align:middle;" align="center" rowspan="2"< span style= "max-width: 100%; clear: both"vertical-align:middle;" align="center" rowspan="2"><b> Age </b></ TD > {if $month}
            {foreach $month.month as $k= >$m}
                <td style="text-align: center;" colspan="{$month.numbers.$k}"><b>{$m}</b></td>
            {/foreach}
        {/if}
    </tr>
    <tr>
        {if $month}
        {foreach $month.month as $k= >$m}
            {section name=count loop=$month.numbers.$k+1 start=1}
                {check_week day=$m|cat:"-"|cat:$smarty.section.count.index}
            {/section}
        {/foreach}
        {/if}
    </tr>

    {if $list}
    {foreach $list as $s}
    <tr>
        <td>{$s.name|default:The '-'}</td>
        <td>{$s.sex|default:The '-'}</td>
        <td>{$s.age|default:The '-'}</td>
        {if $month}
        {foreach $month.month as $k= >$m}
            {section name=count loop=$month.numbers.$k+1 start=1}
                {if $smarty.section.count.index <10 }
                     {$str = ""}
                     {$smarty.section.count.index = $str|cat:"0"|cat:$smarty.section.count.index}
                {/if}
                <td style="
                    {if $s['late']}
                        {if ($m|cat:"-"|cat:$smarty.section.count.index)|in_array:$s['late']}
                            background-color: #5a0099;
                        {/if}
                    {/if}

                    {if $s['leave']}
                        {if ($m|cat:"-"|cat:$smarty.section.count.index)|in_array:$s['leave']}
                            background-color: yellow;
                        {/if}
                    {/if}
                ">

                {if $s['late']}
                    {if ($m|cat:"-"|cat:$smarty.section.count.index)|in_array:$s['late']} {/if}
                {/if}

                {if $s['leave']}
                    {if ($m|cat:"-"|cat:$smarty.section.count.index)|in_array:$s['leave']} {/if}
                {/if}

                </td>

            {/section}
        {/foreach}
        {/if}
    </tr>
    {/foreach}
    <tr>
        <td style="background-color: red"></td> <td>* weekend </ TD ></ tr> <tr> < TD style= "box-sizing: border-box! Important"background-color: white"></td> <td>* Normal </td> </tr> <tr> <td style= "box-sizing: border-box! Important; word-wrap: break-word! Important"background-color: #5a0099"></td> <td>* late </td> </tr> <tr> <td style= "box-sizing: border-box! Important; word-wrap: break-word! Important"background-color: yellow"></ TD >< TD >* Early exit </ TD ></ tr> {/if}
</table>
</body>
</html>
Copy the code

Develop reading

PHP exports Excel with styles

Several uses of PHP headers

Smarty template function

Thanks ~