Quantcast
Channel: PHPExcel Forum Rss Feed
Viewing all articles
Browse latest Browse all 2707

New Post: PHPExcel Set cell with variable

$
0
0
Thanks for the help. Here is my PROD script could you take a look at it and tell me how I would combine what you have into it? Right now it is pulling information from SQL which looks like this.
TeamName      UserID     Deparment     Description
---------------------------------------------------
Smith_Joe    JOE4S       52200          Sales
Smith_Joe    JOE4S       53002          Budget
Smith_Joe    JOE4S       43302          Dev
Black_BJ     BJ3OO       43332          Mrkt
Steh_Tray    ST44S       65573          Prod
Steh_Tray    ST44S       52200          Sales
Here is the code.
<?php 
session_start();
$userid = $_SESSION['userid'];


//Connect
    $ad = ldap_connect("ldap.mc.org");
    
    //Set some variables
    ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3);
    ldap_set_option($ad, LDAP_OPT_REFERRALS, 0);
    
    //Bind to the ldap directory
    $bd = ldap_bind($ad,"adminid","admin");
        
        
        $serverName = "server, 1433";
$connectionInfo = array("UID"=>"adminweb", "PWD"=>"adminweb", "Database"=>"PROD");
$conn = sqlsrv_connect( $serverName, $connectionInfo);



    require_once 'Classes/PHPExcel.php';
    include 'Classes/PHPExcel/Writer/Excel2007.php';
    
    
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
 
// Set document properties
$objPHPExcel->getProperties()->setCreator("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', "No")
$objPHPExcel->getActiveSheet()->setCellValue('A1', "TeamName")
//->setCellValue('B1', "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',ProfileID AS Departments FROM UserProfile u INNER JOIN UserTeamAssign ut ON u.UserID = ut.UserorTeamID WHERE UserID LIKE '%$user%' 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();


 
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))


{

$user=$row['UserID'];
//Search the directory
    $username = ldap_search($ad, "dc=corp,dc=mc,dc=org", "(sAMAccountName=$user)");

   //Create result set
    $entries = ldap_get_entries($ad, $username);
    
   for ($i=0; $i < $entries["count"]; $i++)
    {
        
        $name = $entries[$i]["cn"][0]."";
        }

if ($row['UserID'] != $uid){
     
    $tn=$row['TeamName'];
    $dataArray = array();
     
    $objWorksheet = new PHPExcel_Worksheet($objPHPExcel);
    $objPHPExcel->addSheet($objWorksheet);
    $objWorksheet->setTitle(''. $tn);
    
    
    $objRichText1 = new PHPExcel_RichText();
    
$objBolded1 = $objRichText1->createTextRun('Department Access Confirmation Form');
$objBolded1->getFont()->setBold(true); 
$objWorksheet->getCell('A1')->setValue($objRichText1);
$objWorksheet->mergeCells('A1:D1');
$objWorksheet->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);


    $objRichMsg1 = new PHPExcel_RichText();

$objmsg1 = $objRichMsg1->createTextRun('Stated below is your current list of department/s that you have access to via the system.  Please review and confirm if department access is accurate.  If a department needs to be added and/or removed, please note in the comment section.  Please sign and date indicating your agreement after changes are made.'); 
$objmsg1->getFont()->setBold(false);
$objWorksheet->getStyle('A3')->getAlignment()->setWrapText(true);

$objWorksheet->getCell('A3')->setValue($objRichMsg1);
$objWorksheet->getColumnDimension('B')->setWidth(76);
$objWorksheet->getColumnDimension('A')->setWidth(19);
$objWorksheet->getRowDimension(3)->setRowHeight(58.50);
$objWorksheet->mergeCells('A3:C3');

    $objRichUser1 = new PHPExcel_RichText();

$objUser1 = $objRichUser1->createTextRun('User:'); 
$objUser1->getFont()->setBold(true); 
$objWorksheet->getCell('A5')->setValue($objRichUser1);


    $objRichDept1 = new PHPExcel_RichText();

$objDept1 = $objRichDept1->createTextRun('Department Access:'); 
$objDept1->getFont()->setBold(true); 
$objWorksheet->getCell('B5')->setValue($objRichDept1);

    $objRichCom1 = new PHPExcel_RichText();

$objCom1 = $objRichCom1->createTextRun('Comments:'); 
$objCom1->getFont()->setBold(true); 
$objWorksheet->getCell('A20')->setValue($objRichCom1);
    

    $objRichSig1 = new PHPExcel_RichText();

$objSig1 = $objRichSig1->createTextRun('Signature:_____________________________________________   Date:___________'); 
$objSig1->getFont()->setBold(true); 
$objWorksheet->getCell('B27')->setValue($objRichSig1);
    
    
    $objRichName1 = new PHPExcel_RichText();

$objName1 = $objRichName1->createTextRun($name); 
$objName1->getFont()->setBold(true); 
$objWorksheet->getCell('A7')->setValue($objRichName1);

   //$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);
  
  
$objWorksheet->fromArray($dataArray, NULL, 'B7'); 

$uid = $row['UserID'];
$countRows=0;   

}
else 
{

 
    $objRichText = new PHPExcel_RichText();
    
$objBolded = $objRichText->createTextRun('Department Access Confirmation Form');
$objBolded->getFont()->setBold(true); 
$objWorksheet->getCell('A1')->setValue($objRichText);
$objWorksheet->mergeCells('A1:D1');
$objWorksheet->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);


    $objRichMsg = new PHPExcel_RichText();

$objmsg = $objRichMsg->createTextRun('Stated below is your current list of department/s that you have access to via the system.  Please review and confirm if department access is accurate.  If a department needs to be added and/or removed, please note in the comment section.  Please sign and date indicating your agreement after changes are made.'); 
$objmsg->getFont()->setBold(false);
$objWorksheet->getStyle('A3')->getAlignment()->setWrapText(true);

$objWorksheet->getCell('A3')->setValue($objRichMsg);
$objWorksheet->getColumnDimension('B')->setWidth(76);
$objWorksheet->getColumnDimension('A')->setWidth(19);
$objWorksheet->getRowDimension(3)->setRowHeight(58.50);
$objWorksheet->mergeCells('A3:C3');

    $objRichUser = new PHPExcel_RichText();

$objUser = $objRichUser->createTextRun('User:'); 
$objUser->getFont()->setBold(true); 
$objWorksheet->getCell('A5')->setValue($objRichUser);


    $objRichDept = new PHPExcel_RichText();

$objDept = $objRichDept->createTextRun('Department Access:'); 
$objDept->getFont()->setBold(true); 
$objWorksheet->getCell('B5')->setValue($objRichDept);

    
    $objRichCom = new PHPExcel_RichText();

$objCom = $objRichCom->createTextRun('Comments:'); 
$objCom->getFont()->setBold(true); 
$objWorksheet->getCell('A20')->setValue($objRichCom);
    

    $objRichSig = new PHPExcel_RichText();

$objSig = $objRichSig->createTextRun('Signature:_____________________________________________  Date:___________'); 
$objSig->getFont()->setBold(true); 
$objWorksheet->getCell('B27')->setValue($objRichSig);
    
    
    $objRichName = new PHPExcel_RichText();

$objName = $objRichName->createTextRun($name); 
$objName->getFont()->setBold(true); 
$objWorksheet->getCell('A7')->setValue($objRichName);

    //$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);
   
    
   
$objWorksheet->fromArray($dataArray, NULL, 'B7'); 
$uid = $row['UserID'];

    } 

}
$objPHPExcel->removeSheetByIndex(0);

 
// 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);  ?>

Viewing all articles
Browse latest Browse all 2707

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>