PHP导出Excel

我再这里用的是PHPExcel,可以去PHPExcel官网下载文件,首先引用PHPExcel, 导出代码如下:

[php]

<?php
//引入PHPExcel库文件(路径根据自己情况)

include ‘../phpexcel/Classes/PHPExcel.php’;
include "../include/Connect.php";
//创建对象
$resultPHPExcel = new PHPExcel();
$resultPHPExcel->getActiveSheet()->setCellValue(‘A1’, ‘序號’);
$resultPHPExcel->getActiveSheet()->setCellValue(‘B1’, ‘姓名’);
$resultPHPExcel->getActiveSheet()->setCellValue(‘C1’, ‘手機號碼’);
$resultPHPExcel->getActiveSheet()->setCellValue(‘D1’, ‘密碼’);
$resultPHPExcel->getActiveSheet()->setCellValue(‘E1’, ‘職位’);
$resultPHPExcel->getActiveSheet()->setCellValue(‘F1’, ‘群組’);
$resultPHPExcel->getActiveSheet()->setCellValue(‘G1’, ‘備註’);
//设置列宽
$resultPHPExcel->getActiveSheet()->getColumnDimension(‘C’)->setWidth(’15’);
$resultPHPExcel->getActiveSheet()->getColumnDimension(‘D’)->setWidth(’15’);
$resultPHPExcel->getActiveSheet()->getColumnDimension(‘E’)->setWidth(’10’);
$resultPHPExcel->getActiveSheet()->getColumnDimension(‘F’)->setWidth(’30’);
$resultPHPExcel->getActiveSheet()->getColumnDimension(‘G’)->setWidth(’50’);
//表格数组

$sql="SELECT * FROM `htx_user`";
$result=$db->getAll($sql);
$i = 2;
$qa=1;
for($dx=0;$dx<count($result);$dx++)
{
$resultPHPExcel->getActiveSheet()->setCellValue(‘A’ . $i, $qa);
$resultPHPExcel->getActiveSheet()->setCellValueExplicit(‘B’.$i,$result[$dx][‘UserName’],PHPExcel_Cell_DataType::TYPE_STRING);//设置文本框为文本 避免内容为数字时格式不对齐或首字符为0时不显示0
$resultPHPExcel->getActiveSheet()->setCellValueExplicit(‘C’.$i,$result[$dx][‘IMEI’],PHPExcel_Cell_DataType::TYPE_STRING);
$resultPHPExcel->getActiveSheet()->setCellValueExplicit(‘D’.$i,$result[$dx][‘UserMobile’],PHPExcel_Cell_DataType::TYPE_STRING);
if ($result[$dx][‘Website’]==1) {
$sd="高級工程師";
} elseif ($result[$dx][‘type’]==2) {
$sd="工程師";
} elseif ($result[$dx][‘type’]==3) {
$sd="技術員";
}
$resultPHPExcel->getActiveSheet()->setCellValue(‘E’ . $i, $sd);

$sqn="SELECT GroupName FROM htx_user_group where GroupID in(select grouid from htx_groupuser where userid=".$result[$dx][‘UserID’].")";
$res=$db->getAll($sqn);
$group="";
for($d=0;$d<count($res);$d++)
{
$group .=$res[$d][‘GroupName’]."&nbsp;&nbsp;";

}

$resultPHPExcel->getActiveSheet()->setCellValueExplicit(‘F’.$i, $group,PHPExcel_Cell_DataType::TYPE_STRING);
$resultPHPExcel->getActiveSheet()->setCellValue(‘G’ . $i, $result[$dx][‘UserIntro1’]);
$qa ++;
$i ++;

}

//设置导出文件名

$outputFileName = "用戶資料".date("Ymj").".xls";

$xlsWriter = new PHPExcel_Writer_Excel5($resultPHPExcel);

//ob_start(); ob_flush();

header("Content-Type: application/force-download");

header("Content-Type: application/octet-stream");

header("Content-Type: application/download");

header(‘Content-Disposition:inline;filename="’.$outputFileName.’"’);

header("Content-Transfer-Encoding: binary");

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

header("Pragma: no-cache");

$xlsWriter->save( "php://output" );
?>

[/php]

 

您可能还喜欢...

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注