将表导出到xlsx会将整个页面内容写入文件

时间:2014-07-06 05:35:34

标签: php html excel

将html表导出到xlsx文件会将每个html页面内容写入文件中。

我的页面上有四个按钮和其他html代码。其中一个按钮结果是html表,并将其写入xlsx文件。

但除了编写表格内容外,它还会写出label contentName of all four table

的html
            <h1> Arabic to English </h1>

            <form method="post" enctype="multipart/form-data">

            *.XLSX <input type="file" name="file"  />&nbsp;&nbsp;<input type="submit" name="submit1" value="Arabic to English" />

            </form>

如何停止将除html表之外的内容写入导出的xlsx文件?

这是我的代码:

    <label style ="margin-left:30%; margin-bottom:10%; color:red">Please reaload the page before each test [would be fixed]</label>     

<div class= "c1">
    <div class="row demo-row">
            <div class="col-xs-3">
            <a id="Bar2en" rel="leanModal" name="ar2en" href="#ar2en" class="btn btn-block btn-lg btn-primary">Arabic to English</a>
        </div>
            <div class="col-xs-3">
            <a id="Ben2ar" rel="leanModal" name="ben2ara" href="#ar2en"  class="btn btn-block btn-lg btn-success">English to Arabic</a>
        </div>
            <div class="col-xs-3">
            <a id="Ben2ar" rel="leanModal" name="ben2ara" href="#xlsar2en"  class="btn btn-block btn-lg btn-info">Export Arabic to English</a>
        </div>
            <div class="col-xs-3">
            <a id="Ben2ar" rel="leanModal" name="ben2ara" href="#xlsen2ar"  class="btn btn-block btn-lg btn-info">Export English to Arabic</a>
        </div>
</div>
</div>
<div id="xlsar2en" style="text-align:center">
        <div class="container">
        <div style="text-align:center">
    <?php
    if (isset($_FILES['file'])) {
        header('Content-Type: text/html; charset=UTF-8');
        require_once "simplexlsx.class.php";
        require '../../Arabic.php';
        $Arabic = new I18N_Arabic('Transliteration');
        $xlsx = new SimpleXLSX( $_FILES['file']['tmp_name'] );
        $file="result.xls";
    //echo $_POST['submit1'];
        header("Content-type: application/vnd.ms-excel");
        header("Content-Disposition: attachment; filename=$file");
        echo '<table border="1" cellpadding="3" style="border-collapse: collapse">';
        $eng = array();
        list($cols,) = $xlsx->dimension();  
        foreach( $xlsx->rows() as $k => $r) {
    //      if ($k == 0) continue; // skip first row
            echo '<tr>';
            for( $i = 0; $i < $cols; $i++)
            {
                if ($_POST['submit2'] = 'Arabic to English')
                    $temp =  $Arabic->en2ar($r[$i]);
                else
                    continue;   
                echo '<td>'.( (isset($r[$i])) ? $temp : '&nbsp;' ).'</td>';
            }
            echo '</tr>';
        }
        echo '</table>';
    }
    ?>
        <h1> Arabic to English </h1>
        <form method="post" enctype="multipart/form-data">
        *.XLSX <input type="file" name="file"  />&nbsp;&nbsp;<input type="submit" name="submit1" value="Arabic to English" />
        </form>
    </div>
   </div>
</div>

更新

<div id="xlsar2en" style="text-align:center">
        <div class="container">
        <div style="text-align:center">
        <!-- PHP script -->
        <h1> Arabic to English </h1>
        <form method="post" action="a2e.php" enctype="multipart/form-data" target="iframe1" >
                *.XLSX <input type="file" name="file"  />&nbsp;&nbsp;<input type="submit" name="submit1" value="Arabic to English"/>
    </form>
    <iframe name="iframe1" id="iframe1" src="" style="display:none" ></iframe>
    </div>
   </div>
</div>

<div id="xlsen2ar" style="text-align:center">
        <div class="container">
        <div style="text-align:center">

        <h1> English to Arabic</h1>
        <form method="post" action="e2a.php" enctype="multipart/form-data" target="iframe2" >
        *.XLSX <input type="file" name="file"  />&nbsp;&nbsp;<input type="submit" name="submit2" value="Enlish to Arabic" />
        </form>
        <iframe name="iframe2" id="iframe2" src="" style="display:none" ></iframe>
    </div>
   </div>
</div>

