在使用過程中,如果出現(xiàn)值為FALSE時(shí)血当,請使用iconv()函數(shù)進(jìn)行轉(zhuǎn)碼;
先貼出實(shí)用代碼
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', '所屬醫(yī)生'))
->setCellValue('F1', iconv('gbk', 'utf-8', '藥房'))
->setCellValue('G1', iconv('gbk', 'utf-8', '患者'))
->setCellValue('H1', iconv('gbk', 'utf-8', '代表'))
->setCellValue('I1', iconv('gbk', 'utf-8', '創(chuàng)建時(shí)間'))
->setCellValue('J1', iconv('gbk', 'utf-8', '回調(diào)時(shí)間'))
->setCellValue('K1', iconv('gbk', 'utf-8', '狀態(tài)'))
->setCellValue('L1', iconv('gbk', 'utf-8', '支付方式'))
->setCellValue('M1', iconv('gbk', 'utf-8', '總價(jià)'));
$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' ? '微信支付' : '現(xiàn)金支付';
$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();
設(shè)置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");
操作第一個(gè)工作表
$objPHPExcel->setActiveSheetIndex(0);
設(shè)置工作薄名稱
$objPHPExcel->getActiveSheet()->setTitle(iconv('gbk', 'utf-8', 'phpexcel測試'));
設(shè)置字體和大小
$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");
設(shè)置一列的寬度
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
設(shè)置一行的高度
$objPHPExcel->getActiveSheet()->getRowDimension('6')->setRowHeight(30);
合并單元格
$objPHPExcel->getActiveSheet()->mergeCells('A1:P1');
設(shè)置單元格加粗
$styleArray1 = array(
'font' => array(
'bold' => true,
'size'=>12,
'color'=>array(
'argb' => '00000000',
),
),
'alignment' => array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
),
);
// 將A1單元格設(shè)置為加粗,居中
$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray1);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
給選定單元格寫入內(nèi)容
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Hello Baby');
設(shè)置單元格樣式(水平/垂直居中):
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
設(shè)置單元格樣式(黑色字體):
$objPHPExcel->getActiveSheet()->getStyle('H5')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLACK); // 黑色
設(shè)置單元格格式(背景):
$objPHPExcel->getActiveSheet()->getStyle('H5')->getFill()->getStartColor()->setARGB('00ff99cc'); // 將背景設(shè)置為淺粉色
設(shè)置單元格格式(數(shù)字格式):
$objPHPExcel->getActiveSheet()->getStyle('F'.$iLineNumber)->getNumberFormat()->setFormatCode('0.000');
給單元格中放入圖片:
// 將數(shù)據(jù)中心圖片放在J1單元格內(nèi) $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());
在單元格中設(shè)置超鏈接:
$objPHPExcel->getActiveSheet()->setCellValue('H8', iconv('gbk', 'utf-8', '燕南天')); $objPHPExcel->getActiveSheet()->getCell('H8')->getHyperlink()->setUrl('http://www.jb51.net/');
設(shè)置單元格邊框
$styleThinBlackBorderOutline = array(
'borders' => array (
'outline' => array (
'style' => PHPExcel_Style_Border::BORDER_THIN, //設(shè)置border樣式
//'style' => PHPExcel_Style_Border::BORDER_THICK, 另一種樣式
'color' => array ('argb' => 'FF000000'), //設(shè)置border顏色
),
),
);
$objPHPExcel->getActiveSheet()->getStyle( 'A4:E10')->applyFromArray($styleThinBlackBorderOutline);
//添加一個(gè)新的worksheet
$objExcel->createSheet();
$objActSheet = $objExcel->getSheet($s);
$objActSheet->setTitle('表'.$GSheet);
導(dǎo)出案例
<?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'=>'三國時(shí)高老莊250巷101室'
),
1=>array(
'id'=>1002,
'username'=>'關(guān)羽',
'password'=>'123456',
'address'=>'三國時(shí)花果山'
),
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('學(xué)生信息統(tǒng)計(jì)表').'_'.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;
導(dǎo)入案例
<?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);//獲取第一個(gè)工作表
$highestRow=$sheet->getHighestRow();//取得總行數(shù)
$highestColumn=$sheet->getHighestColumn(); //取得總列數(shù)
//循環(huán)讀取excel文件,讀取一條,插入一條
for($j=2;$j<=$highestRow;$j++){//從第一行開始讀取數(shù)據(jù)
$str='';
for($k='A';$k<=$highestColumn;$k++){ //從A列讀取數(shù)據(jù)
//這種方法簡單,但有不妥撤奸,以'\\'合并為數(shù)組,再分割\\為字段值插入到數(shù)據(jù)庫,實(shí)測在excel中胧瓜,如果某單元格的值包含了\\導(dǎo)入的數(shù)據(jù)會為空
$str.=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue().'\\';//讀取單元格
}
//explode:函數(shù)把字符串分割為數(shù)組。
$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);//這里執(zhí)行的是插入數(shù)據(jù)庫操作
}
unlink($file_url); //刪除excel文件
?>