$i = 0;
$file_name = 'xls/Spisok.xls';
$file_name1 = 'xls/Spisok1.xls';
require_once 'classes/PHPExcel.php';
$phpexcel = PHPExcel_IOFactory::load($file_name);
$page = $phpexcel->setActiveSheetIndex(0);
$query = 'SELECT * FROM list';
$result = pg_query($query) or die('Zaprosda muammo: ' . pg_last_error());
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
$j = $i+2;
$page->setCellValueByColumnAndRow(0,$j,$line4["p_id"]);
$page->setCellValueByColumnAndRow(1,$j,$line4["surname"]);
$page->setCellValueByColumnAndRow(2,$j,$line4["name"]);
$page->setCellValueByColumnAndRow(3,$j,$line4["middlename"]);
$page->setCellValueByColumnAndRow(4,$j,$line4["adress"]);
if (isset($_POST["c5"]) && (!empty($_POST["c5"]))){
$page->setCellValueByColumnAndRow(5,$j,$line4["birth"]);
}
if (isset($_POST["c6"]) && (!empty($_POST["c6"]))){
$page->setCellValueByColumnAndRow(6,$j,$line4["sex"]);
}
if (isset($_POST["c7"]) && (!empty($_POST["c7"]))){
$page->setCellValueByColumnAndRow(7,$j,$line4["nationality"]);
}
if (isset($_POST["c8"]) && (!empty($_POST["c8"]))){
$page->setCellValueByColumnAndRow(8,$j,$line4["marital"]);
}
$i=$i+1;
}
$objWriter = PHPExcel_IOFactory::createWriter($phpexcel, 'Excel2007');
$objWriter->save($file_name1);
pg_free_result($result);
pg_close($conn);