Skip to content

Instantly share code, notes, and snippets.

@Feiron
Last active June 29, 2018 09:26
Show Gist options
  • Save Feiron/56fbc4e00a1a5cccb20aed07198623b3 to your computer and use it in GitHub Desktop.
Save Feiron/56fbc4e00a1a5cccb20aed07198623b3 to your computer and use it in GitHub Desktop.
Создает из 1ого екселя, ексель где дублируется количество строк, согласно столбцу количество
<? require($_SERVER["DOCUMENT_ROOT"] . "/bitrix/header.php");
$APPLICATION->SetTitle("Описание");
require $_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/include/phpexcel/vendor/autoload.php';
$strFileDir = $_SERVER['DOCUMENT_ROOT'] . '/testing/excel/';
$arFiles = array_diff(scandir($strFileDir), array('..', '.'));
foreach ($arFiles as $key => $strFileName) {
if (stristr($strFileName, 'M_')) continue;
parseFileForMarks($strFileName, $strFileDir);
}
function parseFileForMarks($strFileName, $strFileDir)
{
$strCntName = 'Количество';
$strFilePath = $strFileDir . $strFileName;
$obExcelOriginal = PHPExcel_IOFactory::load($strFilePath);
$obActiveSheetOriginal = $obExcelOriginal->getActiveSheet();
$iHighestColumnIndex = PHPExcel_Cell::columnIndexFromString($obActiveSheetOriginal->getHighestColumn());
$iHighestRowIndex = $obActiveSheetOriginal->getHighestRow();
$arHeaders = $arData = array();
$iCntIndex = false;
//for ($j = 1; $j <= $iHighestRowIndex; $j++) {
for ($j = 1; $j <= $iHighestRowIndex; $j++) {
$arRow = array();
for ($i = 0; $i < $iHighestColumnIndex; $i++) {
$strValue = $obActiveSheetOriginal->getCellByColumnAndRow($i, $j)->getValue();
$arRow[] = $strValue;
}
if ($j == 1) {
$arHeaders = $arRow;
$iCntIndex = array_search($strCntName, $arHeaders);
if (!$iCntIndex) {
throw new \Exception('MISSING_CNT_COLUMN');
}
} else {
for ($x = 0; $x < $arRow[$iCntIndex]; $x++) {
$arData[] = $arRow;
}
}
}
$obExcel = new PHPExcel();
$obExcel->setActiveSheetIndex(0);
foreach ($arHeaders as $key => $strHeader) {
$obExcel->getActiveSheet()->setCellValueByColumnAndRow($key, 1, $strHeader);
}
foreach ($arData as $iRowIndex => $arRowValues) {
foreach ($arRowValues as $iCellIndex => $strValue) {
$obExcel->getActiveSheet()->setCellValueByColumnAndRow($iCellIndex, $iRowIndex + 2, $strValue);
}
}
$objWriter = new PHPExcel_Writer_Excel2007($obExcel);
$objWriter->save($strFileDir . 'M_' . $strFileName);
$obExcel->getActiveSheet()->setTitle(date("m.d.Y") . '-companies');
$objWriter = new PHPExcel_Writer_Excel2007($obExcel);
$APPLICATION->RestartBuffer();
header('Content-type: application/vnd.ms-excel');
// It will be called file.xls
header('Content-Disposition: attachment; filename="' . date("m_d_Y") . '_' . 'companies.xlsx"');
// Write file to the browser
$objWriter->save('php://output');
die();
}
require($_SERVER["DOCUMENT_ROOT"] . "/bitrix/footer.php");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment