Example #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> ";
        }
    }
Example #2
0
<p>List of the sheets in this workbook (indexed by sheetId):</p>

<?php 
echo debug($sheetNames);
?>
<hr>


<h2>All Sheets</h2>

<p>Loop through all sheets, printing the sheet name and all of the sheet data in a table</p>

<?php 
foreach ($sheetNames as $sheetName) {
    $sheet = $xlsx->getSheet($sheetName);
    ?>
	<h3><?php 
    echo escape($sheetName);
    ?>
</h3>
	<?php 
    array2Table($sheet->getData());
}
?>
<hr>

<h2>Date Handling</h2>
<p>Excel dates are returned as integers and need to be converted to Unix timestamps for ease of use in PHP.  
This examples takes the data in the 'Dates' sheet and modifies it to include columns for the Excel date,
the converted Unix Timestamp, the timestamp in ISO 8601 format, and the data from the second column in the sheet.</p>