douyong4623 2017-05-19 15:30
浏览 65
已采纳

如何使用PHP Excel创建可读的Excel文件

I have been using PHPExcel to created a spreadsheet from a csv file. I have been able to get that working perfectly with the exception that the Excel files are marked Read-Only. I have browsed the GitHub issues for the Class and I have run a search on this board and can not find anyone speaking to this issue.

Can PHPExcel write an Excel file that is not marked Read-Only? Here is my code. As you can see I have tried marking all of the sheet protection and lock options to false.

<?php /** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('display_startup_errors', true);
define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
date_default_timezone_set('Europe/London');
echo "<h1>FINDLAY FOODS Product List CSV Import to Excel</h1><br /><br />";
/** Include PHPExcel */
require_once dirname(__file__) . '/../Classes/PHPExcel.php';

// Create new PHPExcel object
echo date('H:i:s'), " Create new PHPExcel object", EOL;
$objPHPExcel = new PHPExcel();
// Set document properties
echo date('H:i:s'), " Set properties", EOL;
$objPHPExcel->getProperties()->setCreator("Findlay Foods")->setLastModifiedBy("Findlay Foods")->setTitle("Prod_List_Test_1")->
    setSubject("PRod List Test")->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")->
    setKeywords("office 2007 openxml php")->setCategory("Test result file");
echo date('H:i:s'), " Add data", EOL;
$objPHPExcel->setActiveSheetIndex(0);

$new_array = array();
$row = 1;

$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Code');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'True');
$objPHPExcel->getActiveSheet()->setCellValue('C1', 'Rebate');
$objPHPExcel->getActiveSheet()->setCellValue('D1', 'Break');
$objPHPExcel->getActiveSheet()->setCellValue('E1', 'Portion');
$objPHPExcel->getActiveSheet()->setCellValue('F1', 'Unit');
if (($handle = fopen("prodlist.csv", "r")) !== false)
{
    while (($data = fgetcsv($handle, 1000, ",")) !== false)
    {
        $row++;
        for ($c = 0; $c < 1; $c++)
        {
            $fcode = trim(str_pad($data[0], 5, "0", STR_PAD_LEFT));
            $fcode = (float)$fcode;
            $true = trim($data[1]);
            $true = (float)$true;
            $true = number_format($true, 2, '.', '');
            $rebate = trim($data[2]);
            $bf = trim($data[3]);
            $str = trim($data[4]);
            $portion = filter_var($str, FILTER_SANITIZE_NUMBER_INT);
            $size = strtolower(preg_replace("/[0-9]/", "", $str));
            $size = strtolower(preg_replace("/\./", "", $size));
            $objPHPExcel->getActiveSheet()->setCellValue('A' . $row, $fcode);
            $objPHPExcel->getActiveSheet()->getStyle('A' . $row)->getNumberFormat()->setFormatCode('0000#');
            $objPHPExcel->getActiveSheet()->setCellValue('B' . $row, $true);
            $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode('#,##0.00');
            $objPHPExcel->getActiveSheet()->setCellValue('C' . $row, $rebate);
            $objPHPExcel->getActiveSheet()->getStyle('C' . $row)->getNumberFormat()->setFormatCode('#,##0.00');
            $objPHPExcel->getActiveSheet()->setCellValue('D' . $row, $bf);
            $objPHPExcel->getActiveSheet()->setCellValue('E' . $row, $portion);
            $objPHPExcel->getActiveSheet()->setCellValue('F' . $row, $size);
        }
    }
    fclose($handle);
}
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->getProtection()->setSort(false);
$objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(false);
$objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(false);
$objPHPExcel->getSecurity()->setLockWindows(false);
$objPHPExcel->getSecurity()->setLockStructure(false);

echo date('H:i:s'), " Write to Excel5 format", EOL;
$callStartTime = microtime(true);

$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->setOffice2003Compatibility(true);
$objWriter->save(str_replace(__file__, '/../data/list/findlay-prodlist.xlsx', __file__));
$callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime;
echo date('H:i:s'), " File written to ", str_replace('.php', '.xls', pathinfo(__file__, PATHINFO_BASENAME)),
    EOL;
echo 'Call time to write Workbook was ', sprintf('%.4f', $callTime), " seconds", EOL;
// Echo memory usage
echo date('H:i:s'), ' Current memory usage: ', (memory_get_usage(true) / 1024 / 1024), " MB", EOL;`enter code here`
// Echo memory peak usage
echo date('H:i:s'), " Peak memory usage: ", (memory_get_peak_usage(true) / 1024 / 1024), " MB", EOL;
// Echo done
echo date('H:i:s'), " Done writing file", EOL;
echo 'File has been created in ', getcwd(), EOL; ?>
  • 写回答

1条回答 默认 最新

  • douren0558 2017-05-19 15:32
    关注

    Do "save as" after you open it.

    Edit: sorry, maybe I misunderstood the question. To expand on my answer, the protections etc. that you have addressed are 'inside' the file. The Read-Only status is set by the operating system. It is the file permissions that need to be dealt with. Wherever you make the file must be a writable directory for the user reading it. If downloaded through a browser, the same applies.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化