简体   繁体   English

在PHP中将表导出为Excel文件

[英]Export Table as Excel file in php

CSS is effecting in UI but not in Excel. CSS在UI中起作用,但在Excel中不起作用。 Anyone can help me on this please. 任何人都可以帮助我。

Below code will export table into excel but not able to provide space before tags. 下面的代码会将表导出到excel中,但不能在标签前提供空格。

<?php 
    echo $excel_data = '<table border="1">
        <thead>
            <th align="left">S.No.</th>
            <th align="left">Name</th>
            <th align="left">DOJ</th>
        </thead>
        <tbody>
            <tr>
                <td align="left">1</td>
                <td align="left">Sreekanth Kuriyala</td>
                <td align="left">04-06-2015</td>
            </tr>
            <tr>
                <td align="left">2</td>
                <td style="padding-left:20px;">SK</td>
                <td align="left">26-07-2015</td>
            </tr>
        </tbody>
    </table>';
    $excel_file = 'Reports.xls';
    file_put_contents ($excel_file, $excel_data);
?>
</br>
</br>
<a href="<?php echo $excel_file; ?>" download>Export to Excel</a>   

you have to create a CSV file just create new php file with a function that expects a result of a db select. 您必须创建一个CSV文件,而只需创建一个新的php文件,该文件的功能应为选择数据库的结果。

then just echo cols separated by ; 然后只是回声cols被分隔; and carrier return for next row. 和承运人返回下一行。

this is a function i made: 这是我做的功能:

<?php
function printcsv($result){
                  //result of database select 
header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename=example.csv');
echo "\xEF\xBB\xBF";
$nl = "\n";
//this will be the carrier return var
    if($rs = $result->fetch_array(MYSQLI_NUM)){
        while($rs != ''){
            echo $rs[0].";".$rs[1].";".$rs[2].";".$rs[3].";".$rs[4].";".$rs[5].";".$nl;
            $rs = $result->fetch_array(MYSQLI_NUM);
        }
    }
}
?>

change my mysqli_fetch_array by your result desired fetch and try it =) 通过所需的结果获取来更改我的mysqli_fetch_array并尝试=)

oh, and you cannot write tables on it, only echo data separated by semicolon. 哦,您不能在上面写表,只能回显用分号分隔的数据。 each data for each column, semicolon to put the next data in next column, and carrier return to write in next row. 每列的每个数据,用分号将下一个数据放入下一列,而载体返回以写在下一行。

cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM