Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php将数据库数据导出到excel_Php_Mysql_Excel - Fatal编程技术网

使用php将数据库数据导出到excel

使用php将数据库数据导出到excel,php,mysql,excel,Php,Mysql,Excel,我正在尝试使用php将MySQL数据转换成Excel文件。代码工作正常,但我希望字段名如下所示 不仅仅是这个 代码如下: <?php include_once('config.php'); $xls_filename = 'export_'.date('Y-m-d').'.xls'; $result = mysql_query("Select * from ojt_group3_pole"); header("Content-Type: app

我正在尝试使用php将MySQL数据转换成Excel文件。代码工作正常,但我希望字段名如下所示

不仅仅是这个

代码如下:

<?php 
    include_once('config.php');

   $xls_filename = 'export_'.date('Y-m-d').'.xls'; 

     $result = mysql_query("Select * from ojt_group3_pole"); 

   header("Content-Type: application/xls"); 
   header("Content-Disposition: attachment; filename=$xls_filename"); 
   header("Pragma: no-cache"); 
   header("Expires: 0"); 

   $sep = "\t"; 


   for ($i = 0; $i<mysql_num_fields($result); $i++) { 
    echo mysql_field_name($result, $i) . "\t"; 
   } 
   print("\n"); 
   while($row = mysql_fetch_row($result)) 
   { 
     $schema_insert = ""; 
     for($j=0; $j<mysql_num_fields($result); $j++) 
     { 
       if(!isset($row[$j])) { 
        $schema_insert .= "NULL".$sep; 
       } 
       elseif ($row[$j] != "") { 
         $schema_insert .= "$row[$j]".$sep; 
       } 
       else { 
         $schema_insert .= "".$sep; 
      } 
     } 
     $schema_insert = str_replace($sep."$", "", $schema_insert); 
     $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert); 
     $schema_insert .= "\t"; 
     print(trim($schema_insert)); 
     print "\n"; 
   } 
?> 

您当前的代码基本上是生成一个CSV文件,并给它一个“.xls”扩展名,希望Excel在文件打开时能够纠正问题

CSV格式不支持Excel的所有功能,因此,单元格合并和样式之类的事情将不是当前方法所能做到的

您必须使用PHPExcel之类的库(可能还有其他库)来生成真正的Excel文件,然后编写代码来执行mergeCells()之类的操作,以按照您希望的方式设置电子表格。一个非常基本的例子:

<?php
// Load the libraries
include 'PHPExcel.php';
include 'PHPExcel/Writer/Excel2007.php';

// Create a new PHPExcel instance
$objPHPExcel = new PHPExcel();

// Set the workbook properties
$objPHPExcel->getProperties()->setCreator("Your name here");
$objPHPExcel->getProperties()->setTitle("My Excel Document");

// Add data
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Hello world!');
$objPHPExcel->getActiveSheet()->mergeCells('A1:B1');

// Save the file
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save("mynewfile.xlsx");

停止使用mysql_*首先,确保停止使用“mysql_*函数”。它们已被弃用,并在较新版本的PHP中被完全删除。您应该使用mysqli_uu函数或PDO。您不需要编写excel格式的数据,而是纯文本。您可以尝试的一个快速而肮脏的解决方案是使用普通HTML表创建所需的格式。只需在构建时删除标题即可。当它在浏览器中看起来正确时,重新添加标题,它可能会工作。PhpExcel不推荐使用“PhpExcel不推荐使用”说@除了使用PHPExcel?@rtfm-他们自己的github repo::它的标题是“PHPExcel-弃用”@MagnusEriksson,ta。