我有下面的代碼: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文件。
因此,我想要做這樣的事情(照片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.
感謝。有用。現在我得到了我想要的東西,但我仍然遇到了新的問題。 – Boky
您是否設置了自己的細胞包裝? [鏈接] http://stackoverflow.com/questions/17899857/perserving-newline-characters-in-phpexcel [鏈接] –
這是問題所在。我看到了。它現在就像一個魅力。非常感謝:) – Boky