php - 如何导出到excel?

标签 php mysql database excel export

如何将数据库导出到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();

 ?>

这不工作,有一个错误

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/

相关文章:

php mysql 查询 : multiple drop down options

php - 如何确保为空表单字段发送 null 而不是 0

mysql - SQL 选择一行,就好像它没有空格一样

node.js - Firebase 堆栈 "Error: too many index entries for entity"

php - MySQL保存数据时添加时间戳

javascript - 带文件上传的多步骤表单

php - Symfony2 注册 MySQL soundex 失败?

php - 在 PHP/MySql 中对来自不同查询的表数据进行排序

mysql - 查询以计算重复的参与者名称并仅显示一次参与者名称与计数

sql - 设置用户表时有什么好的做法?查看一些新手教程,但不确定如何正确执行 "really"