/* 导入excel */
public function import_exl($file_name){
import("Org.Util.PHPExcel");
import("Org.Util.PHPExcel.IOFactory");
$ext=end(explode('.',$file_name));
if ($ext == 'xls') {
//如果excel文件后缀名为.xls,导入这个类
$objReader=\PHPExcel_IOFactory::createReader('Excel5');
} else {
//如果excel文件后缀名为.xlsx,导入这个类
$objReader=\PHPExcel_IOFactory::createReader('Excel2007');
}
$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
$sheet = $objPHPExcel->getSheet();
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数
$arr=['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'];
$arr_new=array_flip($arr);
$num=$arr_new[$highestColumn];
if($num != ''){
}else{
$num=0;
}
for($i=1;$i<=$highestRow;$i++){
for($j=1;$j<=$num+1;$j++){
$data[$i][$arr[$j-1]] = $objPHPExcel->getActiveSheet()->getCell($arr[$j-1].$i)->getValue();
}
}
return $data;
}
//简单导出excel
public function export_exl($table_arr,$file_name="",$ext="xls"){
!$file_name && $file_name=date('Ymd');
ob_end_clean();
//输出的文件类型为excel
header("Content-type:application/vnd.ms-excel");
//提示下载
header("Content-Disposition:attachement;filename=".$file_name.".".$ext);
//报表数据
/* $ReportArr =[
['标题1','标题1','标题1','标题1','标题1'],
['栏目1','栏目1','栏目1','栏目1','栏目1'],
['栏目2','栏目2','栏目2','栏目2','栏目2']
]; */
$ReportArr = $table_arr;
$ReportContent = '';
$num1 = count($ReportArr);
for($i=0;$i<$num1;$i++){
$num2 = count($ReportArr[$i]);
for($j=0;$j<$num2;$j++){
//ecxel都是一格一格的,用\t将每一行的数据连接起来
$ReportContent .= '"'.$ReportArr[$i][$j].'"'."\t";
}
//最后连接\n 表示换行
$ReportContent .= "\n";
}
//用的utf-8 最后转换一个编码为gb
$ReportContent = mb_convert_encoding($ReportContent,"gb2312","utf-8");
//输出即提示下载
echo $ReportContent;
}
//导出excel
public function export_exl_new($table_arr,$bt_arr,$title='',$file_name="",$ext="xls"){
!$file_name && $file_name=date('Ymd');
ob_end_clean();
//输出的文件类型为excel
//header("Content-type:application/vnd.ms-excel");
header("Content-type:application/doc");
//提示下载
//header("Content-Disposition:attachement;filename=".$file_name.".".$ext);
header("Content-Disposition:attachement;filename=".$file_name.".doc");
//报表数据
$html .= '<html xmlns:o="urn:schemas-microsoft-com:office:office"xmlns:x="urn:schemas-microsoft-com:office:excel"xmlns="http://www.w3.org/TR/REC-html40">';
$html .= '<head>';
$html .= '<meta http-equiv=Content-Type content="text/html; charset=utf-8">';
$html .= '<meta name=ProgId content=Excel.Sheet>';
$html .= '<meta name=Generator content="Microsoft Excel 11">';
$html .= '</head>';
$html .= '<body>';
$html .= '<table border=1 cellpadding=0 cellspacing=0 style="line-height:32px;100%;">';
$lie=count($table_arr[0]);
if(trim($title)){
$html .= '<tr style="background-color:#f2f2f2;">';
$html .= '<td colspan="'.$lie.'" align="center">';
$html .= '<h2>'.$title.'</h2>';
$html .= '</td>';
$html .= '</tr>';
}
$html .= '<tr style="font-size:14px;">';
foreach($bt_arr as $kk=>$vv){
$html .= '<td align="center" style="background-color:#f2f2f2;padding:7px;">'.$vv.'</td>';
}
$html .= '</tr>';
foreach($table_arr as $kk=>$vv){
$html .= '<tr>';
for($i=0;$i<$lie;$i++){
$html .= '<td align="center" style="font-size:12px;padding:7px;">'.$vv[$i].'</td>';
}
$html .= '</tr>';
}
$html .= '</table>';
$html .= '</body>';
$html .= '</html>';
echo $html;
}
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交