首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将所有列下载到excel工作表中

将所有列下载到excel工作表中
EN

Stack Overflow用户
提问于 2016-07-09 06:14:29
回答 1查看 81关注 0票数 0

我有一个表主帐户,其中有20列,但只显示了两列Emailmobile。现在我想下载所有列到excel表中。我有个密码。但这是行不通的。有人能帮忙吗?

E:\wamp\www\export_to_excel\export_to_excel\index.php中的错误是:警告:为第83行中的foreach()提供的无效参数

代码语言:javascript
运行
复制
    <?php
    include("connection.php");

   $data1 = mysql_query("select * from mainaccount order by id DESC") or die(mysql_error());

                        while($data=mysql_fetch_array($data1)){

                        ?>

                        <?

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

    switch($_POST["ExportType"])
    {
    case "export-to-excel" :
        // Submission from
        $filename = $_POST["ExportType"] . ".xls";       
        header("Content-Type: application/vnd.ms-excel");
        header("Content-Disposition: attachment; filename=\"$filename\"");
        ExportFile($data);
        //$_POST["ExportType"] = '';
        exit();
    default :
        die("Unknown action : ".$_POST["action"]);
        break;
    }
    }
    function ExportFile($records) {
     $heading = false;
    if(!empty($records))
      foreach($records as $row) {
        if(!$heading) {
          // display field/column names as a first row
          echo implode("\t", array_keys($row)) . "\n";
          $heading = true;
        }
        echo implode("\t", array_values($row)) . "\n";
      }
    exit;
     }

   ?>

   <?php } ?>
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap-theme.min.css">
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>

      <div> 
     <div id="container" >
      <div class="col-sm-6 pull-left">
              <div class="well well-sm col-sm-12">
                  <b id='project-capacity-count-lable'><?php echo count($data);?></b> records found.
               <div class="btn-group pull-right">

        <ul class="" role="menu" id="export-menu">
       <li id="export-to-excel"><a href="#">Export to excel</a></li>

       </ul>
      </div>

                  </div>
                  <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" id="export-form">
                    <input type="hidden" value='' id='hidden-type' name='ExportType'/>
                  </form>
               <table id="" class="table table-striped table-bordered">
                <tr>
                    <th>Email</th>
                    <th>Mobile</th>

              </tr>

            <?php   $res5 = mysql_query("select * from mainaccount order by id DESC") or die(mysql_error());
                                        $sr = 1;
                                        while($row5 = mysql_fetch_array($res5))
                                        { ?>
            <tbody>
              <?php foreach($data as $row):?>
              <tr>
              <td><?php  $row5['email']; ?></td>
              <td><?php  $row5['mobile']; ?></td>

              </tr>
              <?php endforeach; ?>
            </tbody>
            <?php } ?>
          </table>
          </div></div>  

        </div>
       </body>   
         <script  type="text/javascript">
       $(document).ready(function() {
       jQuery('#export-menu li').bind("click", function() {
        var target = $(this).attr('id');
       switch(target) {
    case 'export-to-excel' :
    $('#hidden-type').val(target);
   //alert($('#hidden-type').val());
    $('#export-form').submit();
    $('#hidden-type').val('');
    break
    }
   });
   });
  </script>
EN

回答 1

Stack Overflow用户

发布于 2016-07-09 08:32:06

我不能百分之百确定,但我认为函数ExportFile应该这样调用:

代码语言:javascript
运行
复制
        case "export-to-excel" :
    // Submission from
    $filename = $_POST["ExportType"] . ".xls";       
    header("Content-Type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=\"$filename\"");
    $data = mysql_query("select * from mainaccount order by id DESC") or die(mysql_error());
    ExportFile($data);
    //$_POST["ExportType"] = '';
    exit();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38278969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档