$excelObj->getActiveSheet()->getProtection()->setPassword('123456');
$excelObj->getActiveSheet()->getProtection()->setSheet(true);
$excelObj->getActiveSheet()->getProtection()->setSort(true);
$excelObj->getActiveSheet()->getProtection()->setInsertRows(true);
$excelObj->getActiveSheet()->getProtection()->setFormatCells(true);在网上找的这种加密的方式,生成的文件还是可以直接打开的做过的大神给个demo

解决方案 »

  1.   

    PHPExcel 提供了三种保护方式,文档中说
    4.6.26. Setting security on a spreadsheet
    Excel offers 3 levels of “protection”: document security, sheet security and cell security.
    - Document security allows you to set a password on a complete spreadsheet, allowing changes to be made only when that password is entered.
    - Worksheet security offers other security options: you can disallow inserting rows on a specific sheet, disallow sorting, …
    - Cell security offers the option to lock/unlock a cell as well as show/hide the internal formulaAn example on setting document security:
    $objPHPExcel->getSecurity()->setLockWindows(true);
    $objPHPExcel->getSecurity()->setLockStructure(true);
    $objPHPExcel->getSecurity()->setWorkbookPassword("PHPExcel");
    An example on setting worksheet security:
    $objPHPExcel->getActiveSheet()->getProtection()->setPassword('PHPExcel');
    $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
    $objPHPExcel->getActiveSheet()->getProtection()->setSort(true);
    $objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(true);
    $objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(true);
    An example on setting cell security:
    $objPHPExcel->getActiveSheet()->getStyle('B1')->getProtection()->setLocked(
    PHPExcel_Style_Protection::PROTECTION_UNPROTECTED
    );
    你可按需要使用其中一种或几种
      

  2.   

    我之前看过你09年给人回复的帖子是这两种方法,我试了试但是加不了密(也可能是我没有弄对),能给我写个demo吗???直接生产加密的文件就行,其余的都不用!!!向您学习了