[Flow] export to .csv
Mark Kuiphuis
mark at capesso.com.au
Wed Apr 29 02:51:08 CEST 2015
Hi Andre,
Below is a snippet of the code I am using to export information to CSV.
I'm using a standalone view as the CSV file...
$request = $this->controllerContext->getRequest();
$package = $request->getControllerPackageKey();
$subpackage = $request->getControllerSubpackageKey();
$controller = ucfirst($request->getControllerName());
$action = $request->getControllerActionName();
$standaloneView = new \TYPO3\Fluid\View\StandaloneView();
$standaloneView->setFormat('txt');
$templatePathAndFilename =
sprintf('resource://%s/Private/Templates/%s/%s/%s.%s', $package,
$subpackage, $controller, ucfirst($action), $standaloneView->getFormat());
// find all mitglieder with abteilung Tennis
$records = $this->mitgliedRepository->findTennis();
$standaloneView->setTemplatePathAndFilename($templatePathAndFilename);
$standaloneView->assign('records', $records);
if ($standaloneView) {
$this->response->setHeader('Cache-control', 'public', TRUE);
$this->response->setHeader('Content-type', 'application/octet-stream');
$this->response->setHeader('Content-Description', 'File transfer', TRUE);
$this->response->setHeader('Content-Disposition', 'attachment;
filename="Export.csv"', TRUE);
// As the very last thing, I send the headers to the visitor, before
TYPO3 Flow comes to the part where it renders a HTML template
$this->response->sendHeaders();
echo $standaloneView->render();
exit();
}
If this took place in the MitgliedController in the exportAction(), you
would find the txt template of the file in the folder (but of course you
can put it wherever you want):
/Vendor.Package/Resources/Private/Templates/Mitglied/Export.txt
Then in that file you would find something like:
------------------------------
"First Name","Last Name","Email","Mobile"
<f:for each="{records}"
as="record">{record.name.firstName},{record.name.lastName},{record.email},{record.mobile}
</f:for>
------------------------------
You might need to play a bit with where you can put an enter and where
not...
Regards, Mark
On 28/04/2015 10:39 pm, Andre Hohmann wrote:
>
>
> Hallo,
>
> I want to export the class Mitglied to a csv-file. In my
> MitgliedRepository I make a query like this:
>
> public function findTennis() {
> $query = $this->createQuery();
> $result = $query->matching($query->equals('abteilung', 'Tennis'))
> ->setOrderings(array('name' =>
> TYPO3FlowPersistenceQueryInterface::ORDER_ASCENDING))
> ->execute();
> return $result;
> }
>
> In my controller I tried this:
>
> public function exportTennisAction() {
>
> // find all mitglieder with abteilung Tennis
> $records = $this->mitgliedRepository->findTennis();
>
> $csvPath =
> '/var/www/apps/flow/Packages/Application/ITOOP.Atc/Resources/Private/Export/test.csv';
>
> $fp = fopen($csvPath, 'w');
>
> foreach ($records as $lines) {
> fputcsv($fp, $lines);
> }
> fclose($fp);
> }
>
> When I call the exportTennisAction, I get the following:
>
> #1: WARNING: FPUTCSV() EXPECTS PARAMETER 2 TO BE ARRAY, OBJECT GIVEN IN
> /VAR/WWW/APPS/FLOW/DATA/TEMPORARY/DEVELOPMENT/CACHE/CODE/FLOW_OBJECT_CLASSES/ITOOP_ATC_CONTROLLER_MITGLIEDCONTROLLER.PHP
> LINE 492
>
> Line 492 is:
>
> fputcsv($fp, $lines);
>
> Do I have to format the output of $records to arrays? How should I do
> that?
>
> Thank you!
More information about the Flow
mailing list