【问题标题】:Export to excel or csv导出到 excel 或 csv
【发布时间】:2015-02-08 11:34:44
【问题描述】:

我在网站上工作,我需要制作一个 excel 文件或至少一个在 excel 中打开的 csv 文件。 我尝试了很多东西。我在 google、stackoverflow 等上搜索了解决方案。但我发现的一切似乎都不适合我。我尝试了 jQuery 解决方案,也尝试了 PHP 解决方案。

JSFiddle:http://jsfiddle.net/terryyounghk/KPEGU/

当我使用这个小提琴制作 csv 时,输出是:

我希望输出在单独的列中,而不是在一个列中。

这里也是我试过的一个PHP解决方案:

    header("Content-type: application/vnd.ms-excel");
    header("Content-Disposition: attachment;Filename=document_name.xls");

    echo "<html>";
    echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">";
    echo "<body>";
    echo "<table>";
    echo "<tr>";
    echo "<td>Henk</td>";
    echo "<td>Henk2</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>bob</td>";
    echo "<td>bob2</td>";
    echo "</tr>";
    echo "</table>";
    echo "</body>";
    echo "</html>";

这个输出:

这里的输出在正确的列和行中。但不知何故,我无法获得网格线。

这只是我尝试过的 2 个示例。有人知道如何解决这个问题吗?

【问题讨论】:

    标签: php jquery excel csv export


    【解决方案1】:

    在您提供的 JsFiddle 示例中 - 将 colDelim = '","' 更改为 colDelim = '";"',因为它需要逗号分隔值 ; 以使字符串落在不同的单元格中。

    Updated JsFiddle

    【讨论】:

      【解决方案2】:

      我可以看到你在考虑使用表格进行格式化但 CSV 是逗号分隔的。

      我发现最好的方法是像这样使用数组:

      // setup array. Note null values to create empty cells
      $list = array( 
          array('First row', NULL, 'some_data', NULL),
          array('Second row', 'some data', NULL, 'some data'),
      ); 
      
      // set header types
      header("Content-type: text/csv");
      header("Cache-Control: no-store, no-cache");
      header('Content-Disposition: attachment; filename="most_popular-'.$_POST['start_date'].'-'.$_POST['end_date'].'.csv"');
      
      // tell php to open the page as an output (download)
      $outstream = fopen("php://output",'w');
      
      // loop through and put each array row into the csv, comma seperated.
      foreach( $list as $row )  
      {  
          fputcsv($outstream, $row, ',');  
      }  
      
      exit;
      

      请记住,数组中的每一行都需要与其他行完全相同,否则生成 csv 的循环会非常混乱。

      【讨论】:

        【解决方案3】:
        **Try this**
        
        
        
        
         <?php 
        error_reporting(E_ERROR);
        include("dbConnect.inc.php");
        //$ip = $_SERVER['HTTP_CLIENT_IP
            $select_table=mysql_query("SELECT * FROM `key_cat`");
            $setExcelName="Keyword_reports";
        header('Content-Type: text/csv');
        header('Content-Disposition: attachment;filename='.$setExcelName.'.csv');
        /*
        $setExcelName="Keywords";
         header("Content-type: application/octet-stream");
         header("Content-Disposition: attachment; filename=".$setExcelName."_Reoprt.xls");
         header("Pragma: no-cache");
        header("Expires: 0");
        */
        $rows = mysql_fetch_assoc($select_table);
        if($rows)
        {
        getcsv(array_keys($rows));
        }
        while($rows)
        {
        getcsv($rows);
        $rows = mysql_fetch_assoc($select_table);
        }
        // get total number of fields present in the database
        function getcsv($no_of_field_names)
        {
        $separate = '';
        // do the action for all field names as field name
        foreach ($no_of_field_names as $field_name)
        {
        if (preg_match('/\\r|\\n|,|"/', $field_name))
        {
        $field_name = '' . str_replace('', $field_name) . '';
        }
        echo $separate . $field_name;
        
        //sepearte with the comma
        $separate = ',';
        }
        
        //make new row and line
        echo "\r\n";
        
        }
        
        
        ?>
        

        【讨论】:

          猜你喜欢
          • 2011-12-30
          • 2010-09-23
          • 2010-11-01
          • 2015-09-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-04
          • 2010-09-22
          相关资源
          最近更新 更多