在导出到 Excel 之前将数据制成表格格式

发布于 2024-10-15 18:52:18 字数 4216 浏览 3 评论 0原文

我使用下面的这个函数来处理从数据库检索的数组数据,然后将此数据导出到 Excel。

# feed the final items to our formatting function...
$contents = get_excel_data($items);

function get_excel_data($items){

    # set the variable
    $output = null;

    # check if the data is an items and is not empty
    if (is_array($items)  && !empty($items))
    {
        # start the row at 0
        $row = 0;

        # loop the items
        # "foreach(array_values($data) as $item)" is too complicated. "foreach($data as $item)" will suffice.
        # foreach(array_values($items) as $item)
        foreach($items as $item)
        {
            if (is_array($item) && !empty($item))
            {
                if ($row == 0)
                {
                    # write the column headers
                    $output = implode("\t",array_keys($item));
                    $output .= "\n";
                }
                    # create a line of values for this row...
                    $output .= implode("\t",array_values($item));
                    $output .= "\n";

                    # increment the row so we don't create headers all over again
                    $row++;
            }
        }
    }

    # return the result
    return $output;
}

它像下面这样处理输出,

cat_id  cat_name    cat_order   cat_hide    cat_important   cat_created cat_updated
1   Primary image   1   0   1   0000-00-00 00:00:00 2011-01-17 17:26:51
2   Secondary image 2   0   1   0000-00-00 00:00:00 2011-01-17 17:27:01
3   Tertiary image  3   0   1   0000-00-00 00:00:00 2010-10-08 20:03:56
4   Quartary image  4   0   1   0000-00-00 00:00:00 2010-10-08 20:03:56

但理想情况下我想将输出包装在这样的表格中,

 <table border="1">
<tr>
<th>cat_id</th> 
<th>cat_name</th>   
<th>cat_order</th>  
<th>cat_hide</th>   
<th>cat_important</th>  
<th>cat_created</th>    
<th>cat_updated</th>
</tr>

<tr>
<td>1</td>  
<td>Primary image</td>  
<td>1</td>  
<td>0</td>  
<td>1</td>  
<td>0000-00-00 00:00:00</td>    
<td>2011-01-17 17:26:51</td>
</tr>

<td>2</td>  
<td>Secondary image</td>    
<td>2</td>  
<td>0</td>  
<td>1</td>  
<td>0000-00-00 00:00:00</td>    
<td>2011-01-17 17:26:51</td>
</tr>
...
</table>

有什么想法我应该添加到原始代码中以输出上面的表格吗?

我尝试了下面的修改版本,但失败了!

function get_excel_data($items){

    # set the variable
    $output = null;

    # check if the data is an items and is not empty
    if (is_array($items)  && !empty($items))
    {
        # start the row at 0
        $row = 0;

        # loop the items
        # "foreach(array_values($data) as $item)" is too complicated. "foreach($data as $item)" will suffice.
        # foreach(array_values($items) as $item)
        foreach($items as $key => $value )
        {

                if ($row == 0)
                {   
                    /*
                    # write the column headers
                    $output = implode("\t",array_keys($item));
                    $output .= "\n";
                    */

                    $output = '<table border="1"><tr>';
                    $output .= '<th>'.$key.'</th>';
                    $output .= '</tr>';
                }
                    /*
                    # create a line of values for this row...
                    $output .= implode("\t",array_values($item));
                    $output .= "\n";
                    */

                    $output .= '<tr>';
                    $output .= '<td>'.$value.'</td>';

                    $output .= '</tr>';


                if ($row == 0)
                {
                    $output .=  '</table>';
                }

                    # increment the row so we don't create headers all over again
                    $row++;

        }
    }

    # return the result
    return $output;
}

谢谢。

I use this function below to process the array data I retrieve from my db, then export this data for Excel.

# feed the final items to our formatting function...
$contents = get_excel_data($items);

function get_excel_data($items){

    # set the variable
    $output = null;

    # check if the data is an items and is not empty
    if (is_array($items)  && !empty($items))
    {
        # start the row at 0
        $row = 0;

        # loop the items
        # "foreach(array_values($data) as $item)" is too complicated. "foreach($data as $item)" will suffice.
        # foreach(array_values($items) as $item)
        foreach($items as $item)
        {
            if (is_array($item) && !empty($item))
            {
                if ($row == 0)
                {
                    # write the column headers
                    $output = implode("\t",array_keys($item));
                    $output .= "\n";
                }
                    # create a line of values for this row...
                    $output .= implode("\t",array_values($item));
                    $output .= "\n";

                    # increment the row so we don't create headers all over again
                    $row++;
            }
        }
    }

    # return the result
    return $output;
}

It processes the output like this below,

