我想使用php电子表格将数据下载到CSV格式 add_action('admin_post_learning_profile_csv_download'、'learning_profile_csv_download'); 函数学习\u配置文件\u csv\u下载(){ $spreadsheet=新电子表格(); $Excel\u writer=新Xls($spreadsheet); $spreadsheet->setActiveSheetIndex(0); $activeSheet=$spreadsheet->getActiveSheet(); $activeSheet->setCellValue('A1','New file content')->getStyle('A1')->getFont()->setBold(true); 标题('Content-Type:application/vnd.ms-excel'); 标题('Content-Disposition:attachment;filename=“.”.$filename..csv“); 标头('Cache-Control:max age=0'); $Excel\u writer->保存php://output'); $user\u id=get\u current\u user\u id(); echo$user\u id; 回声“”; $learning\u plan=get\u user\u meta(get\u current\u user\u id(),'study\u guide',true); if(空($learning_plan)){ echo“目前没有推荐的资源”; }否则{ foreach($学习计划作为$项目){ $resource_comps=获取术语($item,'ivpt_cc'); echo获取标题($item); foreach($resource\u comps as$comp){ echo$comp->name; 回声“”; } } } }

我想使用php电子表格将数据下载到CSV格式 add_action('admin_post_learning_profile_csv_download'、'learning_profile_csv_download'); 函数学习\u配置文件\u csv\u下载(){ $spreadsheet=新电子表格(); $Excel\u writer=新Xls($spreadsheet); $spreadsheet->setActiveSheetIndex(0); $activeSheet=$spreadsheet->getActiveSheet(); $activeSheet->setCellValue('A1','New file content')->getStyle('A1')->getFont()->setBold(true); 标题('Content-Type:application/vnd.ms-excel'); 标题('Content-Disposition:attachment;filename=“.”.$filename..csv“); 标头('Cache-Control:max age=0'); $Excel\u writer->保存php://output'); $user\u id=get\u current\u user\u id(); echo$user\u id; 回声“”; $learning\u plan=get\u user\u meta(get\u current\u user\u id(),'study\u guide',true); if(空($learning_plan)){ echo“目前没有推荐的资源”; }否则{ foreach($学习计划作为$项目){ $resource_comps=获取术语($item,'ivpt_cc'); echo获取标题($item); foreach($resource\u comps as$comp){ echo$comp->name; 回声“”; } } } },php,wordpress,phpspreadsheet,Php,Wordpress,Phpspreadsheet,这是我的代码,我想将学习计划数据下载为CSV格式,分为两列,一列为标题,另一列为comp->name。那么我如何在foreach循环中为学习计划设置一列和一行呢?对foreach循环进行一些修改 请注意,您不能并行地回显任何html,即使用标记进行回显 希望有帮助 add_action('admin_post_learning_profile_csv_download', 'learning_profile_csv_download'); function learning_pr

这是我的代码,我想将学习计划数据下载为CSV格式,分为两列,一列为标题,另一列为comp->name。那么我如何在foreach循环中为学习计划设置一列和一行呢?

对foreach循环进行一些修改 请注意,您不能并行地回显任何html,即使用

标记进行回显

希望有帮助

    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>';
                }
            }
        }
    }

请您详细说明如何在activesheet中设置值。$csv是一个包含输出的字符串。其中的每一行都将保存您定义的信息。Csv是逗号分隔的值,因此,基本上,在每个循环迭代中,我们将向$Csv添加一行。在单引号之间保留每个值,用php结尾结束每一行。
$title = get_the_title($item);
$csv = "";

foreach ( $resource_comps as $comp ){
 $csv .= "'" . $title . "','" . $comp->name . "\r\n";
}
header('Content-type: text/csv');
echo $csv;