KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Error when exporting to excel #3275

Open TICGAL opened this topic on on Apr 3 - 4 comments

TICGAL commented on Apr 3

I have a bar chart showing the number of open and resolved requests per month.

But when exporting to excel it uses only the first value to fill all columns. If the array index of the data is numeric, it does not print that column.

Array ( [period] => 2024-03 [1] => 5 [2] => 0 )

The version of the library is 10.7.0

Sebastian Morales commented on Apr 5

Would you pls post your export and excel view file code for us to check them for you?

TICGAL commented on Apr 8

Export to excel

class PluginReportingSetting extends \koolreport\KoolReport
{
	use \koolreport\export\Exportable;
	use \koolreport\excel\ExcelExportable;

	public function generateExcel($is_browser = true)
	{
		if ($is_browser) {
			$this->exportToExcel("templateExcel")->toBrowser("report.xls");
		} else {
			$this->exportToExcel("templateExcel")->saveAs(GLPI_TMP_DIR . "/report.xls");
		}
	}
}

Excel view

<?php
global $DB;

$options = $this->params;
$iterator = [];
$report = new PluginReportingReporting();
if ($report->getFromDB($options['id'])) {
	$iterator = $DB->request([
		'FROM'   => PluginReportingChart::getTable(),
		'WHERE'  => ['plugin_reporting_reportings_id' => $report->getID()],
		'ORDER' => 'ranking ASC'
	]);
}
?>
<div sheet-name="<?php echo __('Filter') ?>">
	<div><?php echo $report->fields['name'] ?></div>
	<div><?php echo $report->fields['comment'] ?></div>
	<?php
	$filters = [];
	foreach ($iterator as $data) {
		$setting = new $data['chart_class']();
		$filters = array_merge($filters, $setting::getFilters($data['chart_name']));
	}

	foreach ($filters as $filter) {
		$value = '';
		if (isset($options[$filter])) {
			$value = $options[$filter];
		}
		echo "<div>";
		echo $setting::getFilterName($filter, $value);
		echo "</div>";
	}
	?>
</div>
<?php
foreach ($iterator as $data) {
	$setting = new $data['chart_class']();
	$function = $setting::getChartFunction($data['chart_name']);
	if ($function) {
		$setting->run();
		$info = $setting->{$function}($options);
		$name = $setting::getChartName($data['chart_name']);
		if (strlen($name) > 31) {
			$name = substr($name, 0, 31);
		}
		echo "<div sheet-name='" . $name . "'>";
		if ($data['chart_type'] == $setting::CARD) {
			echo "<div>" . $info['data'] . "</div>";
		} else {
			echo "<div>";
			$info['data'] = $setting::cleanHtml($info['data']);
			\koolreport\excel\Table::create([
				'dataSource' => new koolreport\core\DataStore($info['data']),
			]);
			echo "</div>";
		}
		echo "</div>";
	}
}
?>
Sebastian Morales commented on Apr 15

When you created a DataStore object as datasource for excel Table, pls set its column meta information such as column names, types, etc as well:

https://www.koolreport.com/docs/datastore/overview/#other-methods-columnmeta

Also make sure that the DataStore object's data matches its meta. Let us know how this works. Tks,

TICGAL commented on Apr 16

Hello. The solution works.

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
bug
solved

Excel