1.实现导出Excel文件,并在导出的文件中显示图片
//导出
public function push(){
$goods_list=M('Dajia')->select();
$data = array();
foreach ($goods_list as $k=>$goods_info){
$data[$k][id] = $goods_info['id'];
$data[$k][dingwei] = $goods_info['dingwei'];
$data[$k][mylocal] = $goods_info['mylocal'];
$data[$k][content] = $goods_info['content'];
$data[$k][name] = $goods_info['name'];
$data[$k][tel] = $goods_info['tel'];
}
foreach ($data as $field=>$v){
if($field == 'id'){
$headArr[]='ID';
}
if($field == 'dingwei'){
$headArr[]='提交位置';
}
if($field == 'mylocal'){
$headArr[]='污染位置';
}
if($field == 'content'){
$headArr[]='问题描述';
}
if($field == 'name'){
$headArr[]='举报者姓名';
}
if($field == 'tel'){
$headArr[]='联系电话';
}
}
$filename="jiandu";
$this->getExcel($filename,$headArr,$data);
}
private function getExcel($fileName,$headArr,$data){
//导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入
import("Org.PHPExcel");
import("Org.PHPExcel.Writer.Excel5");
import("Org.PHPExcel.IOFactory.php");
$date = date("Y_m_d",time());
$fileName .= "_{$date}.xls";
//创建PHPExcel对象,注意,不能少了\
$objPHPExcel = new \PHPExcel();
$objProps = $objPHPExcel->getProperties();
//设置表头
$key = ord("A");
//print_r($headArr);exit;
foreach($headArr as $v){
$colum = chr($key);
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.'1', $v);
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.'1', $v);
$key += 1;
}
$column = 2;
$objActSheet = $objPHPExcel->getActiveSheet();
//print_r($data);exit;
foreach($data as $key => $rows){ //行写入
$span = ord("A");
$i=0;
foreach($rows as $keyName=>$value){// 列写入
$i++;
$j = chr($span);
if($value&&($i==7||$i==8||$i==9||$i==10)){
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Photo');
$objDrawing->setDescription('Photo');
$objDrawing->setPath($value);
$objDrawing->setHeight(170);
$objDrawing->setWidth(120);
$objDrawing->setCoordinates($j.$column);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
}
else{
$objActSheet->setCellValue($j.$column, $value);
}
$span++;
}
$column++;
}
/* $objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Photo');
$objDrawing->setDescription('Photo');
$objDrawing->setPath('./uploads/1.jpg');
$objDrawing->setHeight(170);
$objDrawing->setWidth(120);
$objDrawing->setCoordinates('G2');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());*/
$fileName = iconv("utf-8", "gb2312", $fileName);
//重命名表
//$objPHPExcel->getActiveSheet()->setTitle('test');
//设置活动单指数到第一个表,所以Excel打开这是第一个表
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=\"$fileName\"");
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output'); //文件通过浏览器下载
exit;
}
2.获得中文字符拼音首字母
error_reporting(E_ALL^E_NOTICE);
function getFirstCharter($str){
if(empty($str)){return '';}
$fchar=ord($str{0});
if($fchar>=ord('A')&&$fchar<=ord('z')) return strtoupper($str{0});
$s1=iconv('UTF-8','gb2312',$str);
$s2=iconv('gb2312','UTF-8',$s1);
$s=$s2==$str?$s1:$str;
$asc=ord($s{0})*256+ord($s{1})-65536;
if($asc>=-20319&&$asc<=-20284) return 'A';
if($asc>=-20283&&$asc<=-19776) return 'B';
if($asc>=-19775&&$asc<=-19219) return 'C';
if($asc>=-19218&&$asc<=-18711) return 'D';
if($asc>=-18710&&$asc<=-18527) return 'E';
if($asc>=-18526&&$asc<=-18240) return 'F';
if($asc>=-18239&&$asc<=-17923) return 'G';
if($asc>=-17922&&$asc<=-17418) return 'H';
if($asc>=-17417&&$asc<=-16475) return 'J';
if($asc>=-16474&&$asc<=-16213) return 'K';
if($asc>=-16212&&$asc<=-15641) return 'L';
if($asc>=-15640&&$asc<=-15166) return 'M';
if($asc>=-15165&&$asc<=-14923) return 'N';
if($asc>=-14922&&$asc<=-14915) return 'O';
if($asc>=-14914&&$asc<=-14631) return 'P';
if($asc>=-14630&&$asc<=-14150) return 'Q';
if($asc>=-14149&&$asc<=-14091) return 'R';
if($asc>=-14090&&$asc<=-13319) return 'S';
if($asc>=-13318&&$asc<=-12839) return 'T';
if($asc>=-12838&&$asc<=-12557) return 'W';
if($asc>=-12556&&$asc<=-11848) return 'X';
if($asc>=-11847&&$asc<=-11056) return 'Y';
if($asc>=-11055&&$asc<=-10247) return 'Z';
return null;
}
echo getFirstCharter('黎明');
?>