Magento:通过php excel阅读器,阅读excel文件

我是新的magento和使用1.7.2。 你能不能让我知道如何在magento中集成PHP excel Reader,以便系统可以读取excel文件?

请帮帮我。 提前致谢。

Magento通常具有以.CSV格式读取文件的function。 如果你需要集成Excel阅读器,那么你可以通过创build自己的自定义模块来完成。但是.CSV也是一个很好的select。

如果你想读取magento中的csv文件,你可以这样做。

if(isset($_FILES['file_uplod']['name']) && ($_FILES['file_uplod']['name'] != '')) { $filename = $_FILES['file_uplod']['name']; $ext = pathinfo($filename, PATHINFO_EXTENSION); $uploader = new Varien_File_Uploader('file_uplod'); $uploader->setAllowedExtensions(array('text/csv', 'csv','excel','vnd.ms-excel', 'application/csv', 'text/comma-separated-values', 'application/excel', 'application/vnd.ms-excel', 'application/vnd.msexcel', 'text/anytext', 'application/octet-stream', 'application/txt')); $uploader->setAllowCreateFolders(true); //for creating the directory if not exists $uploader->setAllowRenameFiles(false); //if true, uploaded file's name will be changed, if file with the same name already exists directory. $uploader->setFilesDispersion(false); $path = Mage::getBaseDir('media') . DS ; $path = $path ."csv-folder"; $uploader->save($path, $_FILES['file_uplod']['name']); $file = $path."/".$_FILES['file_uplod']['name']; $uploader->save($path, $_FILES['file_uplod']['name']); $file = $path."/".$_FILES['file_uplod']['name']; $csv = new Varien_File_Csv(); $data = $csv->getData($file); //first line are the headers $headers = $data[0]; $allProductData = array(); for($i=1; $i<count($data); $i++){ $productData = array(); foreach ($headers as $index=>$attributeCode) { $productData[$attributeCode] = $data[$i][$index]; } $allProductData[] = $productData; } 

在这里打印$allProductData数组来获取csv.Hope里面的所有数据,这对任何人都有帮助。

如果你想读取magento中的excel文件。 你需要开源项目“ PHP-ExcelReader类 ”

示例代码如下

 $path = Mage::getBaseDir('media') . DS; $path_to_XML = $path . "csv-folder" . DS . "magento.xls"; $include_path = dirname(__FILE__); $path_to_PHP_ExcelReader = $include_path . "/Excel/reader.php"; require_once $path_to_PHP_ExcelReader; // ExcelFile($filename, $encoding); $data = new Spreadsheet_Excel_Reader(); // Set output Encoding. $data->setOutputEncoding('utf-8'); /* if you want you can change 'iconv' to mb_convert_encoding: */ $data->setUTFEncoder('mb'); /* * By default rows & cols indeces start with 1 * For change initial index use: */ $index = 0; $data->setRowColOffset($index); /* setDefaultFormat - set format for columns with unknown formatting */ $data->setDefaultFormat('%.2f'); /* setColumnFormat - set format for column (apply only to number fields) */ $data->setColumnFormat(4, '%.3f'); /* Do the actual reading of file */ $data->read($path_to_XML); echo "<pre>"; print_r($data); exit; 

所有你需要做的是提取这个zip到同一个目录,包含你的类需要XLS支持