PHP数据导出Excel

需要将php数据导出Exel表格中,可以用PHPExcel,但是也有更快的方法,但是会出现些小问题

比如:

/* 
*处理Excel导出 
*@param $datas array 设置表格数据 
*@param $titlename string 设置head 
*@param $title string 设置表头 
*/ 
/**
* 
*/

function excelData($datas,$titlename,$title,$filename){ 
    $str = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\nxmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\nxmlns=\"http://www.w3.org/TR/REC-html40\">\r\n<head>\r\n<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body>"; 
    $str .= $title; 
    $str .="<table border=1><head>".$titlename."</head>"; 
    foreach ($datas  as $key=> $rt ) 
    { 
        $str .= "<tr>"; 
        foreach ( $rt as $k => $v ) 
        { 
            $str .= "<td>{$v}</td>"; 
        } 
        $str .= "</tr>\n"; 
    } 
    $str .= "</table></body></html>"; 
    header( "Content-Type: application/vnd.ms-excel; name='excel'" ); 
    header( "Content-type: application/octet-stream" ); 
    header( "Content-Disposition: attachment; filename=".$filename ); 
    header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" ); 
    header( "Pragma: no-cache" ); 
    header( "Expires: 0" ); 
    exit( $str ); 
} 
  

实例:

$dataResult=[[1,2,3,4,5,6]];  //导出数据
$headTitle = "测试excel项目记录"; 
$title = "测试excel"; 
$headtitle= "<tr style='height:50px;border-style:none;'><th border=\"0\" style='height:60px;width:270px;font-size:22px;' colspan='11' >{$headTitle}</th></tr>"; 
$titlename = "<tr> 
               <th style='width:70px;' >项目1</th> 
               <th style='width:70px;' >项目2</th> 
               <th style='width:70px;'>项目3</th> 
               <th style='width:150px;'>项目4</th> 
               <th style='width:70px;'>项目5</th> 
               <th style='width:70px;'>项目6</th> 
           </tr>"; 
           $filename = $title.".xls"; 
excelData($dataResult,$titlename,$headtitle,$filename); 



实践可以使用,自己封装

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHP可以使用PHPExcel库来导出MySQL数据Excel。 步骤如下: 1. 首先需要连接MySQL数据库,可以使用mysqli或PDO等扩展库。 2. 查询需要导出数据,将结果存储到一个数组中。 3. 引入PHPExcel库,创建一个PHPExcel对象。 4. 设置Excel的属性,如标题、作者、描述等。 5. 创建一个工作表,并设置表头。 6. 将查询结果填充到工作表中。 7. 设置Excel的输出格式为Excel2007,并输出Excel文件。 示例代码如下: ```php //连接MySQL数据库 $conn = mysqli_connect("localhost", "username", "password", "database"); //查询需要导出数据 $sql = "SELECT * FROM table"; $result = mysqli_query($conn, $sql); $data = array(); while ($row = mysqli_fetch_assoc($result)) { $data[] = $row; } //引入PHPExcel库 require_once 'PHPExcel.php'; //创建PHPExcel对象 $objPHPExcel = new PHPExcel(); //设置Excel属性 $objPHPExcel->getProperties()->setTitle("MySQL数据导出")->setCreator("Your Name")->setDescription("MySQL数据导出"); //创建工作表 $objPHPExcel->setActiveSheetIndex(); $objPHPExcel->getActiveSheet()->setTitle("Sheet1"); //设置表头 $objPHPExcel->getActiveSheet()->setCellValue('A1', 'ID'); $objPHPExcel->getActiveSheet()->setCellValue('B1', 'Name'); $objPHPExcel->getActiveSheet()->setCellValue('C1', 'Age'); //填充数据 foreach ($data as $key => $value) { $row = $key + 2; $objPHPExcel->getActiveSheet()->setCellValue('A' . $row, $value['id']); $objPHPExcel->getActiveSheet()->setCellValue('B' . $row, $value['name']); $objPHPExcel->getActiveSheet()->setCellValue('C' . $row, $value['age']); } //设置输出格式为Excel2007 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); //输出Excel文件 header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="data.xlsx"'); header('Cache-Control: max-age='); $objWriter->save('php://output'); ``` 以上代码将查询结果导出Excel文件,并以附件形式下载。可以根据需要修改代码,如更改查询语句、表头等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值