我正在使用敏捷工具包atk4,我想将网格导出为CSV文件,我正在使用下面的代码,我遇到的问题是下面的细节:
代码:
$grid->menu->addItem(['Export as CSV..', 'icon' => 'add square'], new \atk4\csv\Export($model))->saveAsCSV('file.csv', ['id', 'name']);问题:错误:找不到类'atk4\csv\Export‘。意味着在agiletoolkit atk4文件夹中找不到导出库。
我正在使用这篇文章:https://github.com/atk4/csv
想寻求帮助吗?
注意:我已经用不同的方法完成了下面的细节:
if (isset($_GET['export'])) {
header("Location: exportCSV.php?exportid=".@$_GET['export']);
exit();
}
$button =$grid->menu->addItem(['Export as CSV..', 'icon' => 'add square']);
$button->on('click', null, function ($b) use($app) {
return [$app->jsRedirect(['export' =>'export'])];
//return 'success';
});在exportCSV.php文件中
$query = "SELECT id,name,address FROM tableName;";
$result = mysqli_query($conn, $query) or die("database error:". mysqli_error($conn));
$records = array();
while( $rows = mysqli_fetch_assoc($result) ) {
$records[] = $rows;
}
if(isset($records)) {
$csv_file = "csv_export_".date('Ymd') . ".csv";
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=\"$csv_file\"");
$fh = fopen( 'php://output', 'w' );
$is_coloumn = true;
if(!empty($records)) {
foreach($records as $record) {
if($is_coloumn) {
fputcsv($fh, array_keys($record));
$is_coloumn = false;
}
fputcsv($fh, array_values($record));
}
fclose($fh);
}
exit;
}发布于 2021-11-04 17:06:03
正如你在/atk4/csv github仓库中看到的--它从未实现过。它只包含自述文件,仅此而已:)
上面的解决方案没有使用atk4模型,而是使用普通的mysqli连接。如果它可以工作,这是可以的,但是如果您的项目依赖于atk4模型,那么您可能应该改为导出atk4模型。
要将数据导出到CSV文件,您可以执行以下操作:
$records = $grid->model->export();
if(isset($records)) {
... here goes your code to save data in CSV file
}或者,如果您想要功能齐全的解决方案,则可以将此特征添加到您的数据模型中:
<?php
/**
* Trait for exporting model data as CSV file.
*
* Usage:
* Add this trait in your model. And then simply use it like this:
* $model->exportCSV('filename_without_extension', [fields_to_export]);
*/
trait Trait_ModelExportCSV
{
// CSV configuration
public $csv_delimiter = ',';
public $csv_enclosure = '"';
public $csv_escape_char = "\\";
public function exportCSV($filename, $fields = [])
{
$fields = $fields ?: array_keys($this->getFields);
// HTTP header
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=$filename.csv");
header("Pragma: no-cache");
header("Expires: 0");
$output = fopen("php://output", "w");
fprintf($output, chr(0xEF).chr(0xBB).chr(0xBF)); // add BOM for UTF-8 support
// save CSV header
$header = [];
foreach ($fields as $f) {
$header[] = $this->getField($f)->getCaption();
}
fputcsv($output, $header, $this->csv_delimiter, $this->csv_enclosure, $this->csv_escape_char);
// save CSV rows
foreach ($this as $row) {
$r = [];
foreach ($fields as $f) {
$v = $row[$f];
if ($this->getField($f)->type == 'boolean') {
$v = $v === true ? "Y" : "N";
}
if ($v instanceof \DateTime) {
$v = $v->format('Y-m-d');
}
$r[] = $v;
}
fputcsv($output, $r, $this->csv_delimiter, $this->csv_enclosure, $this->csv_escape_char);
}
// close CSV file
fclose($output);
exit;
}
}这是针对较旧的atk4版本,但您可以了解它是如何完成的,并调整它以匹配您正在使用的atk4版本。
https://stackoverflow.com/questions/69839640
复制相似问题