EXPORT到CSV文件的问题

时间:2015-06-01 10:09:24

标签: php html csv

我想在输入文本中导出我写的内容:

- 我创建了一个名为“ExcelFile”的类,其中包含将数据导出到csv文件的方法。

- 我有一个问题,当我尝试用数据导出这个文件时,在csv文件中自动添加一个colone,这个colone包含我表的代码html

- 这是我的代码:

ExcelFile.php

<?php
include_once 'ExcelFile.php';
?>
<html>

<form method='post' action="index.php">
    <table>
        <tr>
            <td><label>First Name</label></td>
            <td><input type='text' name='tfirst' /></td>
        </tr>

        <tr>
            <td><label>Last Name</label></td>
            <td><input type='text' name='tlast' /></td>
        </tr>

        <tr>
            <td><input type='submit' name='btnsave' value='Export Csv File' /></td>
        </tr>

    </table>
</form>
</html>
<?php
if (isset ( $_POST ["btnsave"] )) {

    test ();
}
function test() {
    $Excelfile = new ExcelFile ();
    $Excelfile->Colonne ( "First Name;Last Name" );
    $Excelfile->Insertion ( $_POST ['tfirst'] . ';' . $_POST ['tlast'] );
    $Excelfile->output ( 'Export result' );

}
?>

的index.php

{{1}}

1 个答案:

答案 0 :(得分:0)

在您发送完整个HTML文档后调用output()函数,这就是为什么它包含在您的文件中。

您需要稍微移动代码,以便index.php文件看起来更像这样:

<?php
include_once 'ExcelFile.php';

if (isset ( $_POST ["btnsave"] )) {

    test ();
    exit; //you want to end here, so the html below is not in the CSV
}

function test() {
    $Excelfile = new ExcelFile ();
    $Excelfile->Colonne ( "First Name;Last Name" );
    $Excelfile->Insertion ( $_POST ['tfirst'] . ';' . $_POST ['tlast'] );
    $Excelfile->output ( 'Export result' );

}
?>
<html>

<form method='post' action="index.php">
    <table>
        <tr>
            <td><label>First Name</label></td>
            <td><input type='text' name='tfirst' /></td>
        </tr>

        <tr>
            <td><label>Last Name</label></td>
            <td><input type='text' name='tlast' /></td>
        </tr>

        <tr>
            <td><input type='submit' name='btnsave' value='Export Csv File' /></td>
        </tr>

    </table>
</form>
</html>

(旁注:请考虑使用PHP框架)

(旁注:请考虑使用逗号分隔值而不是分号)

相关问题