How to save the export file unter a selectable directory?

Topics: User Forum
Dec 10, 2011 at 11:02 AM

Hi,

I have a basic question. The script  works like I want, but it is saving the export-file automatically.

How can I let the user to choose in which directory he wants to save the file?

thanks stanneo

Now the script

<?php
ob_start();

/** Error reporting */
error_reporting(E_ALL);

date_default_timezone_set('Europe/London');

/** PHPExcel */
require_once 'Classes/PHPExcel.php';

require_once "settings.php";
require_once "dbconnection.php";



// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set properties
$objPHPExcel->getProperties()->setCreator("TestOnly")
							 ->setLastModifiedBy("TestOnly")
							 ->setTitle("Office 2007 XLSX Test Document")
							 ->setSubject("Office 2007 XLSX Test Document")
							 ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
							 ->setKeywords("office 2007 openxml php")
							 ->setCategory("Test result file");

			
if ($stmt =$mysqli->prepare("SELECT Id, A,B,C  FROM db1t1 order by Id DESC;"));
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($Id, $A, $B, $C);
								
// Add some data, we will use printing features
echo date('H:i:s') . " Add some data\n";

$objPHPExcel->getActiveSheet()->setCellValue('A1','Id');
	$objPHPExcel->getActiveSheet()->setCellValue('B1' ,'A');
	$objPHPExcel->getActiveSheet()->setCellValue('C1' ,'B');
	$objPHPExcel->getActiveSheet()->setCellValue('D1' ,'C');

$i =2;

while($stmt->fetch())

 {

	$objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $Id);
	$objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $A);
	$objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $B);
	$objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $C);
	
	$i++;

}	

// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('db1t1');



// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Save Excel 2007 file
echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));


// Echo memory peak usage
echo date('H:i:s') . " Peak memory usage: " . (memory_get_peak_usage(true) / 1024 / 1024) . " MB\r\n";

// Echo done
echo date('H:i:s') . " Done writing file.\r\n";
ob_end_flush();
?>

 

 

 

Coordinator
Dec 14, 2011 at 12:05 PM
Edited Dec 14, 2011 at 12:07 PM

$objWriter->save('/directory/where/I/want/to/save/the/file/filename.xlsx');

for saving the directory on the server

If you're talking about on a client PC, then use php://output as the save filename, and ensure you send the correct headings to the browser, as shown in /Tests/01simple-download-xlsx.php

Dec 14, 2011 at 2:10 PM
MarkBaker wrote:

$objWriter->save('/directory/where/I/want/to/save/the/file/filename.xlsx');

for saving the directory on the server

If you're talking about on a client PC, then use php://output as the save filename, and ensure you send the correct headings to the browser, as shown in /Tests/01simple-download-xlsx.php

thanks very much, I think it helps for the  first 

but it is also possible to let the user to choose the saving  directory like windows dialog "saving under" or at least as browser dialog "saving under"

The php script, which I posted (ExcelExport.php) runs after klicking on a buttom "Export in Excel" in a diffrent  site.

'<input type=button style="background-color:red"  value="Export in Excel" onClick="javascript:popUp(\'ExcelExport.php\')">',

I hope, I can make clear what I means

Thank you very much and greetings

stanneo