【问题标题】:To make data into table format before exporting to Excel在导出到 Excel 之前将数据转换为表格格式
【发布时间】:2016-11-09 22:55:43
【问题描述】:

我使用下面的这个函数来处理我从数据库中检索到的数组数据,然后将这些数据导出到 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;
}

谢谢。

【问题讨论】:

  • 为什么不创建一个真正的 Excel 文件,而不是试图将制表符分隔的文件或 HTML 文件伪装成 Excel 文件?
  • '为什么不创建一个真正的 Excel 文件' - 你的意思是使用 Microsoft excel 创建 Excel 文件?
  • @lauthiamkok:不,他的意思是以编程方式创建一个实际的 excel 文件...查看 phpexcel - phpexcel.codeplex.com :-)
  • @prodigitalson:谢谢。我知道那个网站。但我更喜欢从头开始更简单的东西,恐怕......
  • @lauthiamkok:您能描述一下您遇到的错误吗?

标签: php database html-table export-to-excel implode


【解决方案1】:

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

# 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;
}

【讨论】:

  • 你的 $output .= '' 放错地方了吗?你希望它在下一个大括号之后,在 foreach 循环之外,对吗?
  • 感谢代码 - 我尝试并修复了小错误 - 但数组键未输入输出,例如 - cat_id、cat_name、cat_order 等。
  • @Jared Farrish:你是对的。这就是我尝试直接在浏览器中编写代码时发生的情况。固定。
  • @lauthiamkok:我想我明白发生了什么。试试修改后的版本。
猜你喜欢
  • 2021-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-18
  • 2017-12-10
  • 2021-10-25
  • 2019-07-11
  • 1970-01-01
相关资源
最近更新 更多