use Illuminate\Support\Facades\Route;
use NumberFormatter;
use voku\helper\HtmlDomParser;
-use Cubist\Excel\Excel;
use Cubist\Util\Files\Files;
use Cubist\Util\Str;
use PhpOffice\PhpSpreadsheet\Exception;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
+use PhpOffice\PhpSpreadsheet\Spreadsheet;
+use PhpOffice\PhpSpreadsheet\Style\Alignment;
+use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
+use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
+
+
// __('!! Statistiques')
trait StatsOperation
{
public function generateExcel($fluidbook_id, $hash, $date = null, $period_override = null) {
$url = route('stats', compact('fluidbook_id', 'hash', 'date', 'period_override'));
- $name = "excel example";
+ $name = "excel";
$safename = Str::slug($name);
$html = HtmlDomParser::file_get_html($url);
- $tr = $html->find('table', 0)->find('tr');
- $excelData = [];
- $keys = [];
- $values = [];
+ foreach ($html->find('html') as $e) {
+ $e->lang = "fr";
+ }
+ dd($html->plaintext);
+ $tables = $html->find('table');
+ $tmpfile = Files::tempnam() . '.xlsx';
+ $this->Excel_($tables, $name, null, $tmpfile);
+ return response()->download($tmpfile, $safename . '.xlsx')->deleteFileAfterSend(true);
+ die();
+ }
- foreach ($tr as $lines) {
- $keys[] = $lines->find('td',0)->text;
- $values[] = trim(preg_replace('/\s\s+/', ' ', $lines->find('td',1)->text));
+ /**
+ * @param $tables array
+ * @param $sheetname string
+ * @param $head array|null
+ * @param $output string|null
+ * @param $width int|null
+ * @return Spreadsheet
+ * @throws \PhpOffice\PhpSpreadsheet\Exception
+ */
+ public function Excel_($tables, $sheetname, $head = null, $output = null, $width = null)
+ {
+ $columns = 0;
+
+ $excel = new Spreadsheet();
+ $excel->getDefaultStyle()
+ ->getNumberFormat()
+ ->setFormatCode(
+ NumberFormat::FORMAT_TEXT
+ );
+
+ foreach ($tables as $key => $table) {
+ $sheet = $excel->createSheet();
+ $sheet->setTitle($sheetname.'-'.$key);
+
+ $tr = $table->find('tr');
+ $theads = $table->find('th');
+ $head = [];
+ $datas = [];
+
+ if($theads) {
+ foreach ($theads as $thead) {
+ $head[] = $thead->text;
+ }
+ }
+
+ foreach ($tr as $k => $lines) {
+ foreach ($lines->find('td') as $td) {
+ $datas[$k][] = trim(preg_replace('/\s\s+/', ' ', $td->text));
+ }
+ }
+
+ $line = 1;
+ if (!is_null($head) && sizeof($head) > 0) {
+ $c = 0;
+ foreach ($head as $label) {
+ $c++; //must be positive value
+ $columns = max($columns, $c);
+ $cell = $sheet->getCellByColumnAndRow($c, $line);
+ $cell->setValue($label);
+ $style = $sheet->getStyleByColumnAndRow($c, $line);
+ $style->getFont()->setBold(true);
+ $style->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
+ }
+ $line++;
+ }
+
+ foreach ($datas as $l) {
+ $c = 1;
+ foreach ($l as $v) {
+ $columns = max($columns, $c);
+ $sheet->getCellByColumnAndRow($c, $line)->setValue($v);
+ $style = $sheet->getStyleByColumnAndRow($c, $line);
+ $style->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
+ $style->getAlignment()->setWrapText(true);
+ $c++;
+ }
+ $line++;
+ }
+
+ for ($i = 0; $i <= $columns; $i++) {
+ if (null === $width || !isset($width[$i]) || null === $width[$i] || $width[$i] == 'auto') {
+ $sheet->getColumnDimensionByColumn($i)->setAutoSize(true);
+ } else {
+ $sheet->getColumnDimensionByColumn($i)->setAutoSize(false)->setWidth($width[$i]);
+ }
+ }
}
- $excelData[] = $keys;
- $excelData[] = $values;
+ if (null !== $output) {
+ self::_save($excel, $output);
+ }
- //dump($excelData);
- //die();
+ return $excel;
+ }
- $tmpfile = Files::tempnam() . '.xlsx';
- Excel::fromArray('name', $excelData, null, $tmpfile);
- return response()->download($tmpfile, $safename . '.xlsx')->deleteFileAfterSend(true);
- die();
+ protected static function _save($excel, $output)
+ {
+ $writer = new Xlsx($excel);
+ $writer->save($output);
}
+
}