header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=a.csv");
echo mb_convert_encoding("uid,流水号", 'gbk', 'utf-8');
echo "\r\n";
......
使用PHPExcel保存excel文件
require './lib/Classes/PHPExcel.php';
$phpexcel = new PHPExcel();
$phpexcel->getActiveSheet()->setCellValue('A1', '姓名')
->setCellValue('B1', '姓名2')
->setCellValue('C1', '姓名3')
->setCellValue('D1', '姓名4')
->setCellValue('E1', '姓名5')
->setCellValue('F1', '姓名6')
->setCellValue('G1', '姓名7')
->setCellValue('H1', '姓名8');
->setCellValue('I1', '姓名9');
......
$phpexcel->getActiveSheet()->setCellValueExplicit('H2', '111111111111111',PHPExcel_Cell_DataType::TYPE_STRING);//设置为字符串格式
$phpexcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
$phpexcel->getActiveSheet()->setTitle('XXXX');
$phpexcel->setActiveSheetIndex(0);
$objWriter = PHPExcel_IOFactory::createWriter($phpexcel, 'Excel5');
$objWriter->save('/xxx/a.xls');
//$objWriter = PHPExcel_IOFactory::createWriter($phpexcel, 'Excel2007');
//$objWriter->save('a.xlsx');
使用PHPExcel下载excel文件
require './lib/Classes/PHPExcel.php';
$phpexcel = new PHPExcel();
$phpexcel->setActiveSheetIndex(0)
->setCellValue('A1', '序')
->setCellValue('B1', '名字);
$i = 1;
while(...)
{
$phpexcel->getActiveSheet()->setCellValue('A'.($i+1), $i)
->setCellValue('B'.($i+1), '张三');
++$i;
}
$phpexcel->getActiveSheet()->setTitle('sheet0');
$phpexcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="a.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($phpexcel, 'Excel2007');
$objWriter->save('php://output');
exit;