Import/Export 个来自 PHP 的文件
Import/Export file from PHP
我认为当我问我的问题(问题)时,我的解释不够清晰。
我有一个项目 PHP,我没有本地环境,我正在在线工作:我通过 Netbeans 更改了我的文件,并且我在没有本地测试的情况下部署了它。我的服务器也是服务器中的 production.My 数据库(MySQL - phpMyadmin)。
我将数据从我的磁盘导入到项目中。然后 PHP 通过 HighChart 利用它。用户可以根据多种选择(日期、城市、客户等)查看这些数据,最后他可以将最终结果下载到文件 csv 中。
这里的问题是我无法下载所有数据,例如:图表中的 Turnover 显示 1400 万,但在文件 csv 中显示的营业额较小。
下面是我的代码:
class Export{
private $_dataIN=[];
private $_author;
private $_filepath;
public function __construct(array $dataIN) {
$this->_dataIN = $dataIN;
$this->setFilepath();
$this->prepareFile();
}
public function setFilepath() {
$date = new \DateTime();
$this->_filepath='../DATA/ExportFiles/ExportData'.$date->getTimestamp();
}
public function prepareFile(){
$file = fopen($this->_filepath, 'w');
fputcsv($file, array('Piece', 'Client', 'Libelle Client', 'Facturation','OTP','Typcde','Cdecli','Cdesap','Article','Designation','Dev','Montant_fac_dev_ht','Montant_fac_eur','Avion','Nature','Repsite','Zone','LRU','Contract'),';');
foreach ($this->_dataIN as $key => $statement) {
fputcsv($file, array($statement->getId(), $statement->getCustomerId(), $statement->getCustomerName(), $statement->getDate(), $statement->getOTP(), $statement->getCdetype(), $statement->getCdecustomer(), $statement->getSAPcode(), $statement->getArticle(), $statement->getDesigniation(), $statement->getCurrency(), $statement->getAmount(), $statement->getAmountEur(), $statement->getAircraft(), $statement->getNature(), $statement->getRepsite(), $statement->getZone(), $statement->getLru(),$statement->getContract()),';');
}
}
public function download() {
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//octet-stream'
header('Content-Disposition: attachment; filename='.basename($this->_filepath).'.csv');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($this->_filepath) . 'Giga'); //Kilo
readfile($this->_filepath);
exit();
我希望这次我的解释是干净的,你能帮忙吗?
最好这样使用而不存储:
public function __construct($dataIN = array()) {
$this->_dataIN = $dataIN;
$this->prepareFile();
}
public function prepareFile(){
$header[] = array('Piece', 'Client', 'Libelle Client', 'Facturation','OTP','Typcde','Cdecli','Cdesap','Article','Designation','Dev','Montant_fac_dev_ht','Montant_fac_eur','Avion','Nature','Repsite','Zone','LRU','Contract');
$data = array();
foreach ($this->_dataIN as $key => $statement) {
$data[] = array($statement->getId(), $statement->getCustomerId(), $statement->getCustomerName(), $statement->getDate(), $statement->getOTP(), $statement->getCdetype(), $statement->getCdecustomer(), $statement->getSAPcode(), $statement->getArticle(), $statement->getDesigniation(), $statement->getCurrency(), $statement->getAmount(), $statement->getAmountEur(), $statement->getAircraft(), $statement->getNature(), $statement->getRepsite(), $statement->getZone(), $statement->getLru(),$statement->getContract());
}
$final_data = array_merge($header, $data);
$this->array_to_csv($final_data, 'report.csv');
}
function array_to_csv($array, $download = "")
{
if ($download != "")
{
header('Content-Description: File Transfer');
header("Content-type: application/vnd.ms-excel");
header('Content-Disposition: attachement; filename="' . $download . '"');
header('Content-Transfer-Encoding: binary');
}
ob_start();
$f = fopen('php://output', 'w') or show_error("Can't open php://output");
$n = 0;
foreach ($array as $line)
{
$n++;
if ( ! fputcsv($f, $line))
{
show_error("Can't write line $n: $line");
}
}
fclose($f) or show_error("Can't close php://output");
$str = ob_get_contents();
ob_end_clean();
if ($download == "")
{
return $str;
}
else
{
print "\xEF\xBB\xBF"; // UTF-8 BOM
print $str;
}
}
我认为当我问我的问题(问题)时,我的解释不够清晰。 我有一个项目 PHP,我没有本地环境,我正在在线工作:我通过 Netbeans 更改了我的文件,并且我在没有本地测试的情况下部署了它。我的服务器也是服务器中的 production.My 数据库(MySQL - phpMyadmin)。 我将数据从我的磁盘导入到项目中。然后 PHP 通过 HighChart 利用它。用户可以根据多种选择(日期、城市、客户等)查看这些数据,最后他可以将最终结果下载到文件 csv 中。 这里的问题是我无法下载所有数据,例如:图表中的 Turnover 显示 1400 万,但在文件 csv 中显示的营业额较小。
下面是我的代码:
class Export{
private $_dataIN=[];
private $_author;
private $_filepath;
public function __construct(array $dataIN) {
$this->_dataIN = $dataIN;
$this->setFilepath();
$this->prepareFile();
}
public function setFilepath() {
$date = new \DateTime();
$this->_filepath='../DATA/ExportFiles/ExportData'.$date->getTimestamp();
}
public function prepareFile(){
$file = fopen($this->_filepath, 'w');
fputcsv($file, array('Piece', 'Client', 'Libelle Client', 'Facturation','OTP','Typcde','Cdecli','Cdesap','Article','Designation','Dev','Montant_fac_dev_ht','Montant_fac_eur','Avion','Nature','Repsite','Zone','LRU','Contract'),';');
foreach ($this->_dataIN as $key => $statement) {
fputcsv($file, array($statement->getId(), $statement->getCustomerId(), $statement->getCustomerName(), $statement->getDate(), $statement->getOTP(), $statement->getCdetype(), $statement->getCdecustomer(), $statement->getSAPcode(), $statement->getArticle(), $statement->getDesigniation(), $statement->getCurrency(), $statement->getAmount(), $statement->getAmountEur(), $statement->getAircraft(), $statement->getNature(), $statement->getRepsite(), $statement->getZone(), $statement->getLru(),$statement->getContract()),';');
}
}
public function download() {
header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//octet-stream'
header('Content-Disposition: attachment; filename='.basename($this->_filepath).'.csv');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($this->_filepath) . 'Giga'); //Kilo
readfile($this->_filepath);
exit();
我希望这次我的解释是干净的,你能帮忙吗?
最好这样使用而不存储:
public function __construct($dataIN = array()) {
$this->_dataIN = $dataIN;
$this->prepareFile();
}
public function prepareFile(){
$header[] = array('Piece', 'Client', 'Libelle Client', 'Facturation','OTP','Typcde','Cdecli','Cdesap','Article','Designation','Dev','Montant_fac_dev_ht','Montant_fac_eur','Avion','Nature','Repsite','Zone','LRU','Contract');
$data = array();
foreach ($this->_dataIN as $key => $statement) {
$data[] = array($statement->getId(), $statement->getCustomerId(), $statement->getCustomerName(), $statement->getDate(), $statement->getOTP(), $statement->getCdetype(), $statement->getCdecustomer(), $statement->getSAPcode(), $statement->getArticle(), $statement->getDesigniation(), $statement->getCurrency(), $statement->getAmount(), $statement->getAmountEur(), $statement->getAircraft(), $statement->getNature(), $statement->getRepsite(), $statement->getZone(), $statement->getLru(),$statement->getContract());
}
$final_data = array_merge($header, $data);
$this->array_to_csv($final_data, 'report.csv');
}
function array_to_csv($array, $download = "")
{
if ($download != "")
{
header('Content-Description: File Transfer');
header("Content-type: application/vnd.ms-excel");
header('Content-Disposition: attachement; filename="' . $download . '"');
header('Content-Transfer-Encoding: binary');
}
ob_start();
$f = fopen('php://output', 'w') or show_error("Can't open php://output");
$n = 0;
foreach ($array as $line)
{
$n++;
if ( ! fputcsv($f, $line))
{
show_error("Can't write line $n: $line");
}
}
fclose($f) or show_error("Can't close php://output");
$str = ob_get_contents();
ob_end_clean();
if ($download == "")
{
return $str;
}
else
{
print "\xEF\xBB\xBF"; // UTF-8 BOM
print $str;
}
}