To Download And Save Data From A Excel Sheet In Yii

Hii, People I am very new to yii and currently working on a app where I want to Import data through a excel file(.xlsx) containing information regarding user details. I already created the upload button for the same purpose and wants to import data from file( Excel)to save data & populate database. I Really need help and directions to get overcome through this problem. already tried few extension(PHP Excel, Php Excel reader and Yii excel). but didnt seems helpul for me. All responses are welcome and thanks in advance.

As you’ve said, you need to use something like PHPExcel to read the file. Get that working first, then the rest should be relatively simple.

To give you an idea, here’s an example of a method that I use in a class to read a spreadsheet:




	public function readFile()

	{

		require_once(Yii::getPathOfAlias('ext.excel.Classes') . '/PHPExcel.php');

		

		try

		{

			$wb = PHPExcel_IOFactory::load($this->filePath);

			$ws = $wb->getSheet(0);

			

			$lastRowIndex = $ws->getHighestRow();

			$lastColIndex = count(self::$expectedHeaders);

			

			// Headers

			for ($c = 0; $c <= $lastColIndex; $c++)

			{

				$cell = $ws->getCellByColumnAndRow($c, 1);

				$this->importHeaders[$c] = self::getCellText($cell);

			}

			

			// Data

			for ($r = 2; $r <= $lastRowIndex; $r++)

			{

				$rowData = array();

				for ($c = 0; $c <= $lastColIndex; $c++)

				{

					$cell = $ws->getCellByColumnAndRow($c, $r);

					$rowData[$c] = self::getCellText($cell);

				}

				$this->importData[] = $rowData;

			}

		}

		catch (Exception $ex)

		{

			$this->addError('file', 'Failed to read file.');

			throw $ex;

		}

	}



There are other methods that read the data in the arrays and validate it.

The only problem I had with PHPExcel was a conflict in the auto-loader. Provide details about what you’ve tried and the specific problems you encountered to get more help.