如何导出为Excel?

如何导出数据库到Excel? 我有这样的桌子:

+---------------------------------------------------------------------------------------+ id |title ---------------------------------------------------------------------------------------- 52 | Book A 54 | <div editor_id="mce_editor_0" align="justify">Pembayaran Dividen PT. Telkom Tbk. untuk Tahun Buku 2007 Tahap I akan dilaksanakan mulai tanggal&nbsp; 18 Desember 2007 dengan mempergunakan Surat Pemberitahuan Pembayaran Dividen (SPPD) sebagai bukti pembayaran.</div> +---------------------------------------------------------------------------------------+ 

我有这样的出口脚本:

 <?php // nama file $namaFile = "report.xls"; // Function penanda awal file (Begin Of File) Excel function xlsBOF() { echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); return; } // Function penanda akhir file (End Of File) Excel function xlsEOF() { echo pack("ss", 0x0A, 0x00); return; } // Function untuk menulis data (angka) ke cell excel function xlsWriteNumber($Row, $Col, $Value) { echo pack("sssss", 0x203, 14, $Row, $Col, 0x0); echo pack("d", $Value); return; } // Function untuk menulis data (text) ke cell excel function xlsWriteLabel($Row, $Col, $Value ) { $L = strlen($Value); echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L); echo $Value; return; } // header file excel 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/x-msdownload"); // header untuk nama file header("Content-Disposition: attachment; filename=".$namaFile.""); //header("Content-Transfer-Encoding: binary "); // memanggil function penanda awal file excel xlsBOF(); // ------ membuat kolom pada excel --- // // mengisi pada cell A1 (baris ke-0, kolom ke-0) xlsWriteLabel(0,0,"ID"); xlsWriteLabel(0,1,"TITLE"); // koneksi ke mysql mysql_connect("localhost", "root", ""); mysql_select_db("db_lama"); // query menampilkan semua data $query = "SELECT * FROM bf_articles "; $hasil = mysql_query($query); // nilai awal untuk baris cell $noBarisCell = 1; // nilai awal untuk nomor urut data $noData = 1; while ($data = mysql_fetch_array($hasil)) { // menampilkan no. urut data xlsWriteNumber($noBarisCell,0,$data['id']); xlsWriteLabel($noBarisCell,1,$data['title']); // increment untuk no. baris cell dan no. urut data $noBarisCell++; $noData++; } // memanggil function penanda akhir file excel xlsEOF(); exit(); ?> 

这不起作用,有一个错误

文件错误:数据可能已经丢失

我能做什么 ? 请帮帮我 :(

可能是你的运行时问题。 否则使用这个链接。 它有,

 At first: Try to track where exactly it arrears(row) and check the length of string in cell(should be only < 255) This module has some nasty moments. By default the limit had been set in constructor of Spreadsheet_Excel_Writer_BIFFwriter($this->_limit = 2080;) ~20kb. Change it to size what you need. 

其他方式:1)出口像 ,

 <?php $file="demo.xls"; $test="<table ><tr><td>Cell 1</td><td>Cell 2</td></tr></table>"; header("Content-type: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename=$file"); echo $test; ?> 

2)从这个链接获得帮助。

那么我通常使用PHPExcel( http://phpexcel.codeplex.com/ )来做到这一点,但是您也可以通过设置标题手动执行此操作。 如果你需要格式化,字体样式等,然后使用PHPExcel,否则做一个CSVstring,并设置内容头吐出一个.xls文件,它应该读得很好。

你可以很容易地做到这一点,如前所述(虽然这不是你的问题)。 如果你想把它做成CSV,你可以使用前面的例子,或者这是我很久以前写的:

http://pastebin.ws/8fbfco