Csv
Open csv resource
Open csv resource
Return multidimensional array (array[row][col]=value) as CSV file:
$this->get('csv')
->setFilename('myfile.csv')
->setData($dataArray)
->outputFile();
Create $Csv. Then iterate through the set of pages and add selected values to csv data. Lastly, return data as csv file:
$Csv = $this->get('csv')->setFilename('testname.csv');
foreach ( $Pages as $Page ){
$Csv->addRow(
$Page->name,
$Page->price
);
}
$Csv->outputFile();
Setting the data one cell at a time:
foreach ( $Pages as $Page ){
$Csv->newRow();
$Csv->addColumn($Page->name);
$Csv->addColumn($Page->price);
}
Setting delimiters. Default delimiter is ; and enclosure "
$Csv->setDelimiter(',');
$Csv->setEnclosure('"');
Setting colums:
$Csv->addColumn('Määrä','number'); // value is formatted as number
$Csv->addColumn('Päivä','date'); // value is formatted as date
$Csv->addColumn(Array(
'title' => 'Määrä', // Column title
'name' => 'amount', // Column name. Map associative data.
'type' => 'number', // number, string, date.
'default' => 'X', // Value to use if empty value.
'pad_length' => 10, // Fixed column char length
'pad_string' => '-', // String to use when making pad_length
'pad_type' => 'left', // Add pad string to left/right of the content
'hidden' => false // If TRUE, column is not added to file
));
Fixed length columns:
$Csv->setDelimiter(''); // Do not add delimiter char
$Csv->setEnclosure(''); // Do not add eclosure chars
$Csv->setPadString(" "); // Default padding char
$Csv->setPadType("left"); // Default padding type
$Csv->addColumn(Array(
'pad_length' => 10, // Make content at least 10 char length
'pad_type' => 'right', // Override default pad type
'pad_string' => 'x' // Override default char
));
Set filename. File will be parsed when you ask data from it:
$data = $this->get('csv')->setFilename('testname.csv')->getData();
You can use additional parameters:
$Csv = $this->get('csv')
->setFilename('testname.csv')
->setDelimiter(',')
->setEnclosure('"')
->setLinebreak("CR")
->setEmptyString("")
->setPadString(" ")
->setPadType("left");
$Data = $Csv->getData();
| Method | Return type | Description |
|---|---|---|
| setFilename(string) | this | Set output filename. |
| getFilename() | string | Return the set output filename. |
| setCharset(string) | this | Set file charset. Default: system default charset |
| setBom(bool) | this | Add Byte Order MArk in the beginning of the file. (only UTF-8) |
| setDelimiter(string) | this | Set cell delimiter, Default: , |
| setEnclosure(string) | this | Set cell enclosure, Default: " |
| setLinebreak(string) | this | Set row linebreak, Default: |
| setCellbreak(string) | this | Set linebreak inside cell data. Default: space |
| setEmptyString(string) | this | Value to use when empty cell |
| setPadString(strtig) | this | String to use when padding column to pad_length |
| setPadType(type) | this | Add pad strings to right/left |
| setLocale(string) | this | Set locale for date/number format |
| setColumns(array) | this | Clear columsn and add column definitions. |
| addColumn(string/array) | this | Set column definition |
| Method | Return type | Description |
|---|---|---|
| setContent(array) | this | Clear data and add data from two dimensional array. |
| addContent(array) | this | Add data from array. |
| clearContent() | this | Clear data. |
| setData(array) | this | Set 2-dimensional array. |
| addData(array) | this | Add 2-dimensional array |
| newRow() | this | Add new empty line. |
| addRow(array) | this | Add new row from array. |
| addRow(col1,col2,..) | this | Add new row from arguments. |
| addCell(data) | this | Add new cell to last line. |
| Method | Return type | Description |
|---|---|---|
| getData() | array | Return the data as two dimensional array. |
| getRowIndex() | int | Return the index of last row. |
| isRows() | int | Return TRUE if there are any data. |
| countRows() | int | Return number of lines. |
| countCols() | int | Return max number of cols. |
| getContent() | string | Return the csv content. |
| outputFile() | void | Send CSV file to browser. |
| saveToFile(filename) | this | Save data to file. |