Ejemplo n.º 1
0
    public function import($list_value)
    {
        $added = 0;
        $fail = 0;
        $update = 0;
        $tempReport = 1;
        //ID инфоблка путём обрезки из выбронного поля списка
        $IBlockId = str_replace('[', '', explode("]", $list_value)[0]);
        $IBlock = $this->getBlockByID($IBlockId);
        if (isset($_FILES['file']) && $_FILES['file']['size'] > 0) {
            $fileSrc = $_FILES['file']['tmp_name'];
            $xlsx = new \XLSXReader($fileSrc);
            //Используем методы класса XLSXReader
            $sheetNames = $xlsx->getSheetNames();
            foreach ($sheetNames as $sheetName) {
                $sheet = $xlsx->getSheet($sheetName);
                $arSheetList[$sheetName] = $sheet->getData();
                //Массив с ключами элементов
                $arRowNames = $arSheetList[$sheetName][0];
                foreach ($arSheetList[$sheetName] as $row) {
                    for ($i = 0; $i < count($row); $i++) {
                        $arTemp[$arRowNames[$i]] = $row[$i];
                    }
                    $arListRows[] = $arTemp;
                }
                unset($arListRows[0]);
                //Удаляем массив с ключами
                $arList[$sheetName] = $arListRows;
                //Форматированный массив файла .xlsx
                $arListRows = array();
                //обнуляем массив для для случая с несколькими листами
            }
            $arResult['LIST'] = $arList;
            $el = new \CIBlockElement();
            ?>

            <div class="report_detail">
                <button class="close_show_list" id="closeReportDetail" onclick="closeReportDetail();">X</button>

                <?php 
            foreach ($arResult['LIST'] as $arItem) {
                ?>
                    <?php 
                foreach ($arItem as $item) {
                    if (!$item['CODE']) {
                        $arParams = array("replace_space" => "-", "replace_other" => "-");
                        $item['CODE'] = Cutil::translit($item['NAME'], "ru", $arParams);
                    }
                    global $USER;
                    $arLoadProductArray = array("MODIFIED_BY" => $USER->GetID(), "IBLOCK_SECTION_ID" => false, "IBLOCK_ID" => $IBlock['ID'], "NAME" => $item['NAME'], "CODE" => $item['CODE'], "ACTIVE" => $item['ACTIVE'], "PREVIEW_TEXT" => $item['PREVIEW_TEXT'], "DETAIL_TEXT" => $item['DETAIL_TEXT']);
                    $IBlockElement = $this->getBlockElementByCode($item['CODE']);
                    if ($PRODUCT_ID = $el->Add($arLoadProductArray)) {
                        echo $tempReport . ": Added: " . $item['CODE'] . "<br/>";
                        $added++;
                        $tempReport++;
                    } elseif (isset($IBlockElement)) {
                        if ($arLoadProductArray != array_intersect_key($IBlockElement, $arLoadProductArray)) {
                            $el->Update($IBlockElement['ID'], $arLoadProductArray);
                            echo $tempReport . ": Update: " . $IBlockElement['CODE'] . "<br/>";
                            $update++;
                            $tempReport++;
                        }
                    } else {
                        echo $tempReport . ": Error: " . $el->LAST_ERROR;
                        $fail++;
                        $tempReport++;
                    }
                }
            }
            if ($tempReport == 1) {
                echo "Пусто";
            }
            ?>
            </div>
            <div class="report">Added: <?php 
            echo $added;
            ?>
; Updated: <?php 
            echo $update;
            ?>
; Fail: <?php 
            echo $fail;
            ?>
;</div>
            <button class="show_logs">Show Logs</button><?php 
        } else {
            echo "<span class='error_mes'><b>Файл не выбран!</b></span> ";
        }
    }
Ejemplo n.º 2
0
<?php

date_default_timezone_set('UTC');
require 'XLSXReader.php';
$xlsx = new XLSXReader('sample.xlsx');
$sheetNames = $xlsx->getSheetNames();
?>
<!DOCTYPE html>
<html>
<head>
	<title>XLSXReader Sample</title>
	<style>
		body {
			font-family: Helvetica, sans-serif;
			font-size: 12px;
		}

		table, td {
			border: 1px solid #000;
			border-collapse: collapse;
			padding: 2px 4px;
		}
	</style>
</head>
<body>
<h1>XLSXReader Examples</h1>

<h2>Sheet Names</h2>

<p>List of the sheets in this workbook (indexed by sheetId):</p>