PHPExcel创建“不可读的内容" [英] PHPExcel creates 'unreadable content'

查看:92
本文介绍了PHPExcel创建“不可读的内容"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试解决大约1000种不同方式的问题.欣赏是否有人可以发现问题.

I've tried to fix this problem about 1000 different ways. Appreciate if anyone else can spot the problem.

我有使用PHPExcel的代码,该代码可生成多个Excel工作表并将其保存到磁盘.

I have code using PHPExcel that generates multiple Excel sheets and saves them to disk.

使用MS Excel 2010从第二个文件开始打开所有文件时,出现错误"Excel在FILENAME中发现了不可读的内容.您要恢复此工作簿的内容."该文件已恢复",并且可以正常运行.在OPen Office中打开不会产生任何错误.

When opening everything from the second file onwards using MS Excel 2010, I get the error "Excel found unreadable content in FILENAME. Do you want to recover the contents of this workbook." The file is 'recovered' and works perfectly ok. Opening in OPen Office does not produce any errors.

这是代码的简化版本.

<?php
$filenames = array("filenames go here");

$sheet1 = PHPExcel_IOFactory::load($template1);
$writer1 = new PHPExcel_Writer_Excel2007($sheet1);
$writer1->save('somefilename.xlsx');
//This file opens without problem
$sheet1->disconnectWorksheets();
unset($sheet1);
unset($writer1);

$i = 0;
foreach($filenames as $file){
    $template = "template2.xlsx";
    $fileName = 'filename' . $i . ' .xlsx';
    $spreadsheet = PHPExcel_IOFactory::load($template);
    $objWriter = new PHPExcel_Writer_Excel2007($spreadsheet);
    $objWriter->save($fileName);
    $spreadsheet->disconnectWorksheets();
    unset($spreadsheet);
    //This file throws error when opened in Excel 2007+
    $i++;
 }
?>

我检查了以下内容:

  1. 没有明显的php错误消息,空格或多余的输入会破坏文件.
  2. 模板文件可以在Excel 2010中正常打开.
  3. 我尝试使用不同的PHPExcel writer方法,但是它们都产生相同的问题.

真正的代码会完成很多额外的工作,但是我已经通过注释掉它来负责.上面的代码似乎是唯一可以引入问题的地方.

The real code does a whole load of extra stuff, but I've checked that it is not responsible by commenting it out. The code above seems to be the only place where a problem could be introduced.

感谢收到任何建议.

====编辑==== 这是Excel产生的错误日志:

====EDITED==== This is the error log produced by Excel:

<?xml version="1.0" encoding="UTF-8" standalone="true"?>
-<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
     <logFileName>error049120_01.xml</logFileName>
     <summary>Errors were detected in file 'C:\Users\USERNAME\AppData\Local\Temp\FILENAME.xlsx'</summary>
     -<additionalInfo>
           <info>Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.</info>
     </additionalInfo>
</recoveryLog>

推荐答案

此错误与php.ini中的mbstring.func_overload配置有关. 您可以使用解决方法来避免此错误-只需在脚本开头添加即可:

This bug was related to configuration mbstring.func_overload in php.ini. You can use workaround to avoid this bug - just add at begin of your script:

if (function_exists('mb_internal_encoding')) {
    $oldEncoding=mb_internal_encoding();
    mb_internal_encoding('latin1');
}

,最后:

if (function_exists('mb_internal_encoding'))
    mb_internal_encoding($oldEncoding);

它对我有所帮助,所以,我相信它也会对您有所帮助.

It helped me, so, I believe it'll help you as well.

这篇关于PHPExcel创建“不可读的内容"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