从MySQL创buildExcel文件

我试图从MySQL数据库中的一个表中生成一个XLS文件,但Excel文件格式不正确,并且当Excel文件生成“您试图打开的文件的格式不同于指定的格式时出现错误”。 文件打开时,数据格式不正确。

任何想法我失踪?

<?php $host = 'XXXXXXX'; $dbname = 'XXXXXXXX'; $username = 'XXXXXXXX'; $password = 'XXXXXXXX'; function xlsBOF() { echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); return; } function xlsEOF() { echo pack("ss", 0x0A, 0x00); return; } function xlsWriteLabel($Row, $Col, $Value ) { $L = strlen($Value); echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L); echo $Value; return; } function xlsWriteNumber($Row, $Col, $Value) { echo pack("sssss", 0x203, 14, $Row, $Col, 0x0); echo pack("d", $Value); return; } try { $conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password); echo "Connected to $dbname at $host successfully."; $conn = null; } catch (PDOException $pe) { die("Could not connect to the database $dbname :" . $pe->getMessage()); } $q = "SELECT * FROM tablename"; $qr = mysql_query( $q ) or die( mysql_error() ); header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header("Content-Disposition: attachment;filename=export_".$dbtable.".xls "); header("Content-Transfer-Encoding: binary "); xlsBOF(); $col = 0; $row = 0; $first = true; while( $qrow = mysql_fetch_assoc( $qr ) ) { if( $first ) { foreach( $qrow as $k => $v ) { xlsWriteLabel( $row, $col, strtoupper( ereg_replace( "_" , " " , $k ) ) ); $col++; } $col = 0; $row++; $first = false; } // go through the data foreach( $qrow as $k => $v ) { // write it out xlsWriteLabel( $row, $col, $v ); $col++; } // reset col and goto next row $col = 0; $row++; } xlsEOF(); exit(); 

我不确定.xls,但输出MySQL结果作为CSV表fputcsv函数没有多less大惊小怪:

 // Clear any previous output ob_end_clean(); // I assume you already have your $result $num_fields = mysql_num_fields($result); // Fetch MySQL result headers $headers = array(); $headers[] = "[Row]"; for ($i = 0; $i < $num_fields; $i++) { $headers[] = strtoupper(mysql_field_name($result , $i)); } // Filename with current date $current_date = date("y/m/d"); $filename = "MyFileName" . $current_date . ".csv"; // Open php output stream and write headers $fp = fopen('php://output', 'w'); if ($fp && $result) { header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename='.$filename); header('Pragma: no-cache'); header('Expires: 0'); echo "Title of Your CSV File\n\n"; // Write mysql headers to csv fputcsv($fp, $headers); $row_tally = 0; // Write mysql rows to csv while ($row = mysql_fetch_array($result, MYSQL_NUM)) { $row_tally = $row_tally + 1; echo $row_tally.","; fputcsv($fp, array_values($row)); } die; } 

使用http://phpexcel.codeplex.com/

C'est la meilleur solution pourgénérerun fichier excel Vous pouvezmêmecréerplusieurs feuilles dans le le fichier et formater les cellules(couleur,police,bordure,…)

谷歌翻译

这是生成excel文件的最佳解决scheme您甚至可以在文件中创build多个工作表并对单元格(颜色,字体,边框等)进行格式化。

 <?php //download.php page code //THIS PROGRAM WILL FETCH THE RESULT OF SQL QUERY AND WILL DOWNLOAD IT. (IF YOU HAVE ANY QUERY CONTACT:rahulpatel541@gmail.com) //include the database file connection include_once('database.php'); //will work if the link is set in the indx.php page if(isset($_GET['name'])) { $name=$_GET['name']; //to rename the file header('Content-Disposition: attachment; filename='.$name.'.xls'); header('Cache-Control: no-cache, no-store, must-revalidate, post-check=0, pre-check=0'); header('Pragma: no-cache'); header('Content-Type: application/x-msexcel; charset=windows-1251; format=attachment;'); $msg=""; $var=""; //write your query $sql="select * from tablename"; $res = mysql_query($sql); $numcolumn = mysql_num_fields($res); //will fetch number of field in table $msg="<table><tr><td>Sl No</td>"; for ( $i = 0; $i < $numcolumn; $i++ ) { $msg.="<td>"; $msg.= mysql_field_name($res, $i); //will store column name of the table to msg variable $msg.="</td>"; } $msg.="</tr>"; $i=0; $count=1; //used to print sl.no while($row=mysql_fetch_array($res)) //fetch all the row as array { $msg.="<tr><td>".$count."</td>"; for($i=0;$i< $numcolumn;$i++) { $var=$row[$i]; //will store all the values of row $msg.="<td>".$var."</td>"; } $count=$count+1; $msg.="</tr>"; } $msg.="</table>"; echo $msg; //will print the content in the exel page } ?> <?php //index.php page $name="any file name"; echo "<a href='download.php?name=".$name."'>Click to download</a>"; //link to download file ?>