e2a.php

    <?php
    if (isset($_FILES['file'])) {
        header('Content-Type: text/html; charset=UTF-8');
        require_once "simplexlsx.class.php";
        require '../../Arabic.php';
        $Arabic = new I18N_Arabic('Transliteration');
        $xlsx = new SimpleXLSX( $_FILES['file']['tmp_name'] );
        $file="result.xls";
        header("Content-type: application/vnd.ms-excel");
        header("Content-Disposition: attachment; filename=$file");
        echo '<table border="1" cellpadding="3" style="border-collapse: collapse">';

        $eng = array();
        list($cols,) = $xlsx->dimension();  
        foreach( $xlsx->rows() as $k => $r) {
    //      if ($k == 0) continue; // skip first row
            echo '<tr>';
            for( $i = 0; $i < $cols; $i++)
            {   
                if ($_POST['submit2'] = 'Enlish to Arabic')
                    $temp =  $Arabic->en2ar($r[$i]);    
            echo '<td>'.( (isset($r[$i])) ? $temp : '&nbsp;' ).'</td>';
            }
            echo '</tr>';
        }
        echo '</table>';
    }
    ?>

1 个答案:

答案 0 :(得分:0)

输出缓冲区,在你的header()调用之前,或者把标题放在顶部,文件写入将捕获任何输出,所以很明显任何html在你写文件之前都会被捕获,你有3个选择,

  1. 在写入文件之前不输出任何内容,(在任何html输出之前移动该代码) - 可能会在它之后捕获,

  2. 使用ob_start(),ob_get_clean()缓存输出,然后在文件后回显它,

  3. 最佳选择将文件输出分成单独的php文件,并在需要时通过iframe加载。

  4. 你仍然会在同一个文件中强行下载时遇到问题,最好的办法就是发布一个目标作为iframe的表单,然后在iframe中运行文件处理和下载位,用css隐藏iframe。

    我很惊讶你没有标题已发送错误。

    所以

     <form method="post" action="location of file processing .php file" enctype="multipart/form-data" target="iframe" >
            *.XLSX <input type="file" name="file"  />&nbsp;&nbsp;<input type="submit" name="submit1" value="Arabic to English" />
            </form>
    
    <iframe name="iframe" id="iframe" src="" style="display:none" ></iframe>
    

    将此位放在表格&#34; action&#34;指出。

    <?php
        if (isset($_FILES['file'])) {
            header('Content-Type: text/html; charset=UTF-8');
            require_once "simplexlsx.class.php";
            require '../../Arabic.php';
            $Arabic = new I18N_Arabic('Transliteration');
            $xlsx = new SimpleXLSX( $_FILES['file']['tmp_name'] );
            $file="result.xls";
        //echo $_POST['submit1'];
            header("Content-type: application/vnd.ms-excel");
            header("Content-Disposition: attachment; filename=$file");
            echo '<table border="1" cellpadding="3" style="border-collapse: collapse">';
            $eng = array();
            list($cols,) = $xlsx->dimension();  
            foreach( $xlsx->rows() as $k => $r) {
        //      if ($k == 0) continue; // skip first row
                echo '<tr>';
                for( $i = 0; $i < $cols; $i++)
                {
                    if ($_POST['submit2'] = 'Arabic to English')
                        $temp =  $Arabic->en2ar($r[$i]);
                    else
                        continue;   
                    echo '<td>'.( (isset($r[$i])) ? $temp : '&nbsp;' ).'</td>';
                }
                echo '</tr>';
            }
            echo '</table>';
        }
        ?>
    

    对于您的更新,我不确定我只是回答有关输出错误的部分,但我可以告诉您这是不正确的

     if ($_POST['submit2'] = 'Enlish to Arabic')
         $temp =  $Arabic->en2ar($r[$i]);    
         echo '<td>'.( (isset($r[$i])) ? $temp : '&nbsp;' ).'</td>';
    

    暗示

     if ($_POST['submit2'] = 'Enlish to Arabic'){
         $temp =  $Arabic->en2ar($r[$i]);    
     }
     echo '<td>'.( (isset($r[$i])) ? $temp : '&nbsp;' ).'</td>';
    

    $_POST['submit2'] = 'Enlish to Arabic';
    if ($_POST['submit2'] == 'Enlish to Arabic'){...
    

    在这种情况下,你从未在这里设置$ temp变量,$ _POST [&#39; submit2&#39;]总是等于&#39; Enlish to Arabic&#39;

    你会想要这个(你也做了单身=总是如此)

    if ($_POST['submit2'] == 'Enlish to Arabic'){
         $temp =  $Arabic->en2ar($r[$i]);    
         echo '<td>'.( (isset($r[$i])) ? $temp : '&nbsp;' ).'</td>';
    }