cat_id  cat_name    cat_order   cat_hide    cat_important   cat_created cat_updated
1   Primary image   1   0   1   0000-00-00 00:00:00 2011-01-17 17:26:51
2   Secondary image 2   0   1   0000-00-00 00:00:00 2011-01-17 17:27:01
3   Tertiary image  3   0   1   0000-00-00 00:00:00 2010-10-08 20:03:56
4   Quartary image  4   0   1   0000-00-00 00:00:00 2010-10-08 20:03:56

but ideally I want to wrap the output in a table like this,

 <table border="1">
<tr>
<th>cat_id</th> 
<th>cat_name</th>   
<th>cat_order</th>  
<th>cat_hide</th>   
<th>cat_important</th>  
<th>cat_created</th>    
<th>cat_updated</th>
</tr>

<tr>
<td>1</td>  
<td>Primary image</td>  
<td>1</td>  
<td>0</td>  
<td>1</td>  
<td>0000-00-00 00:00:00</td>    
<td>2011-01-17 17:26:51</td>
</tr>

<td>2</td>  
<td>Secondary image</td>    
<td>2</td>  
<td>0</td>  
<td>1</td>  
<td>0000-00-00 00:00:00</td>    
<td>2011-01-17 17:26:51</td>
</tr>
...
</table>

Any ideas what I should add into the original code to output the table above?

I tried with my version of modification below but it fails!

function get_excel_data($items){

    # set the variable
    $output = null;

    # check if the data is an items and is not empty
    if (is_array($items)  && !empty($items))
    {
        # start the row at 0
        $row = 0;

        # loop the items
        # "foreach(array_values($data) as $item)" is too complicated. "foreach($data as $item)" will suffice.
        # foreach(array_values($items) as $item)
        foreach($items as $key => $value )
        {

                if ($row == 0)
                {   
                    /*
                    # write the column headers
                    $output = implode("\t",array_keys($item));
                    $output .= "\n";
                    */

                    $output = '<table border="1"><tr>';
                    $output .= '<th>'.$key.'</th>';
                    $output .= '</tr>';
                }
                    /*
                    # create a line of values for this row...
                    $output .= implode("\t",array_values($item));
                    $output .= "\n";
                    */

                    $output .= '<tr>';
                    $output .= '<td>'.$value.'</td>';

                    $output .= '</tr>';


                if ($row == 0)
                {
                    $output .=  '</table>';
                }

                    # increment the row so we don't create headers all over again
                    $row++;

        }
    }

    # return the result
    return $output;
}

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

向日葵 2024-10-22 18:52:18

这是您的函数的未经测试的变体,可能会起作用。

# feed the final items to our formatting function...
$contents = get_excel_data($items);

function get_excel_data($items){

    # set the variable
    $output = null;

    # check if the data is an items and is not empty
    if (is_array($items)  && !empty($items))
    {
        # start the row at 0
        $row = 0;

        $output = '<table border="1">'

        # loop the items
        # "foreach(array_values($data) as $item)" is too complicated. "foreach($data as $item)" will suffice.
        # foreach(array_values($items) as $item)
        foreach($items as $item)
        {
            if (is_array($item) && !empty($item))
            {
                if ($row == 0)
                {
                    $output .= '<tr>';
                    foreach(array_keys($item) as $header)
                    {
                        $output .= '<th>'.$header.'</th>';
                    }
                    $output .= '</tr>';
                }

                $output .= '<tr>';
                foreach(array_values($item) as $cell)
                {
                    $output .= '<td>'.$cell.'</td>';
                }
                $output .= '</tr>';

                # increment the row so we only create headers once
                $row++;
            }
        }
        $output .= '</table>';
    }

    # return the result
    return $output;
}

Here is an untested variant on your function that is likely to work.

# feed the final items to our formatting function...
$contents = get_excel_data($items);

function get_excel_data($items){

    # set the variable
    $output = null;

    # check if the data is an items and is not empty
    if (is_array($items)  && !empty($items))
    {
        # start the row at 0
        $row = 0;

        $output = '<table border="1">'

        # loop the items
        # "foreach(array_values($data) as $item)" is too complicated. "foreach($data as $item)" will suffice.
        # foreach(array_values($items) as $item)
        foreach($items as $item)
        {
            if (is_array($item) && !empty($item))
            {
                if ($row == 0)
                {
                    $output .= '<tr>';
                    foreach(array_keys($item) as $header)
                    {
                        $output .= '<th>'.$header.'</th>';
                    }
                    $output .= '</tr>';
                }

                $output .= '<tr>';
                foreach(array_values($item) as $cell)
                {
                    $output .= '<td>'.$cell.'</td>';
                }
                $output .= '</tr>';

                # increment the row so we only create headers once
                $row++;
            }
        }
        $output .= '</table>';
    }

    # return the result
    return $output;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文