如何将数据库导出到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 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();
?>
这不工作,有一个错误
file error: data may have been lost
我能做什么?请帮助我:(
最佳答案
可能是您的运行时问题。否则使用 this link.它有,
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)导出like ,
<?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) 从this link 获得帮助.
关于php - 如何导出到excel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18841453/