gpt4 book ai didi

magento - 如何在 Magento 中的 CSV 文件中写入一些数据?

转载 作者:行者123 更新时间:2023-12-02 21:05:34 24 4
gpt4 key购买 nike

我正在尝试将一些货件详细信息打印为 CSV 文件,我几乎已完成此操作,但它没有提供下载选项。它会自动创建文件并将其保存在提到的目录中。

我认为我缺少 _prepareDownloadResponse() 函数,但如何实现它。

我正在使用此代码 ->

$file_path = 'sample_shipment.csv';
$mage_csv = new Varien_File_Csv();
$mage_csv->saveData($file_path, $content);

编辑:所有这些代码都在我的indexcontroller.php中完整代码位于indexcontroller.php ->

class Company_Manifestupload_IndexController extends Mage_Adminhtml_Controller_Action
{
public function indexAction() {
echo 'Manifestupload Index!';
}
public function bluedartmanifestuploadAction() {
$request = $this->getRequest();
$shipmentIds = $request->getPost('shipment_ids', array());
$file_path = 'manifest_upload_'.Mage::getSingleton('core/date')->date('d-m-Y_H-i-s').'_csv.csv';
$mage_csv = new Varien_File_Csv();
$shipment_csv=array();

/* Putting data in $shipment_csv[] */
$mage_csv->saveData($file_path, $shipment_csv);
}

最佳答案

为了使 csv 文件可下载,只需将以下代码放入相关的 Controller 操作中:

$fileName       = 'filename.csv';
$content = $csvData; //prepare the csv data and return as string
$this->_prepareDownloadResponse($fileName, $content); //this will make the csv file downloadable with the $content as contents in it.

希望这有帮助。

编辑:请查找更新的 Controller 操作代码。

public function bluedartmanifestuploadAction() {
$request = $this->getRequest();
$shipmentIds = $request->getPost('shipment_ids', array());
$fileName = 'manifest_upload_'.Mage::getSingleton('core/date')->date('d-m-Y_H-i-s').'_csv.csv';

//prepare csv contents
#prepare header
$csv = '';
$_columns = array(
"field1",
"field2",
//...
);
$data = array();
foreach ($_columns as $column) {
$data[] = '"'.$column.'"';
}
$csv .= implode(',', $data)."\n";
#prepare data
foreach($dataFromShipmentIds as $_data){ //just dummy loop
$data = array();
$data[] = $_data['field1'];
$data[] = $_data['field2'];
//...
$csv .= implode(',', $data)."\n";
}
//now $csv varaible has csv data as string

$this->_prepareDownloadResponse($fileName, $csv);
}

编辑2:上面的代码只有在理想情况下才能正常工作。当字段中有“\n”和“,”时,它不会生成正确的Csv。要使其工作正确地,人们可以使用 CSV 文件中的代码。我将在此处添加它-

首先如上所述在 $data[] 中创建值数组,然后使用以下代码代替使用 $csv.=implode(',',$data)."\n"

foreach ($data as $value) {
if (strpos($value, $delimiter) != false ||
strpos($value, $enclosure) != false ||
strpos($value, "\n") != false ||
strpos($value, "\r") != false ||
strpos($value, "\t") != false ||
strpos($value, ' ') != false) {
$str2 = $enclosure;
$escaped = 0;
$len = strlen($value);
for ($i=0;$i<$len;$i++) {
if ($value[$i] == $escape_char) {
$escaped = 1;
} else if (!$escaped && $value[$i] == $enclosure) {
$str2 .= $enclosure;
} else {
$escaped = 0;
}
$str2 .= $value[$i];
}
$str2 .= $enclosure;
$str .= $str2.$delimiter;
} else {
$str .= $enclosure.$value.$enclosure.$delimiter;
}
}
$str = substr($str,0,-1);
$str .= "\n";

关于magento - 如何在 Magento 中的 CSV 文件中写入一些数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11861295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com