2015-08-16 95 views
0

我有下面的代碼:PHPExcel從數據庫轉換爲XLSX

<?php 
     require_once 'PHPExcel/Classes/PHPExcel.php'; 
     include "PHPExcel/Classes/PHPExcel/Writer/Excel2007.php"; 
     session_start(); 
     include("config.php"); 
     global $kon; 
     ob_start(); 
     $excel = new PHPExcel; 
     $excel->getProperties()->setCreator('Boris Jelic'); 
     $excel->getProperties()->setLastModifiedBy('Boris Jelic'); 
     $excel->getProperties()->setTitle('Orders'); 
     $excel->removeSheetByIndex(0); 


     $cols = array('tijd' => 'A', 'shop' => 'B', 'products' => 'C', 'naam' => 'D', 'adres' => 'E', 'gemeente' => 'F', 'telefoonnummer' => 'G', 'email' => 'H', 'leeggoed' => 'I'); 
     $list = $excel->createSheet(); 
     $list->setTitle('Users'); 
     $list->getColumnDimension('A')->setWidth(20); 
     $list->getColumnDimension('B')->setWidth(25); 
     $list->getColumnDimension('C')->setWidth(40); 
     $list->getColumnDimension('D')->setWidth(40); 
     $list->getColumnDimension('E')->setWidth(40); 
     $list->getColumnDimension('F')->setWidth(20); 
     $list->getColumnDimension('G')->setWidth(15); 
     $list->getColumnDimension('H')->setWidth(40); 
     $list->getColumnDimension('I')->setWidth(40); 
     $list->setCellValue('A1', 'Tijd'); 
     $list->setCellValue('B1', 'Shop'); 
     $list->setCellValue('C1', 'Products'); 
     $list->setCellValue('D1', 'Naam en voornaam'); 
     $list->setCellValue('E1', 'Adres'); 
     $list->setCellValue('F1', 'Gemeente'); 
     $list->setCellValue('G1', 'Telefoonnummer'); 
     $list->setCellValue('H1', 'Email'); 
     $list->setCellValue('I1', 'Leeggoed'); 


     //za background 
     $excel->getActiveSheet()->getStyle('A1:I1')->getFill() 
     ->setFillType(PHPExcel_Style_Fill::FILL_SOLID) 
     ->getStartColor()->setARGB('FFE8E5E5'); 

     //stavljamo naslove bold 
     $excel->getActiveSheet()->getStyle('A1:I1')->getFont()->setBold(true); 

     //povecavamo velicinu slova 
     $excel->getActiveSheet()->getStyle('A1:I1')->getFont()->setSize(13); 


     //moramo prvo uzeti sve orders sa statusom 1 
     $rezOrders = mysqli_query($kon, "SELECT 
             shops.naam as shopNaam, 
             GROUP_CONCAT(producten.naam SEPARATOR '\r') as prodNaam, GROUP_CONCAT(producten.id SEPARATOR '\r') as prodID, 
             order_details.aantal as kolicina, order_details.leeggoed as leeggoed, order_details.id as ordDetId, SUM(order_details.aantal) as prodCount, 
             korisnici.ime as korNaam, korisnici.email as korMail, korisnici.id as korId, korisnici.prezime as korPrez, korisnici.mobitelBroj as broj, korisnici.mjesto as adres, 
             korisnici.huisnummer as hn, 
             korisnici.gemeente as gemeente, 
             orders.besteld_op as tijd 
             FROM orders 
             INNER JOIN korisnici ON korisnici.id = orders.user_id 
             INNER JOIN order_details ON order_details.order_id = orders.id 
             INNER JOIN product_shop_tt ON order_details.product_shop_tt_id = product_shop_tt.id 
             INNER JOIN producten ON producten.id = product_shop_tt.product_id 
             INNER JOIN shops ON shops.id = product_shop_tt.shop_id 
             WHERE orders.status = 1 GROUP BY korisnici.id"); 

     $rowcounter = 2; 

     while ($row = mysqli_fetch_assoc($rezOrders)){ 


      $prod_id = $row['prodID']; 
      $prodKol = $row["prodNaam"] . " -- Aantal : " . $row["kolicina"] . "x"; 
      echo "Product + quantity : " . $prodKol . "<br />"; 


      //Da uzmemo aantal za taj product 


      /*echo "Product i kolicina : " . $redAan["productNaam"] . " - " . $redAan["kolicina"] . "<br />";*/ 
      $tijd = $row["tijd"]; 
      $ime = $row["korNaam"] . " " . $row["korPrez"]; 
      $email = $row["korMail"]; 
      $telNummer = $row["broj"]; 
      $gemeente = $row["gemeente"]; 
      $adresa = $row["adres"] . " " . $row["hn"]; 
      $leegoed = $row["leeggoed"]; 

      $list->setCellValue('A'.$rowcounter, $tijd); 
      $list->setCellValue('B'.$rowcounter, $row['shopNaam']); 
      $list->setCellValue('C'.$rowcounter, $prodKol."\r"); 
      $list->getStyle('C'.$rowcounter)->getAlignment()->setWrapText(true); 
      $list->setCellValue('D'.$rowcounter, $ime); 
      $list->setCellValue('E'.$rowcounter, $adresa); 
      $list->setCellValue('F'.$rowcounter, $gemeente); 
      $list->setCellValue('G'.$rowcounter, $telNummer); 
      $list->setCellValue('H'.$rowcounter, $email); 
      $list->setCellValue('I'.$rowcounter, $leegoed); 
      $rowcounter++; 
     } 


     $writer = new PHPExcel_Writer_Excel2007($excel); 
     if($writer->save('files/users.xlsx')){ 
      echo "Konvertovan je."; 
     } 
    ?> 

我將結果保存在XLSX檔案,一切工作正常,但我試圖讓的內容中有一點點不同的顯示我的excel文件。

現在我得到這樣的withing我的Excel文件(照片1) enter image description here

但我想獲得像 enter image description here

因此,我想要做這樣的事情(照片2)由商店分組。如果一個用戶從同一商店訂購了更多產品,並且它們都顯示在excel文件的一行中,而不是像現在這樣,則會爲每個產品創建一個新行。

修訂 當我添加GROUP_CONCAT(producten.naam SEPARATOR '\ r')作爲prodNaam和GROUP BY korisnici.id我得到的比對結果在我的Excel文件。完全像op照片2.但是接下來我有下一個問題。我無法獲得每個產品的order_details.aantal值,我比每個用戶只有第一個產品的值更高。但如果我把GROUP BY order_details.id然後我得到每個產品的正確的order_details.aantal值,但然後我得到我的excel文件就像照片1.

回答

1

一個簡單的方法來做到這一點將是使用GROUP BY和GROUP_CONCAT在你的SQL語句中。

特別是,將您的查詢按代表「用戶」和「商店」的字段分組,而不是簡單地選擇producten.naam作爲prodNaam,而是使用GROUP_CONCAT(producten.naam,'\ r')。

欲瞭解更多關於PHPExcel換行的信息,請點擊此處:how to make New lines in a cell using phpexcel

+0

感謝。有用。現在我得到了我想要的東西,但我仍然遇到了新的問題。 – Boky

+1

您是否設置了自己的細胞包裝? [鏈接] http://stackoverflow.com/questions/17899857/perserving-newline-characters-in-phpexcel [鏈接] –

+0

這是問題所在。我看到了。它現在就像一個魅力。非常感謝:) – Boky

0

你可以做這樣的事情

$userHolder = ''; 
$shopHolder = ''; 
while ($row = mysqli_fetch_assoc($rezOrders)){ 

    if($userHolder != $value['user']){ 
    $list->setCellValue('A'.$rowcounter, $row['korNaam']); 
    $userHolder = $value['user']; 
    $shopHolder = ''; 
    } 

    if($shopHolder != $value['shop']){ 
    $list->setCellValue('B'.$rowcounter, $row['shopNaam']); 
    $shopHolder = $value['shop']; 
    } 
    $list->setCellValue('C'.$rowcounter, $row['prodNaam']); 
    $rowcounter++; 
} 
+0

謝謝。我嘗試了Brendans的答案,它的工作,但現在我有一個新問題。我更新了我的問題。 – Boky