引入类库XLSXWriter

以PHPCMS中获取栏目下所有子栏目/文章域名生成excel表格为例:

/**
 * 获取域名
 * 
 * @author Messiah 
 * @version v1.0.0  2023.12.11
 * @param int $parentid 栏目id
 * @return object
*/
public function getUrl()
{    
    $catid = $_GET["parentid"];
      
    // 获取当前栏目的arrchildid、modelid
    $category = $this->model->get_one(["catid"=>$catid]);
    if($category){
        $arrchildid = $category['arrchildid'];
        $modelid = $category['modelid'];
    }else{
        showmessage(L('未找到该栏目下数据'), '?m=admin&c=category&a=init', 3000);
    };
      
    if($modelid){
      // 获取表名tablename
      $sql = "select tablename from v9_model where modelid = $modelid";
      $this->db->query($sql);
      $arr = $this->db->fetch_array();
      if($arr){
          $tablename = $arr[0]['tablename'];
      }else{
          showmessage(L('未找到模型数据'), '?m=admin&c=category&a=init', 3000);
      };

      // 获取当前栏目下所有文章url、title
      $sql = "select title,url from v9_".$tablename." where catid in($arrchildid)";
      $this->db->query($sql);
      $arr = $this->db->fetch_array();

      $data = $arr;


      // 获取当前栏目下所有子栏目url、title
      $sql = "select catname,url from v9_category where catid in($arrchildid)";
      $this->db->query($sql);
      $arr = $this->db->fetch_array();
      if($arr){
          foreach($arr as $v){
              $push = ["title"=>$v["catname"],"url"=>$v["url"]];
              array_push($data,$push);
          }
      };
    }else{
        // 单页
        $data = [
            ["title"=>$category['catname'],"url"=>$category['url']]
        ];
    }
  
    $result = $this->setXlsx($data,$category['catname']);
}

/**
 * 生成Excel表格
 * 
 * @author Messiah 
 * @version v1.0.0  2023.12.11
 * @param array $data 数据
 * @param string $title 标题
 * @return object
*/
public function setXlsx($data=[],$title="")
{    
    if(!$data){
        showmessage(L('数据错误'), '?m=admin&c=category&a=init', 3000);
    };
    require_once "vendor/PHP_XLSXWriter-0.39/xlsxwriter.class.php";
    $writer = new XLSXWriter();
    $writer->setAuthor('Messiah');

    $sheet_name = 'Sheet1';
    $writer->writeSheetHeader($sheet_name, array('栏目标题/文章名称'=> 'string', 'URL'=> 'string'));

    foreach($data as $row) {
        $writer->writeSheetRow($sheet_name, $row);
    }

    $filename = $title . " - 栏目下URL数据 - " . date("YmdHis",time()) .'.xlsx';
    $writer->writeToFile($filename);
      // 告诉浏览器输出的内容类型为 Excel
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    // 告诉浏览器文件的大小和名称
    header('Content-Disposition: attachment;filename="'. $filename .'"');

    $writer->writeToStdOut();

    $this->folder($filename);// 移动文件
}

/**
 * 转移文件
 * @author Messiah 
 * @version v1.0.0  2023.12.12
 * @param string $fileName 文件名
 * @return object
 */
public function folder($fileName)
{
    $log_folder = 'xlsxFile/' . date('Y');
    if (!file_exists($log_folder)) {
        mkdir($log_folder, 0777, true);
    }

    $log_folder .= '/' . date('m');
    if (!file_exists($log_folder)) {
        mkdir($log_folder, 0777, true);
    }
  
      // 移动文件至新目录
    $source_file = $fileName;
    $destination_file = $log_folder . '/' . $source_file;
    if (file_exists($source_file)) {
        rename($source_file, $destination_file);
    }
}