我是所有专家我是php的新手。 任何人都可以告诉我加快PHPExcel这是我的代码,它读取20000行4列。它需要超过15秒。非常感谢你
function upload_fl($FILES){
$file = $FILES['excel'];
//echo getcwd();
//print_r($file);
require 'phpexcel/PHPExcel.php';
if(move_uploaded_file($file['tmp_name'],'C:/wamp/www/datatable_017/php/upload/'.$file['name'])){
$data = '../php/upload/'.$file['name'];
$objPHPExcel = PHPExcel_IOFactory::load($data);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
//var_dump($sheetData);
echo sizeof($sheetData);
//$writefile = fopen($FILES['excel']['name'].'.txt','w');
/*
foreach($sheetData as $row){
foreach($row as $col->$value){
//$value_inser = (is_numeric($value) == true ? ''number',''.$value.'',''':''text','',''.$value.''');
//fwrite($writefile,$cate_set_id[$col].','.$com_id.','.$year.','.$value_insert.'\n');
}
}
-->
fclose($writefile);
*/
return 'upload/'.$file['name'].'.txt';
}else{
return 'upload failed';
}
}//function
答案 0 :(得分:1)
如果您有多个工作表,但不需要加载所有工作表,则可以使用setLoadSheetsOnly()方法限制Reader将加载的工作表。要加载单个命名工作表:
$inputFileType = 'Excel5';
$inputFileName = './sampleData/example1.xls';
$sheetname = 'Data Sheet #2';
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
/** Advise the Reader of which WorkSheets we want to load **/
$objReader->setLoadSheetsOnly($sheetname);
/** Load $inputFileName to a PHPExcel Object **/
$objPHPExcel = $objReader->load($inputFileName);
或者您可以通过传递一组名称来指定几个工作表,其中一次调用setLoadSheetsOnly():
$inputFileType = 'Excel5';
$inputFileName = './sampleData/example1.xls';
$sheetnames = array('Data Sheet #1','Data Sheet #3');
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
/** Advise the Reader of which WorkSheets we want to load **/
$objReader->setLoadSheetsOnly($sheetnames);
/** Load $inputFileName to a PHPExcel Object **/
$objPHPExcel = $objReader->load($inputFileName);
如果您只需要访问工作表的一部分,那么您可以定义一个读取过滤器来识别您实际想要加载的单元格:
$inputFileType = 'Excel5';
$inputFileName = './sampleData/example1.xls';
$sheetname = 'Data Sheet #3';
/** Define a Read Filter class implementing PHPExcel_Reader_IReadFilter */
class MyReadFilter implements PHPExcel_Reader_IReadFilter {
public function readCell($column, $row, $worksheetName = '') {
// Read rows 1 to 7 and columns A to E only
if ($row >= 1 && $row <= 7) {
if (in_array($column,range('A','E'))) {
return true;
}
}
return false;
}
}
/** Create an Instance of our Read Filter **/
$filterSubset = new MyReadFilter();
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
/** Advise the Reader of which WorkSheets we want to load
It's more efficient to limit sheet loading in this manner rather than coding it into a Read Filter **/
$objReader->setLoadSheetsOnly($sheetname);
echo 'Loading Sheet using filter';
/** Tell the Reader that we want to use the Read Filter that we've Instantiated **/
$objReader->setReadFilter($filterSubset);
/** Load only the rows and columns that match our filter from $inputFileName to a PHPExcel Object **/
$objPHPExcel = $objReader->load($inputFileName);
如果您不需要加载格式化信息,只需加载工作表数据,那么setReadDataOnly()方法将告诉读者仅加载单元格值,忽略任何单元格格式:
$inputFileType = 'Excel5';
$inputFileName = './sampleData/example1.xls';
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
/** Advise the Reader that we only want to load cell data, not formatting **/
$objReader->setReadDataOnly(true);
/** Load $inputFileName to a PHPExcel Object **/
$objPHPExcel = $objReader->load($inputFileName);
虽然请注意,只读取这样的原始数据不允许您区分日期值和浮点数
如果您想使用大型Excel文件,请不要通过进行toArray()
调用在内存中构建大型PHP数组,因为这是内存使用量的一大开销,并且性能也会很高在构建数组时不断需要分配越来越多的内存......如果你要一次处理一行,使用PHPExcel中内置的迭代器,或者只使用一个循环来访问每一行反过来。