ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## php第三方库: * PHPExcel * [**php_xlsxwriter**](https://gitee.com/mirrors/php_xlsxwriter) [php_xlsxwriter](https://gitee.com/mirrors/php_xlsxwriter "php_xlsxwriter")例子; ``` //writer 类 $writer = new XLSXWriter(); //文件名 $filename = "example.xlsx"; //设置 header,用于浏览器下载 header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"'); header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate'); header('Pragma: public'); //导出的数据 $string = array ( 0 => array ( 'payc_bill_time' => '2017-07-12 16:40:44', 'payt_received_date' => '2017-07-12', 'ci_name' => '租金', 'payt_num' => 'YRZB(2012)A0047', 'payt_scsr_name' => '李巧红', 'payt_received' => '300.00', 'paytd_type' => '现金', 'emp_name' => '郑振标', ), 1 => array ( 'payc_bill_time' => '2017-07-12 16:39:55', 'payt_received_date' => '2017-07-12', 'ci_name' => '租金', 'payt_num' => 'YRZB(2012)A0046', 'payt_scsr_name' => '22222', 'payt_received' => '45.00', 'paytd_type' => '现金', 'emp_name' => '郑振标', ), ... ... ); //每列的标题头 $title = array ( 0 => '开单时间', 1 => '收款时间', 2 => '开票项目', 3 => '票据编号', 4 => '客户名称', 5 => '实收金额', 6 => '收款方式', 7 => '收款人', ); //工作簿名称 $sheet1 = 'sheet1'; //对每列指定数据类型,对应单元格的数据类型 foreach ($title as $key => $item){ $col_style[] = $key ==5 ? 'price': 'string'; } //设置列格式,suppress_row: 去掉会多出一行数据;widths: 指定每列宽度 $writer->writeSheetHeader($sheet1, $col_style, ['suppress_row'=>true,'widths'=>[20,20,20,20,20,20,20,20]] ); //写入第二行的数据,顺便指定样式 $writer->writeSheetRow($sheet1, ['xxx财务报表'], ['height'=>32,'font-size'=>20,'font-style'=>'bold','halign'=>'center','valign'=>'center']); /*设置标题头,指定样式*/ $styles1 = array( 'font'=>'宋体','font-size'=>10,'font-style'=>'bold', 'fill'=>'#eee', 'halign'=>'center', 'border'=>'left,right,top,bottom'); $writer->writeSheetRow($sheet1, $title,$styles1); // 最后是数据,foreach写入 foreach ($data as $value) { foreach ($value as $item) { $temp[] = $item; } $rows[] = $temp; unset($temp); } $styles2 = ['height'=>16]; foreach($rows as $row){ $writer->writeSheetRow($sheet1, $row,$styles2); } //合并单元格,第一行的大标题需要合并单元格 $writer->markMergedCell($sheet1, $start_row=0, $start_col=0, $end_row=0, $end_col=7); //输出文档 $writer->writeToStdOut(); exit(0); ```