Jump to content

PHPExcel export problem


Naurich

Recommended Posts

Hello everyone.

I can not export data with PHPExcel library. I have two buttons, where one is for data filtering, and another for data export to excel. Filtering is working fine, but when I making export to excel, it giving me just some characters, but not data. Here is my code, could someone explain me, what I am doing wrong.

<?phpinclude_once 'partials/headers.php';?><?phpif(isset($_POST['search'])) {      $search = "%".$_POST['search_text']."%";        //validate the token        if(!empty($search)) {            //check if user exist in the database                $sqlQuery = "SELECT * FROM pers_dat_sanem WHERE par_ko_sniedz LIKE :search";                 $statement = $db->prepare($sqlQuery);                $statement->execute(array(':search'=> $search));                   function getRow($statement) {                    return $statement->fetch();                }        } }?><br><form action="search.php" method="post">                <div class="form-group">                       <div class="input-group">                            <span class="input-group-addon">Meklēt</span>                            <input type="text" name="search_text" id="search_text" placeholder="<?php if(isset ($_POST['search_text'])) {echo ($_POST['search_text']);} ?>" class="form-control" value="" />                       </div>                  </div>                 <div align="center">                <button type="submit" name="search" class="btn btn-default" value="search">Meklēt</button>            </div></form><div class="table-responsive" id="nod_dat"> <?php if(isset($statement)) { $statement->rowCount() > 0 ?>     <h4 align="center">Saņemto personas datu meklēšanas rezultāti</h4>  <table id="result_np" class="table table bordered">      <tr>                                      <th>Datu saņemšanas laiks</th>                                      <th>Persona kas sniedz datus</th>                                        <th>Personas datu sniedzēja adrese</th>                                       <th>Persona kas saņem datus</th>                                        <th>Persona par ko sniedz datus</th>                                        <th>Saņemtie datu veidi</th>                                        <th>Datu saņemšanas mērķis</th>                               </tr>      <?php }; ?><?php if(isset($statement)) {while($row = getRow($statement)) :?>                <tr>                       <td><?php echo $row["dat_san_laik"]; ?></td>                      <td><?php echo $row["pers_kas_dod"]; ?></td>                     <td><?php echo $row["kas_dod_adrese"]; ?></td>                     <td><?php echo $row["pers_kas_sanem"]; ?></td>                      <td><?php echo $row["par_ko_sniedz"]; ?></td>                       <td><?php echo $row["datu_veidi"]; ?></td>                        <td><?php echo $row["datu_san_merk"]; ?></td>                                </tr>          <?php endwhile; ?>  </table>      <form method="post">        <fieldset>            <legend>Lejupielādēt rezultātu</legend>            <p>                <input type="submit" name="download" id="download" value="Lejupielādēt failu">            </p>        </fieldset>    </form>  <?php }; ?></div><?php    if (isset($_POST['download'])) {            require_once 'src/PHPExcel.php';                try {                    $sheet = new PHPExcel();                        $sheet->getProperties()->setCreator('www.zemgale.lv/dvi')                               ->setLastModifiedBy('www.zemgale.lv/dvi')                               ->setTitle('Atrasto datu izdruka')                               ->setKeywords('dati subjekts apstrāde');                    $sheet->getDefaultStyle()->getAlignment()->setVertical(                                PHPExcel_Style_Alignment::VERTICAL_TOP);                    $sheet->getDefaultStyle()->getAlignment()->setHorizontal(                PHPExcel_Style_Alignment::HORIZONTAL_CENTER);        $sheet->getDefaultStyle()->getFont()->setName('Arial');        $sheet->getDefaultStyle()->getFont()->setSize(12);                $sheet->setActiveSheetIndex(0);        $activeSheet = $sheet->getActiveSheet();        $activeSheet->getPageSetup()->setOrientation(                PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE)                ->setFitToWidth(1)                ->setFitToHeight(0);                $activeSheet->getHeaderFooter()->setOddHeader('&C&B&16' .                     $sheet->getProperties()->getTitle())                    ->setOddFooter('&CPage &P of &N');        // Set column headings        $sheet->getActiveSheet()                ->setCellValue('A1', 'Datu saņemšanas laiks')                ->setCellValue('B1', 'Persona kas sniedz datus')                ->setCellValue('C1', 'Personas datu sniedzēja adrese')                ->setCellValue('D1', 'Persona kas saņem datus')                ->setCellValue('E1', 'Persona par ko sniedz datus')                ->setCellValue('F1', 'Saņemtie datu veidi')                ->setCellValue('G1', 'Datu saņemšanas mērķis')                ;        // Populate individual cells with data               //column number, which we will be incrementing$colnum=1;foreach ($row as $value){    $colnum++;    $sheet->getActiveSheet()->SetCellValue('A'."$colnum", $value["dat_san_laik"]);    $sheet->getActiveSheet()->SetCellValue('B'."$colnum", $value["pers_kas_dod"]);    $sheet->getActiveSheet()->SetCellValue('C'."$colnum", $value["kas_dod_adrese"]);    $sheet->getActiveSheet()->SetCellValue('D'."$colnum", $value["pers_kas_sanem"]);    $sheet->getActiveSheet()->SetCellValue('E'."$colnum", $value["par_ko_sniedz"]);    $sheet->getActiveSheet()->SetCellValue('F'."$colnum", $value["datu_veidi"]);    $sheet->getActiveSheet()->SetCellValue('G'."$colnum", $value["datu_san_merk"]);}              // Generate Excel file and download        header('Content-Type: application/vnd.ms-excel;');        header('Content-Disposition: attachment;filename="data.xls"');        header('Cache-Control: max-age=0');         $writer = PHPExcel_IOFactory::createWriter($sheet, 'Excel2007');        $writer->save('php://output');        exit;    } catch (Exception $e) {        $error = $e->getMessage();    }} ?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.