public function testConstruct() { $excel = new SimpleExcel('CSV'); $excel2 = new SimpleExcel(); $excel2->constructParser('CSV'); $this->assertEquals($excel->parser, $excel2->parser); return $excel; }
/** * Функция конвертации * xml, tsv, html, json файлов в csv * * @param [string] $infile [входной файл] * @param [string] $outfile [выходной файл] * @return [void] */ public static function convertOtherFormats($infile, $pathToConvert) { //проверить существование входящего файла if (!file_exists($infile)) { throw new Exception("Входящий файл " . basename($infile) . " не существует"); } $ext = File::getFileExtension($infile); if (!in_array($ext, ['xml', 'tsv', 'html', 'json', 'xlt', 'xls', 'xlsx', 'doc', 'docx'])) { throw new Exception("Файл данного формата ({$ext}) не поддерживается"); } if (in_array($ext, ['doc', 'docx'])) { shell_exec("HOME=/var/www/new.marketrf.ru/www_system soffice --headless --convert-to html --outdir " . dirname($infile) . " {$infile}"); $infile = substr($infile, 0, strpos($infile, "." . $ext)) . ".html"; $ext = 'html'; } $path = Yii::getPathOfAlias('core.extensions.SimpleExcel'); require_once $path . DS . "SimpleExcel.php"; $inFileType = SimpleExcel::identity($infile); $file = new SimpleExcel($inFileType); $file->parser->loadFile($infile); $slugger = new SlugBehavior(); $infileName = File::getFileName($infile); $outFileName = $slugger->makeSlug(basename($infileName)) . ".csv"; $outfile = $pathToConvert . DS . $outFileName; $file->convertTo('CSV'); File::checkPermissions($pathToConvert); $file->writer->saveFile($outFileName, $outfile); unset($file); //удаляем временный html файл если входной был doc, docx if (in_array($ext, ['doc', 'docx'])) { unlink($infile); } return [['filename' => $outfile]]; //тут именно так и нужно, что с другой фунцией они возвращали одинаковый результат }
/** * Export selected category in Excel * * @param $category * @param $lang * @param $currency * @param $location */ public function exportAction($category, $lang, $currency, $location) { $this->view->disable(); $cats = PCategory::getCatTree($category, $lang); $currency = strtoupper($currency); $rate = PCrosscurrency::findFirst("title='{$currency}'")->currencyrate; $prods = PProductMain::find(["hold = 0\n AND notforsale = 0\n AND category_id IN ({$cats})"]); // var_dump($prods->toArray()); // Create files directory if it doesn't exist if (!file_exists(ROOT . "/public/files")) { mkdir(ROOT . "/public/files", 0777); } $rnd = $currency == 'UAH' || $currency == 'RUB' ? 0 : 2; $list = []; $list[] = ['ID', 'Название', "Цена, {$currency}", "Доставка, {$currency}", 'URL']; $i = 1; foreach ($prods as $prod) { $list[$i] = [$prod->id, htmlspecialchars_decode($prod->getsingleInfo("lang='{$lang}'")->title), round($prod->priceUSD * $rate, $rnd), $this->unitConvert->deliveryCost($prod, $location) * $rate, "https://madeheart.com/{$lang}/{$prod->id}/" . htmlspecialchars_decode($prod->getsingleInfo("lang='{$lang}'")->url) . ".html"]; $i++; } // var_dump($list); $random = rand(111111, 999999); $csv_file = "files/csv_{$random}.csv"; $excel_file = "files/excel_{$random}.xls"; $csv = fopen($csv_file, 'w'); foreach ($list as $item) { fputcsv($csv, $item); } fclose($csv); $excel = new SimpleExcel('csv'); $excel->parser->loadFile($csv_file); $excel->convertTo('xml'); $excel->writer->saveFile('file', $excel_file); readfile($excel_file); header('Content-Disposition: attachment; filename=list.xls'); unlink($csv_file); unlink($excel_file); }