doupo2241 2017-07-11 09:47
浏览 35
已采纳

使用PHPExcel从MySQL获取字段名称到Excel

I'm trying to get the field name of my table coming from my database using PHPExcel.

However, I'm getting an error saying

mysql_field_name() expects parameter 2 to be long, string given in C:\xampp\htdocs\phpexcelsample\download.php on line 42

I think I'm missing something here... can you give me hints on how to get the fieldname?

Here's my code:

<?php




$dbhost= "localhost"; //your MySQL Server 
$dbuser = "root"; //your MySQL User Name 
$dbpass = ""; //your MySQL Password 
 $dbname = "sales"; 
//your MySQL Database Name of which database to use this 
 $tablename = "deposit"; 
//your MySQL Table Name which one you have to create excel file 
 // your mysql query here , we can edit this for your requirement 
 $sql = "Select * from $tablename "; 
//create  code for connecting to mysql 
$connect = @mysql_connect($dbhost, $dbuser, $dbpass) 
or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . 
mysql_errno()); 
//select database 
$Db = @mysql_select_db($dbname, $connect) 
or die("Couldn't select database:<br>" . mysql_error(). "<br>" . 
mysql_errno()); 
//execute query 
$result = @mysql_query($sql,$connect) 
or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . 
mysql_errno()); 

error_reporting(E_ALL);


require_once 'Classes/PHPExcel.php';
// Execute the database query
// Instantiate a new PHPExcel object 
$objPHPExcel = new PHPExcel();  
// Set the active Excel worksheet to sheet 0 
$objPHPExcel->setActiveSheetIndex(0);  
// Initialise the Excel row number 
$rowCount = 1;  

//start of printing column names as names of MySQL fields  
$column = 'A';
for ($i = 1; $i < mysql_num_fields($result); $i++)    
{
//$objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, 
mysql_field_name($result,$i));
$objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, 
mysql_field_name($result,'EMPLOYEE'));
$column++;
}
//end of adding column names  

//start while loop to get data  
$rowCount = 2;  
while($row = mysql_fetch_row($result))  
{  
$column = 'A';
for($j=1; $j<mysql_num_fields($result);$j++)  
{  
    if(!isset($row[$j]))  
        $value = NULL;  
    elseif ($row[$j] != "")  
        $value = strip_tags($row[$j]);  
    else  
        $value = "";  

    $objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, $value);
    $column++;
}  
$rowCount++;
} 


// Redirect output to a client’s web browser (Excel5) 
header('Content-Type: application/vnd.ms-excel'); 
header('Content-Disposition: attachment;filename="Result.xls"'); 
header('Cache-Control: max-age=0'); 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 
$objWriter->save('php://output');









?>
  • 写回答

2条回答 默认 最新

  • douweiduo7526 2017-07-16 09:18
    关注

    I've found out how to do it :)

    Here's my code:

    $dbhost= "localhost"; //your MySQL Server 
    $dbuser = "root"; //your MySQL User Name 
    $dbpass = ""; //your MySQL Password 
    $dbname = "sales"; 
    //your MySQL Database Name of which database to use this 
    $tablename = "deposit"; 
    //your MySQL Table Name which one you have to create excel file 
    // your mysql query here , we can edit this for your requirement 
    $sql = "Select * from $tablename "; 
    //create  code for connecting to mysql 
     $connect = @mysql_connect($dbhost, $dbuser, $dbpass) 
    or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . 
    mysql_errno()); 
    //select database 
    $Db = @mysql_select_db($dbname, $connect) 
    or die("Couldn't select database:<br>" . mysql_error(). "<br>" . 
    mysql_errno()); 
     //execute query 
    $result = @mysql_query($sql,$connect) 
    or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . 
    mysql_errno()); 
    
    error_reporting(E_ALL);
    
     require_once 'Classes/PHPExcel.php';
     // Execute the database query
    // Instantiate a new PHPExcel object 
    $objPHPExcel = new PHPExcel();  
    // Set the active Excel worksheet to sheet 0 
    $objPHPExcel->setActiveSheetIndex(0);  
    // Initialise the Excel row number 
    $rowCount = 1;  
    
     while($row = mysql_fetch_array($result)){
    $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['DATE']);
    
     $rowCount++;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab