Download multiple excel file

Hi, is it possible to download multiple excel file using one php file script?

yes, just give it different names as argument to download.

Do i need to duplicate the code to make it download multiple code? How the code look like? Sorry i didn’t know

function doThing($arg)
   echo " I am doing my thing with $arg";

doThing("First thing");
doThing("Second thing");
doThing("Third thing");

Function doThing is where the code to write in excel? And args stand for what? And where shoulf i call the dothing function?

doThing would be a function that can be called repeatedly, like downloading a file.

function downloadFileFromServer($path)
    // here you put the code that downloads the file.
    // $path is the path with the filename that it needs to get

Now, you create an array of the files you need to download,

$filesToDownload = [

// now we call the function to do the work
foreach($filesToDownload as $file)
    // this will call the function for each file to be downloaded.

Any resource link that i can refer for this sample?

Hi, I tried as you said but unfortunately it doesn’t work. Mine doesn’t download as excel but as file

The code is as below

require_once 'Classes/PHPExcel.php';

$hostname = "Server";

$connectioninfo = array( "Database"=>"tbltest", "UID"=>"sa", "PWD"=>"password");
$conn = sqlsrv_connect($hostname,$connectioninfo);

$query = "SELECT DISTINCT(StationID) FROM tblStation";

$result2 = sqlsrv_query($conn,$query);
$name = array();
while($row = sqlsrv_fetch_array($result2))
	$name[] =  $row[$i];

foreach($name as $file)

function loadData($filename)
	$hostname = "Server"; 
	$connectioninfo = array( "Database"=>"tbltest", "UID"=>"sa", "PWD"=>"password");
	$conn = sqlsrv_connect($hostname,$connectioninfo);

	$date = date("Y/m/d");
	$sql = "SELECT TOP 20 * FROM [tbltest].[dbo].[tbldetail] WHERE StationID = '".$filename."' AND CAST(LogDate as date)= '".$date."'";
	$result =sqlsrv_query($conn,$sql);


	$objPHPExcel = new PHPExcel();
	$numRow =1;
	while( $data = sqlsrv_fetch_object($result)) {
			 $numRow = $numRow + 1;
			$objPHPExcel->getActiveSheet()->setCellValue("A".$numRow,  $data->StationID);
			$objPHPExcel->getActiveSheet()->setCellValue("B".$numRow,  $data->TesterID);
			$objPHPExcel->getActiveSheet()->setCellValue("C".$numRow,  $data->LogDate);
			$objPHPExcel->getActiveSheet()->setCellValue("D".$numRow,  $data->TestType);
			$objPHPExcel->getActiveSheet()->setCellValue("E".$numRow,  $data->Barcode);
			$objPHPExcel->getActiveSheet()->setCellValue("F".$numRow,  $data->TestNo);
			$objPHPExcel->getActiveSheet()->setCellValue("G".$numRow,  $data->TestMode);
			$objPHPExcel->getActiveSheet()->setCellValue("H".$numRow,  $data->Result);
			$objPHPExcel->getActiveSheet()->setCellValue("I".$numRow,  $data->Status);
			$objPHPExcel->getActiveSheet()->setCellValue("J".$numRow,  $data->OperatorID);
			$objPHPExcel->getActiveSheet()->setCellValue("K".$numRow,  $data->Remark); 
			$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Station ID');
			$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Tester ID');
			$objPHPExcel->getActiveSheet()->setCellValue('C1', 'Log Date');
			$objPHPExcel->getActiveSheet()->setCellValue('D1', 'Test Type');
			$objPHPExcel->getActiveSheet()->setCellValue('E1', 'Barcode');
			$objPHPExcel->getActiveSheet()->setCellValue('F1', 'Test No');
			$objPHPExcel->getActiveSheet()->setCellValue('G1', 'Test Mode');
			$objPHPExcel->getActiveSheet()->setCellValue('H1', 'Result');
			$objPHPExcel->getActiveSheet()->setCellValue('I1', 'Status');
			$objPHPExcel->getActiveSheet()->setCellValue('J1', 'Operator ID');
			$objPHPExcel->getActiveSheet()->setCellValue('K1', 'Remark');
	header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
	header('Content-Disposition: attachment;filename= '.basename($filename));
	header('Cache-Control: max-age=0');

	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');


So you aren’t downloading the files, you are creating them?

I create and download the excel file

You are sending in a filename, but not adding an extension when it creates the file.

Oh so i need to specify the filename with extension just like you did previously?

Yes, otherwise the file cannot be opened as expected, because the extension tells the computer what to use.

yeah… windows… :roll_eyes:

Sponsor our Newsletter | Privacy Policy | Terms of Service