php - 我想使用 php 电子表格将数据下载为 CSV 格式
问题描述
add_action('admin_post_learning_profile_csv_download', 'learning_profile_csv_download');
function learning_profile_csv_download(){
$spreadsheet = new Spreadsheet();
$Excel_writer = new Xls($spreadsheet);
$spreadsheet->setActiveSheetIndex(0);
$activeSheet = $spreadsheet->getActiveSheet();
$activeSheet->setCellValue('A1' , 'New file content')->getStyle('A1')->getFont()->setBold(true);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'. $filename .'.csv"');
header('Cache-Control: max-age=0');
$Excel_writer->save('php://output');
$user_id = get_current_user_id();
echo $user_id;
echo '<br>';
$learning_plan = get_user_meta( get_current_user_id(), 'study_guide', true);
if( empty( $learning_plan ) ) {
echo 'No Resources Currently Recommended.';
} else {
foreach( $learning_plan as $item ) {
$resource_comps = get_the_terms( $item, 'ivpt_cc' );
echo get_the_title($item);
foreach( $resource_comps as $comp ){
echo $comp->name;
echo'<br>';
}
}
}
}
这是我的代码,我想将学习计划数据下载为 CSV 格式,分为两列,一次进入标题,另一列用于 comp->name。那么如何在 foreach 循环中设置列和行以进行学习计划呢?
解决方案
对您的 foreach 循环进行一些修改请注意,您不能并行回显任何 html,即带有<br>
标签的回显...
希望有帮助
$title = get_the_title($item);
$csv = "";
foreach ( $resource_comps as $comp ){
$csv .= "'" . $title . "','" . $comp->name . "\r\n";
}
header('Content-type: text/csv');
echo $csv;
推荐阅读
- xamarin.ios - 从命令行构建需要营业执照
- vue.js - vue-router 路由加密(无 webpack)以确保安全
- c# - 为什么 TextBlock 会阻止按钮事件触发?
- assembly - “riscv,plic0”中启用的中断布局取决于什么?
- php - 减少 admin-ajax 请求的数量 - Wordpress
- python - 将excel文件导入python脚本时出现ImportError
- c# - 如何在引用 dll 的 Razor 类库中共享视图组件?
- ios - 如何从您的应用中打开 facebook\instagram 页面 \ 个人资料?(迅速)
- docker - 码头工人 | 使用 VS 2019 在 Azure DevOps 中创建 Docker 映像时出错
- powershell - 需要 windows 脚本从 csv 文件中删除特定列