我是 PHPExcel 新手,第一次使用它。我已经编写了这段代码,但我知道有更好、更快的方法。目前,当我阅读示例电子表格(6000 行,与实际电子表格相同)时,在 localhost 上大约需要 2 分钟。
那么,除了让代码更快更精简之外,有没有办法展示实际的进度呢?
我已经阅读了文档,但仍然对此感到困惑。
这是我目前正在使用的代码:
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '../local_code/classes/');
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
/** Define a Read Filter class implementing PHPExcel_Reader_IReadFilter */
class MyReadFilter implements PHPExcel_Reader_IReadFilter
{
public function readCell($column, $row, $worksheetName = '') {
// read columns A, C, G, H from row 2
if ($row >= 2) {
if (in_array($column,array("A","C","G","H"))) {
return true;
}
}
return false;
}
}
$inputFileName = 'newsheet.xlsx'; // change to uploaded file
$sheetname = 'FULL COMBINED';
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setLoadSheetsOnly($sheetname);
$objReader->setReadFilter(new MyReadFilter());
$objPHPExcel = $objReader->load($inputFileName);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,false,false,true);
foreach($sheetData as $row => $columns) {
if($row != 1) {
$value = '';
foreach($columns as $cell => $val) {
if($cell == 'A') {
$val = trim($val);
}
if($cell == 'C') {
if(substr($val,0,1) != 'B') {
$val = trim($val);
$temp = substr($val,1);
if(is_numeric($temp))
$val = (int) $temp;
}
}
if($cell == 'G') {
$val = ($val * 86400) - 2209075200 - 86400;
}
if($cell == 'H') {
$val == floatval($val);
}
if($val != "")
$value[] = $val;
}
$values = "('" . implode("','",$value) . "')";
mysql_query("INSERT INTO projects_timesheet (employee_code,cost_code,date,hours) VALUES " . $values)or die(mysql_error());
}
}
问题 1
有没有更快的方法来做我现在正在做的事情?我知道分块,但似乎我做错了,因为它不起作用。
问题2
有没有办法可以向用户显示脚本当前所在的行?
任何帮助将不胜感激