在使用过程中,如果出现值为FALSE时,请使用iconv()函数进行转码;
先贴出实用代码
import::appCls('phpexcel/phpexcel');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator($cname)
->setLastModifiedBy($cname)
->setTitle($tname)
->setSubject($tname)
->setDescription($tname)
->setKeywords($tname)
->setCategory($tname);
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setTitle(iconv('gbk', 'utf-8', $tname));
$objPHPExcel->getDefaultStyle()->getFont()->setName(iconv('gbk', 'utf-8', '宋体'));
$objPHPExcel->getDefaultStyle()->getFont()->setSize(10);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type: application/vnd.ms-excel;");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");
header("Content-Disposition:attachment;filename=" . $tname . '.xlsm');
header("Content-Transfer-Encoding:binary");
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', iconv('gbk', 'utf-8', '编号'))
->setCellValue('B1', iconv('gbk', 'utf-8', '订单名称'))
->setCellValue('C1', iconv('gbk', 'utf-8', '交易号'))
->setCellValue('D1', iconv('gbk', 'utf-8', '订单号'))
->setCellValue('E1', iconv('gbk', 'utf-8', '所属医生'))
->setCellValue('F1', iconv('gbk', 'utf-8', '药房'))
->setCellValue('G1', iconv('gbk', 'utf-8', '患者'))
->setCellValue('H1', iconv('gbk', 'utf-8', '代表'))
->setCellValue('I1', iconv('gbk', 'utf-8', '创建时间'))
->setCellValue('J1', iconv('gbk', 'utf-8', '回调时间'))
->setCellValue('K1', iconv('gbk', 'utf-8', '状态'))
->setCellValue('L1', iconv('gbk', 'utf-8', '支付方式'))
->setCellValue('M1', iconv('gbk', 'utf-8', '总价'));
$i = 2;
$data = $this->M('pay_list')->getsall();
import::appCls('order/orderpay');
foreach ($data as $k => $v) {
$pay = new orderpay($v->id);
switch ($v->status) {
case '0':
$status = '未支付';
break;
case '1':
$status = '已支付,待取药';
break;
case '2':
$status = '已取药';
break;
case '4':
$status = '支付失败';
break;
default:
$status = '未支付';
break;
}
$v->payapi = 'wxpay' ? '微信支付' : '现金支付';
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue("A$i", $v->id)
->setCellValue("B$i", $v->title)
->setCellValue("C$i", $v->trade_no)
->setCellValue("D$i", $v->ordersn)
->setCellValue("E$i", $pay->P('doctor'))
->setCellValue("F$i", $pay->P('shop'))
->setCellValue("G$i", $pay->P('openid'))
->setCellValue("H$i", $pay->P('sale'))
->setCellValue("I$i", date("Y-m-d H:i:s", $v->create_time))
->setCellValue("J$i", date("Y-m-d H:i:s", $v->back_time))
->setCellValue("K$i", iconv('gbk', 'utf-8', $status))
->setCellValue("L$i", iconv('gbk', 'utf-8', $v->payapi))
->setCellValue("M$i", $v->amount);
$i++;
}
$objWriter->save("php://output");
生成PHPExcel对象
$objPHPExcel = new PHPExcel();
设置excel文档的属性
$objPHPExcel->getProperties()->setCreator("Sam.c") ->setLastModifiedBy("Sam.c Test") ->setTitle("Microsoft Office Excel Document") ->setSubject("Test") ->setDescription("Test") ->setKeywords("Test") ->setCategory("Test result file");
操作第一个工作表
$objPHPExcel->setActiveSheetIndex(0);
设置工作薄名称
$objPHPExcel->getActiveSheet()->setTitle(iconv('gbk', 'utf-8', 'phpexcel测试'));
设置字体和大小
$objPHPExcel->getDefaultStyle()->getFont()->setName(iconv('gbk', 'utf-8', '宋体')); $objPHPExcel->getDefaultStyle()->getFont()->setSize(10);
输出excel格式
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type: application/vnd.ms-excel;");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");
header("Content-Disposition:attachment;filename=" . $tname . '.xlsm');
header("Content-Transfer-Encoding:binary");
$objWriter->save("[php://output](php://output)");
输出PDF
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->setSheetIndex(0);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type: application/pdf");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");
header("Content-Disposition:attachment;filename=".$m_strOutputPdfFileName);
header("Content-Transfer-Encoding:binary");
$objWriter->save("php://output");
设置一列的宽度
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
设置一行的高度
$objPHPExcel->getActiveSheet()->getRowDimension('6')->setRowHeight(30);
合并单元格
$objPHPExcel->getActiveSheet()->mergeCells('A1:P1');
设置单元格加粗
$styleArray1 = array(
'font' => array(
'bold' => true,
'size'=>12,
'color'=>array(
'argb' => '00000000',
),
),
'alignment' => array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
),
);
// 将A1单元格设置为加粗,居中
$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray1);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
给选定单元格写入内容
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Hello Baby');
设置单元格样式(水平/垂直居中):
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
设置单元格样式(黑色字体):
$objPHPExcel->getActiveSheet()->getStyle('H5')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLACK); // 黑色
设置单元格格式(背景):
$objPHPExcel->getActiveSheet()->getStyle('H5')->getFill()->getStartColor()->setARGB('00ff99cc'); // 将背景设置为浅粉色
设置单元格格式(数字格式):
$objPHPExcel->getActiveSheet()->getStyle('F'.$iLineNumber)->getNumberFormat()->setFormatCode('0.000');
给单元格中放入图片:
// 将数据中心图片放在J1单元格内 $objDrawing = new PHPExcel_Worksheet_Drawing(); $objDrawing->setName('Logo'); $objDrawing->setDescription('Logo'); $objDrawing->setPath('test.jpg'); $objDrawing->setWidth(400); $objDrawing->setHeight(123); $objDrawing->setCoordinates('J1'); $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
在单元格中设置超链接:
$objPHPExcel->getActiveSheet()->setCellValue('H8', iconv('gbk', 'utf-8', '燕南天')); $objPHPExcel->getActiveSheet()->getCell('H8')->getHyperlink()->setUrl('http://www.jb51.net/');
设置单元格边框
$styleThinBlackBorderOutline = array(
'borders' => array (
'outline' => array (
'style' => PHPExcel_Style_Border::BORDER_THIN, //设置border样式
//'style' => PHPExcel_Style_Border::BORDER_THICK, 另一种样式
'color' => array ('argb' => 'FF000000'), //设置border颜色
),
),
);
$objPHPExcel->getActiveSheet()->getStyle( 'A4:E10')->applyFromArray($styleThinBlackBorderOutline);
//添加一个新的worksheet
$objExcel->createSheet();
$objActSheet = $objExcel->getSheet($s);
$objActSheet->setTitle('表'.$GSheet);
导出案例
<?php
error_reporting(E_ALL);
date_default_timezone_set('Asia/Shanghai');
require_once './Classes/PHPExcel.php';
$data=array(
0=>array(
'id'=>1001,
'username'=>'张飞',
'password'=>'123456',
'address'=>'三国时高老庄250巷101室'
),
1=>array(
'id'=>1002,
'username'=>'关羽',
'password'=>'123456',
'address'=>'三国时花果山'
),
2=>array(
'id'=>1003,
'username'=>'曹操',
'password'=>'123456',
'address'=>'延安西路2055弄3号'
),
3=>array(
'id'=>1004,
'username'=>'刘备',
'password'=>'654321',
'address'=>'愚园路188号3309室'
)
);
$objPHPExcel=new PHPExcel();
$objPHPExcel->getProperties()->setCreator('http://www.jb51.net')
->setLastModifiedBy('http://www.jb51.net')
->setTitle('Office 2007 XLSX Document')
->setSubject('Office 2007 XLSX Document')
->setDescription('Document for Office 2007 XLSX, generated using PHP classes.')
->setKeywords('office 2007 openxml php')
->setCategory('Result file');
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1','ID')
->setCellValue('B1','用户名')
->setCellValue('C1','密码')
->setCellValue('D1','地址');
$i=2;
foreach($data as $k=>$v){
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$i,$v['id'])
->setCellValue('B'.$i,$v['username'])
->setCellValue('C'.$i,$v['password'])
->setCellValue('D'.$i,$v['address']);
$i++;
}
$objPHPExcel->getActiveSheet()->setTitle('三年级2班');
$objPHPExcel->setActiveSheetIndex(0);
$filename=urlencode('学生信息统计表').'_'.date('Y-m-dHis');
/*
*生成xlsx文件
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
*/
/*
*生成xls文件
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
*/
$objWriter->save('php://output');
exit;
导入案例
<?php
require_once 'Classes/PHPExcel.php';
require_once 'Classes/PHPExcel/IOFactory.php';
require_once 'Classes/PHPExcel/Reader/Excel5.php';
$objReader=PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format
$objPHPExcel=$objReader->load($file_url);//$file_url即Excel文件的路径
$sheet=$objPHPExcel->getSheet(0);//获取第一个工作表
$highestRow=$sheet->getHighestRow();//取得总行数
$highestColumn=$sheet->getHighestColumn(); //取得总列数
//循环读取excel文件,读取一条,插入一条
for($j=2;$j<=$highestRow;$j++){//从第一行开始读取数据
$str='';
for($k='A';$k<=$highestColumn;$k++){ //从A列读取数据
//这种方法简单,但有不妥,以'\\'合并为数组,再分割\\为字段值插入到数据库,实测在excel中,如果某单元格的值包含了\\导入的数据会为空
$str.=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue().'\\';//读取单元格
}
//explode:函数把字符串分割为数组。
$strs=explode("\\",$str);
$sql="INSERT INTO `".TB_PREFIX."business`(`username`,`password`,`company`,`prov`,`address`,`btime`,`phone`,`email`,`name`) VALUES (
'{$strs[0]}',
'{$strs[1]}',
'{$strs[2]}',
'{$strs[3]}',
'{$strs[4]}',
'{$strs[5]}',
'{$strs[6]}',
'{$strs[7]}',
'{$strs[8]}')";
$db->query($sql);//这里执行的是插入数据库操作
}
unlink($file_url); //删除excel文件
?>
网友评论