Search the Community
Showing results for tags 'phpecel'.
-
I have been working on some PHP code to take some SQL data and put it into a Excel workbook with multiple sheets. The data example is below. I want each user and there access to be put on a separate sheet with the sheet name or the user. I am able to get the data from SQL and then create the sheets with the users name but the data is only being copied on the last sheet with all other sheets blank. Can some lend a hand and let me know where I am going wrong with the code below? Example Data: TeamName UserID Deparment Description --------------------------------------------------- Smith_Joe JOE4S 52200 Sales Smith_Joe JOE4S 52201 Sales\Dep Black_BJ BJ3OO 43332 Mrkt My Code: <?php session_start(); $userid = $_SESSION['userid']; $serverName = "sqlserver, 1433"; $connectionInfo = array("UID"=>"userid", "PWD"=>"password", "Database"=>"database"); $conn = sqlsrv_connect( $serverName, $connectionInfo); if( $conn === false ) { echo "Could not connect.\n"; die( print_r( sqlsrv_errors(), true)); } require_once 'Classes/PHPExcel.php'; include 'Classes/PHPExcel/Writer/Excel2007.php'; // Create new PHPExcel object $objPHPExcel = new PHPExcel(); // Set document properties $objPHPExcel->getProperties()->setCreator("Budget SYstem") ->setLastModifiedBy("System") ->setTitle("System") ->setSubject("System Security") ->setDescription("System Security Report") ->setKeywords("office 2007") ->setCategory("Security"); // Create the worksheet $objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->getActiveSheet()->setCellValue('A1', "TeamName") ->setCellValue('B1', "UserID") ->setCellValue('C1', "Departments") ->setCellValue('D1', "Description"); /* Set up and execute the query. */ $tsql = "SELECT MemberOfTeamID AS 'TeamName', SUBSTRING(UserID, CHARINDEX('\', UserID)+1, 250) AS 'UserID', Left(ProfileID, PatIndex('%[^0-9,.,-+-,^a-z]%', ProfileID)) AS 'Departments',SUBSTRING(ProfileID, PatIndex('%[^0-9,.,-+-]%', ProfileID), 8000) AS Description FROM UserProfile u INNER JOIN UserTeamAssign ut ON u.UserID = ut.UserorTeamID WHERE UserID LIKE '%$UsedrID%' AND u.ProfileID != 'Planners' AND u.ProfileID <> '00 Access_All_Models' AND u.ProfileID <> '00 Required for Global Rates' AND MemberOfTeamID <> 'AdminTeam'"; $stmt = sqlsrv_query( $conn, $tsql); $dataArray= array(); $uid = $row['UserID']; while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){ if ($row['UserID'] != $uid){ $objWorksheet = new PHPExcel_Worksheet($objPHPExcel); $objPHPExcel->addSheet($objWorksheet); $objWorksheet->setTitle(''. $row['TeamName']); $objPHPExcel->setActiveSheetIndexByName($row['TeamName']); $row_array['TeamName'] = $row['TeamName']; $row_array['UserID'] = $row['UserID']; $row_array['Departments'] = $row['Departments']; $row_array['Description'] = $row['Description']; array_push($dataArray,$row_array); } $row_array['TeamName'] = $row['TeamName']; $row_array['UserID'] = $row['UserID']; $row_array['Departments'] = $row['Departments']; $row_array['Description'] = $row['Description']; array_push($dataArray,$row_array); $uid = $row['UserID']; } $objPHPExcel->getActiveSheet()->fromArray($dataArray, NULL, 'A2'); // Save Excel 2007 file #echo date('H:i:s') . " Write to Excel2007 format\n"; $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); ob_end_clean(); // We'll be outputting an excel file header('Content-type: application/vnd.ms-excel'); // It will be called file.xls header('Content-Disposition: attachment; filename="security.xlsx"'); $objWriter->save('php://output'); Exit; /* Free statement and connection resources. */ sqlsrv_free_stmt( $stmt); sqlsrv_close( $conn); ?>