扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
1.
创新互联公司 - 成都IDC机房托管,四川服务器租用,成都服务器租用,四川网通托管,绵阳服务器托管,德阳服务器托管,遂宁服务器托管,绵阳服务器托管,四川云主机,成都云主机,西南云主机,成都IDC机房托管,西南服务器托管,四川/成都大带宽,大带宽服务器,四川老牌IDC服务商
这属于php的技术;
2.
php可以用PHPExcel直接导出成excel文件;
代码如下:
require_once("../lib/excelcreator.class.php");
$myxls
=
new
ExcelCreator
("中文Excel");
$aTableHead
=
'
Row
ss:AutoFitHeight="0"
CellData
ss:Type="String"Name3/Data/Cell
CellData
ss:Type="String"Surname3/Data/Cell
/Row';
$aTableBody
=
'
Row
ss:AutoFitHeight="0"
CellData
ss:Type="String"Schwarz3/Data/Cell
CellData
ss:Type="String"Oliver3/Data/Cell
/Row';
$aTableBottom
=
'
Row
ss:AutoFitHeight="0"
CellData
ss:Type="String"123/Data/Cell
CellData
ss:Type="String"Peter3/Data/Cell
/Row';
$workSheet
=
$myxls-createWorkSheet
(
"中文sheet1",
$aTableHead,
$aTableBody,
$aTableBottom
);
echo
$myxls-createExcel
(
$workSheet
);
1
一、下载PHPExcel包
2
二、新建文件夹,命名为PHPExcel,将上面两个文件PHPExcel,PHPExcel.php放到我们新建的PHPExcel文件夹下面;然后将此文件夹放在thinkPHP核心包里面,路径如下:D:\***\你的项目\ThinkPHP\Library\Vendor
3
三、前端界面代码
div style="margin-left: 100px;margin-top: -33px;"
form action="{:U(output)}" enctype="multipart/form-data" method="post"
input type="file" name="photo" /
input type="submit" value="导出数据" style="background-color: #337AB7;color: white;"
/form
/div
4
四、导出表格样式代码
//商家表格导出模板
public function exportExcel($expTitle, $expCellName, $expTableData) {
$xlsTitle = iconv('utf-8', 'gb2312', $expTitle); //文件名称
$fileName = '网站商家信息表' . date('_YmdHis'); //or $xlsTitle 文件名称可根据自己情况设定
$cellNum = count($expCellName);
$dataNum = count($expTableData);
vendor("PHPExcel.PHPExcel");
$objPHPExcel = new\PHPExcel();
$cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ',
'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV',
'AW', 'AX', 'AY', 'AZ');
$objPHPExcel-getActiveSheet()-getColumnDimension('F')-setWidth(22);
$objPHPExcel-getActiveSheet()-getColumnDimension('D')-setWidth(10);
$objPHPExcel-getActiveSheet()-getColumnDimension('G')-setWidth(10);
$objPHPExcel-getActiveSheet()-getColumnDimension('H')-setWidth(15);
$objPHPExcel-getActiveSheet()-getColumnDimension('A')-setWidth(10);
$objPHPExcel-getActiveSheet()-getColumnDimension('B')-setWidth(15);
$objPHPExcel-getActiveSheet()-getColumnDimension('C')-setWidth(20);
$objPHPExcel-getActiveSheet()-getColumnDimension('E')-setWidth(15);
$objPHPExcel-getActiveSheet()-getColumnDimension('I')-setWidth(20);
$objPHPExcel-getActiveSheet()-getColumnDimension('J')-setWidth(10);
$objPHPExcel-getActiveSheet()-getColumnDimension('K')-setWidth(20);
$objPHPExcel-getActiveSheet()-getColumnDimension('L')-setWidth(20);
$objPHPExcel-getActiveSheet()-getColumnDimension('M')-setWidth(25);
$objPHPExcel-getActiveSheet()-getColumnDimension('L')-setWidth(22);
$objPHPExcel-getActiveSheet()-getColumnDimension('M')-setWidth(12);
$objPHPExcel-getActiveSheet()-getColumnDimension('N')-setWidth(15);
$objPHPExcel-getActiveSheet(0)-mergeCells('A1:' . $cellName[$cellNum - 1] . '1');
//合并单元格
$objPHPExcel-getActiveSheet()-setCellValue('A1',
'网站商家信息表')-getStyle()-getAlignment()-setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
for ($i = 0; $i $cellNum; $i++) {
$objPHPExcel-setActiveSheetIndex(0)-setCellValue($cellName[$i].'2', $expCellName[$i][1]);
}
// Miscellaneous glyphs, UTF-8
for ($i = 0; $i $dataNum; $i++) {
for ($j = 0; $j $cellNum; $j++) {
$objPHPExcel-getActiveSheet(0)-setCellValue($cellName[$j] .
($i + 3), " ".$expTableData[$i][$expCellName[$j][0]]);
}
}
ob_end_clean(); //清除缓冲区,避免乱码
header('pragma:public');
header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle. '.xls"');
header("Content-Disposition:attachment;filename=$fileName.xls");
//attachment新窗口打印inline本窗口打印
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter-save('php://output');
exit;
}
5
五、导出所需数据代码
function output($begin=0, $end=0) {//导出商家信息Excel
$xlsName = "User";
$xlsCell = array(
array('userid', '商家id'),
array('groupid', '商家等级'),
array('store_name', '店铺名称'),
array('contact_name', '联系人'),
array('phone', '手机'),
array('email', '邮箱'),
array('activity_count', '活动商品'),
array('frozen_deposit', '冻结中保证金'),
array('regdate', '注册时间'),
array('loginnum', '登录次数'),
array('lastdate', '最近登录'),
array('id_number', '身份证号码'),
array('name', '姓名'),
array('qq', 'QQ'), );
$beginToday = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
$endToday = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
if ($begin 0) { $beginToday = $begin;}
if ($end 0) {$endToday = $end;}
$xlsModel = M('member');
$xlsData=$xlsModel-query("SELECT
m.userid,m.groupid,c.store_name,c.contact_name,m.phone,m.email,c.frozen_deposit,FROM_UNIXTIME(m.regdate)
AS regdate,loginnum,FROM_UNIXTIME(m.lastdate) AS lastdate,m.qq FROM
xw_member AS m ,xw_member_merchant AS c WHERE m.modelid = 2 AND m.userid
= c.userid and (m.lastdate between $beginToday and $endToday) ORDER BY
m.userid DESC");
$dataNum = count($xlsData);
for ($i = 0; $i $dataNum; $i++){
if ($xlsData[$i][groupid] == '1') {
$xlsData[$i][groupid] = '普通商家';
} else if ($xlsData[$i][groupid] == '2') {
$xlsData[$i][groupid] = '金牌商家';
} else if ($xlsData[$i][groupid] == '3') {
$xlsData[$i][groupid] = '白金商家';
} else if ($xlsData[$i][groupid] == '4') {
$xlsData[$i][groupid] = '钻石商家';
}
$rs=M('member_attesta')-where("userid= '".$xlsData[$i]['userid']."'
AND type = 'identity'")-getField('infos');
$identity = string2array($rs);
$xlsData[$i]['id_number'] = $identity['id_number'];
$xlsData[$i]['name'] = $identity['name'];
$xlsData[$i]['activity_count']=
M('product')-where(array('company_id'=$xlsData[$i]['userid']))-count();
}
$this-exportExcel($xlsName, $xlsCell, $xlsData);
}
}
6
六、导出数据表格如下
如果没有图片要导出的话,可以使用以下简单方式。
?php
header("Content-type:application/vnd.ms-word");
header("Content-Disposition:filename=zgfun.doc");
echo
"要导出的内容一。\r\n";
echo
"要导出的第二行。";
?
=============
如果要导出带图片的,则需要使用把网页保存为mht的格式下载。word可以直接打开。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流