Export to CSV
Get started #
CSVExportable
trait allows you to export datastores to CSV files. Since version 10.0.0, this is the preferable CSV export function because it's much faster than BigSpreadsheetExportable trait's exportToBigCSV
and has a lot more options.
class MyReport extends \koolreport\KoolReport
{
use \koolreport\excel\CSVExportable;
// use \koolreport\excel\BigSpreadsheetExportable;
...
}
CSV exporting options #
The 'delimiter'
or 'fieldSeparator'
option defines a string used to separate columns in the exported CSV file. Default value is a comma.
'columns'
option is an array defining a list of columns in the exported CSV file. Values could be either column indexes, column keys or column labels. if not specified, all columns are exported. "BOM"
parameter takes boolean value, default is false
, BOM determine whether exported CSV will use UTF8 Bit Order Mark (BOM).
<?php
$report = new MyReport;
$report->run()->exportToCSV('salesReport', array(
'delimiter' => ';',
"columns"=>array(
0, 1, 2, 'column3', 'column4'
)
"BOM"=>false,
))->toBrowser("myreport.csv");
Since version 10.0.0, there're a whole lot of CSV export options added:
$report = new MyReport;
$report->run();
$report->exportToCSV(
array(
"dataStores" => array(
"ordersExport" => [
"separator" => ",", // default separator = "," i.e. comma
"enclosure" => "\"", // default general enclosure = "" i.e. empty string
"enclosure" => ["(", ")"], // all enclosure property could be a 2 element array
"typeEnclosures" => [
"string" => "\"", // default string enclosure is general enclosure
"date" => "\"", // default date enclosure is general enclosure
"datetime" => "\"", // default datetime enclosure is general enclosure
"number" => "", // default number enclosure = "" i.e. empty string
"boolean" => "", // default boolean enclosure = "" i.e. empty string
],
'nullEnclosure' => "", // default = "" i.e empty string
'nullString' => "NULL", // default = false i.e empty string for null value
'useColumnFormat' => 1, // default = 1, set = 0 to increase export speed
'useEnclosureEscape' => 1, // default = 1, set = 0 to increase export speed
'useTypeEnclosure' => 1, // default = 1, set = 0 to increase export speed
"escape" => "\\", // if escape is empty/undefined, double enclosures will be used
"eol" => "\n", // define End of line character, default eol is "\n"
"columns"=>array(
"customerName",
"productName",
"productLine",
"orderDate",
"orderMonth",
"orderYear",
"orderQuarter",
"dollar_sales" => [
"type" => "number",
"enclosure" => ["<", ">"], // to apply custom column enclosure "useCustomColumnEnclosure" must be true
"headerEnclosure" => "\"",
"nullEnclosure" => "",
"nullString" => "nULL",
"enclosureEscape" => "\"",
]
),
'useCustomColumnEnclosure' => 0, // default = 0
'useCustomColumnNullString' => 0, // default = 0
'useCustomColumnEnclosureEscape' => 0, // default = 0
],
),
"useLocalTempFolder" => true,
"BOM" => false, // default bom = false
"buffer" => 1000, // unit: KB ~ 1000 bytes. Default buffer = 1000 KB
),
)
->toBrowser("orders.csv");
With version 10.0.0 CSV export speed is seriously optimized. Exporting hundreds of thousands of rows takes from seconds to more than ten seconds depending how many complex options you set.
Since version 10.5.0 we add "footer" (possible values: "sum", "count", "avg", "min", "max"), "footerText", and "footerFormat" for "columns" setting in CSV export:
$report = new MyReport();
$report->run()->exportToCSV(
array(
"dataStores" => array(
"salesDatastore" => [
"columns"=>array(
...
"dollar_sales" => [
"type" => "number",
"footer" => "sum",
"footerText" => "Total: @dollar_sales",
"footerFormat" => ["type" => "number", "decimals" => 2, "prefix" => "$"],
"footerFormat" => function($sum, $colMeta) { return ...; },
]
),
],
),
),
)
->toBrowser("orders.csv");
Get started with KoolReport
KoolReport will help you to construct good php data report by gathering your data from multiple sources, transforming them into valuable insights, and finally visualizing them in stunning charts and graphs.