PHPExcel 从数据库转换为 xlsx
PHPExcel converting from a database to 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 文件中显示一些不同的内容。
现在我的 excel 文件得到了类似的东西(照片 1)
但我想要这样的东西(照片2)
所以我想做一些类似按商店分组的事情。如果一个用户从同一家商店订购了更多产品,它们都显示在 excel 文件的一行中,而不像现在,它会为每个产品创建一个新行。
已更新
当我将 GROUP_CONCAT(producten.naam SEPARATOR '\r') 添加为 prodNaam 和 GROUP BY korisnici.id 我得到的结果比 excel 文件。与照片 2 完全一样。但是我遇到了下一个问题。我无法获得每个产品的 order_details.aantal 价值,我只能获得每个用户的第一个产品的价值。但是如果我把 GROUP BY order_details.id 然后我得到每个产品的正确 order_details.aantal 值但是然后我得到我的 excel 文件就像照片 1.
一个简单的方法是在 SQL 语句中使用 GROUP BY 和 GROUP_CONCAT。
特别是,按代表 "User" 和 "Shop" 的字段对查询进行分组,而不是简单地选择 producten.naam 作为 prodNaam,使用 GROUP_CONCAT(producten.naam, '\r').
有关 PHPExcel 换行符的更多信息,请查看此处:how to make New lines in a cell using phpexcel。
你可以这样做
$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++;
}
我有下一个代码:
<?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 文件中显示一些不同的内容。
现在我的 excel 文件得到了类似的东西(照片 1)
但我想要这样的东西(照片2)
所以我想做一些类似按商店分组的事情。如果一个用户从同一家商店订购了更多产品,它们都显示在 excel 文件的一行中,而不像现在,它会为每个产品创建一个新行。
已更新 当我将 GROUP_CONCAT(producten.naam SEPARATOR '\r') 添加为 prodNaam 和 GROUP BY korisnici.id 我得到的结果比 excel 文件。与照片 2 完全一样。但是我遇到了下一个问题。我无法获得每个产品的 order_details.aantal 价值,我只能获得每个用户的第一个产品的价值。但是如果我把 GROUP BY order_details.id 然后我得到每个产品的正确 order_details.aantal 值但是然后我得到我的 excel 文件就像照片 1.
一个简单的方法是在 SQL 语句中使用 GROUP BY 和 GROUP_CONCAT。
特别是,按代表 "User" 和 "Shop" 的字段对查询进行分组,而不是简单地选择 producten.naam 作为 prodNaam,使用 GROUP_CONCAT(producten.naam, '\r').
有关 PHPExcel 换行符的更多信息,请查看此处:how to make New lines in a cell using phpexcel。
你可以这样做
$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++;
}