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.

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

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历