本文整理汇总了PHP中PHPExcel_Reader_Excel2007::listWorksheetNames方法的典型用法代码示例。如果您正苦于以下问题:PHP PHPExcel_Reader_Excel2007::listWorksheetNames方法的具体用法?PHP PHPExcel_Reader_Excel2007::listWorksheetNames怎么用?PHP PHPExcel_Reader_Excel2007::listWorksheetNames使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PHPExcel_Reader_Excel2007
的用法示例。
在下文中一共展示了PHPExcel_Reader_Excel2007::listWorksheetNames方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: uploadAction
public function uploadAction()
{
// $data = $this->getRequest()->getPost();
$file = $_FILES['xls'];
if ($file['error'] == 4) {
Mage::getSingleton('core/session')->addError(Mage::helper('maxfurniture')->__('Please choose file to upload.'));
$this->_redirect('*/*/');
return;
} elseif ($file['error']) {
Mage::getSingleton('core/session')->addError(Mage::helper('maxfurniture')->__('Upload error. Please try again.'));
$this->_redirect('*/*/');
return;
} elseif (!preg_match('/\\.xlsx{0,1}$/', $file['name'])) {
Mage::getSingleton('core/session')->addError(Mage::helper('maxfurniture')->__('You can only use XLS or XLSX files.'));
$this->_redirect('*/*/');
return;
}
$name = $file['name'];
$ext = preg_match('/xlsx$/', $name) ? 'xlsx' : 'xls';
$xls_file_name = Mage::getBaseDir() . "/custom/add_products/xls/import.{$ext}";
@unlink($xls_file_name);
move_uploaded_file($file['tmp_name'], $xls_file_name);
try {
require_once Mage::getBaseDir() . "/custom/include/PHPExcel/PHPExcel.php";
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
$cacheSettings = array('cacheTime' => 6000);
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
if ($ext == 'xls') {
$objReader = new PHPExcel_Reader_Excel5();
} else {
$objReader = new PHPExcel_Reader_Excel2007();
}
$objReader->setReadDataOnly(true);
$names = $objReader->listWorksheetNames($xls_file_name);
// ww($CUSTOM);
$DIR = Mage::getBaseDir() . "/custom/add_products/";
$files = array();
foreach ($names as $key => $name) {
$name = strtolower($name);
$name = preg_replace('/\\d$/', '', $name);
$files[$name] = $name;
}
foreach ($files as $file) {
@unlink($DIR . 'csv/' . $file . '.csv');
}
$i = 0;
foreach ($names as $key => $name) {
$file_name = strtolower($name);
$file_name = preg_replace('/\\d$/', '', $file_name);
$i++;
$file_path = $DIR . 'csv/' . $file_name . '.csv';
$skip_first_row = false;
if (file_exists($file_path)) {
// wlog("Appending: $file_name.csv", false);
$skip_first_row = true;
}
/* else wlog("Creating: $file_name.csv", false);*/
$fp = fopen($file_path, 'a');
$objReader->setLoadSheetsOnly(array($name));
$objPHPExcel = $objReader->load($xls_file_name);
$objWorksheet = $objPHPExcel->getActiveSheet();
$j = 0;
foreach ($objWorksheet->getRowIterator() as $row) {
$j++;
if ($j == 1 && $skip_first_row) {
continue;
}
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
$fields = array();
foreach ($cellIterator as $cell) {
$value = $cell->getValue();
$fields[] = $value;
}
fputcsv($fp, $fields);
}
fclose($fp);
}
} catch (Exception $e) {
Mage::getSingleton('core/session')->addError(Mage::helper('maxfurniture')->__('Error processing file.<br/>%s', $e->getMessage()));
$this->_redirect('*/*/');
return;
}
Mage::getSingleton('core/session')->addSuccess(Mage::helper('maxfurniture')->__('File uploaded and conferted to CSV files successfully.'));
$this->_redirect('*/*/');
}