ThinkPHP 3.2 + PHPExcel 导入导出文件 第三方类库不能用问题解决
ThinkPHP3.2 采用了namesapce,就是所谓的命名空间,会导致很多盆友放到/Vendor下边导入文件的时候出现各种问题,鄙人百度了好多,但是好多都是老版本,最新版的ThinkPHP跟PHPExcel一直没有解决,没有办法,只能看代码,终于,找到了这个问题的解决办法,在这里Mark下,有需要的盆友可以看看。
把PHPExcel加压之后的文件里边的Classes的文件夹 PHPExcel 跟 php文件 PHPExcel.php 放到你\ThinkPHP\Library\Vendor 目录下,然后在Controller中这样写:
导入代码实例:
public function importExcel(){
vendor('PHPExcel');
$PHPExcel = new \PHPExcel();
$saveFile = '/mnt/www/tp/Application/Home/Controller/test.xlsx';
$PHPReader = new \PHPExcel_Reader_Excel2007();
if(!$PHPReader->canRead($saveFile)){
$PHPReader = new \PHPExcel_Reader_Excel5();
if(!$PHPReader->canRead($saveFile)){
echo 'no Excel';
return ;
}
}
$PHPExcel = $PHPReader->load($saveFile);
$currentSheet = $PHPExcel->getSheet(0);
/**get max column*/
$allColumn = $currentSheet->getHighestColumn();
/**get max row*/
$allRow = $currentSheet->getHighestRow();
$return = array();
$i=0;
for($currentRow = 2;$currentRow
for($currentColumn= 'A';$currentColumn
$count = ord($currentColumn) - 65;
$val = $currentSheet->getCellByColumnAndRow($count,$currentRow)->getValue();
}
$i++;
}
}
导出实例代码:
public function exportExcel(){
vendor('PHPExcel');
$objExcel = new \PHPExcel();
//set document Property
$objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
$objActSheet = $objExcel->getActiveSheet();
$key = ord("A");
$objActSheet->setCellValue("A1", 'test1');
$objActSheet->setCellValue("A2", 'test2');
$objActSheet->setCellValue("B1", 'test3');
$objActSheet->setCellValue("B2", 'test4');
$outfile = "test.xls";
//export to exploer
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition:inline;filename="'.$outfile.'"');
header("Content-Transfer-Encoding: binary");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
$objWriter->save('php://output');
exit;
}
各位可以试下,反正我是测试过了,至于你们信不信,反正我是信啦。
PS:其实没啥,就是导入文件之后,所有的new 都加个\,你就成功了,原来生活如此简单哦也!
再PS:第三方类库(TP的命名方式是已.class.php结尾的,如果你的类库中不是已这种方式结尾),请放到Think/Library/Vender中,然后使用vender()方式调用,比如你的Vendor文件夹下有个PHPExcel.php的文件,你只需要vendor('PHPExcel');如果你的Vendor文件夹下有个PHPExcel的问价夹,里边test.php文件,你只需要vendor('PHPExcel.test');
一次类推即可。
本文仅供初学者讨论学习,大牛请绕道,不喜勿喷,共建和谐社会!

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
