【此分享已过期,请使用phpspreadsheet导入导出Excel】
(1)使用PHPExcel整合ThinkPHP5.0导入导出Excel文件。
(2)参考文档整合ThinkPHP5.0使用TCPdf导出PDF文件。
参考文档:http://www.thinkphp.cn/code/1491.html
代码详情请下载文件,希望对大家有帮助。
(1)Excel输出关键代码:
$Excel['fileName']="PHPExcel示例".date('Y年m月d日-His',time());//or $xlsTitle
$Excel['cellName']=['A','B','C','D'];//定义列数
$Excel['H'] = ['A'=>22,'B'=>26,'C'=>30,'D'=>30];//横向水平宽度
$Excel['V'] = ['1'=>40,'2'=>26];//纵向垂直高度
$Excel['sheetTitle']="PHPExcel示例";//大标题,自定义
$Excel['xlsCell']=Data::head();//获取表头信息
$data=Data::data();//获取数据
\app\index\model\PHPExcel::excelPut($Excel,$data);//生成Excel(2)Excel读取关键部分vendor("PHPExcel.PHPExcel");
$PHPReader = new \PHPExcel_Reader_Excel2007();
if( ! $PHPReader->canRead($filePath))
{
//使用2007读取不了,就用2003读取。
$PHPReader = new \PHPExcel_Reader_Excel5();
if( ! $PHPReader->canRead($filePath)){
echo 'not Excel';
return ;
}
}
$PHPExcel = $PHPReader->load($filePath); //读取文件
$currentSheet = $PHPExcel->getSheet(0); //读取第一个工作簿
$allColumn = $currentSheet->getHighestColumn(); // 所有列数,得出的是字母:F
$allColumn_num = \PHPExcel_Cell::columnIndexFromString($allColumn);//F转换为6
$allRow = $currentSheet->getHighestRow(); // 所有行数
$data = [];
//遍历行,遍历列,逐个读取数据:默认有标题,所以从第二行读起
for ($rowIndex = 2; $rowIndex <= $allRow; $rowIndex++)
{
$sub=[];
for ($colIndex=0; $colIndex < $allColumn_num; $colIndex++) {
//stringFromColumnIndex($colIndex); 将字母转换为数字
$sub[]=$currentSheet->getCell(\PHPExcel_Cell::stringFromColumnIndex($colIndex).$rowIndex)->getValue();
}
$data[]=$sub;
}
$allColumn=$allColumn_num;
return ['data'=>$data,'col'=>$allColumn,'row'=>$allRow];(3)PDF生成关键部分public static function pdf($header=[],$data=[],$fileName='PdfFile'){
set_time_limit(120);
if(empty($header) || empty($data)) $this->error("导出的数据为空!");
vendor("tcpdf.tcpdf");
$pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);//新建pdf文件
//设置文件信息
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor("Author");
$pdf->SetTitle("PHP输出示例-2018");
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
//设置页眉页脚
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, 'http://www.aliyun.com','Copyright © 2015-2018 by linzening, Ltd. All Rights reserved',array(66,66,66), array(0,0,0));
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);//设置默认等宽字体
$pdf->SetMargins(PDF_MARGIN_LEFT, 24, PDF_MARGIN_RIGHT);//设置页面边幅
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);//设置纸张头部
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);//设置纸张底部
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);//设置自动分页符
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->setLanguageArray(['a_meta_charset'=>'UTF-8','a_meta_dir'=>'ltr','a_meta_language'=>'en','w_page'=>date("Y-m-d").' page']);
$pdf->SetFont('droidsansfallback', '');
$pdf->AddPage();
$pdf->SetFillColor(245, 245, 245);
$pdf->SetTextColor(0);
$pdf->SetDrawColor(66, 66, 66);
$pdf->SetLineWidth(0.3);
$pdf->SetFont('droidsansfallback', '',9);
// Header
$num_headers = count($header);
for($i = 0; $i < $num_headers; ++$i) {
$pdf->Cell(180/$num_headers, 8, $header[$i], 1, 0, 'C', 1);
}
$pdf->Ln();
// 填充数据
$fill = 0;
foreach($data as $list) {
//每頁重复表格标题行
if(($pdf->getPageHeight()-$pdf->getY())<($pdf->getBreakMargin()+2)){
$pdf->SetFillColor(245, 245, 245);
$pdf->SetTextColor(0);
$pdf->SetDrawColor(66, 66, 66);
$pdf->SetLineWidth(0.3);
$pdf->SetFont('droidsansfallback', '',9);
// Header
for($i = 0; $i < $num_headers; ++$i) {
$pdf->Cell(180/$num_headers, 8, $header[$i], 1, 0, 'C', 1);
}
$pdf->Ln();
}
// Color and font restoration
$pdf->SetFillColor(245, 245, 245);
$pdf->SetTextColor(40);
$pdf->SetLineWidth(0.1);
$pdf->SetFont('droidsansfallback', '');
$i=0;
foreach($list as $key=>$row){
// $pdf->Cell($head_width[$i], 6, $row, 'LR', 0, 'C', $fill);
$pdf->MultiCell(180/$num_headers, 6, $row, $border=1, $align='C',$fill, $ln=0, $x='', $y='', $reseth=true, $stretch=0,$ishtml=false, $autopadding=true, $maxh=0, $valign='C', $fitcell=true);
$i++;
}
$pdf->Ln();
$fill=!$fill;
}
$showType= 'I';//PDF输出的方式。I,在浏览器中打开;D,以文件形式下载;F,保存到服务器中;S,以字符串形式输出;E:以邮件的附件输出。
$pdf->Output("{$fileName}-".time().".pdf", $showType);
exit;
}
excelopdf.zip
( 5.22 MB 下载:630 次 )
