使用PHPExcel加载Excel文件

时间:2013-09-13 14:42:20

标签: php phpexcel

我有以下代码:

public function actionSearch()
{
    $inputFileName = "C:/Apache2/htdocs/filePurchase/files/file.xlsx";
    $objReader = new PHPExcel_Reader_Excel2007();
    $objPHPExcel = $objReader->load($inputFileName);
}

当我在浏览器上运行此操作时,会显示以下消息:

  

连接已重置

     

在页面加载时重置了与服务器的连接。

使用以下代码是没有问题的:

$inputFileName = "C:/Apache2/htdocs/filePurchase/files/file.xlsx";
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);

有什么问题?


修改

的var_dump($ objReader):

object(PHPExcel_Reader_Excel2007)[19]
private '_referenceHelper' => 
object(PHPExcel_ReferenceHelper)[21]
protected '_readDataOnly' => boolean false
protected '_includeCharts' => boolean false
protected '_loadSheetsOnly' => null
protected '_readFilter' => 
object(PHPExcel_Reader_DefaultReadFilter)[20]
protected '_fileHandle' => null

Apache日志:

[Fri Sep 13 19:46:50 2013] [notice] Parent: child process exited with status 255 -- Restarting.
httpd.exe: Could not reliably determine the server's fully qualified domain name, using ::1 for ServerName
[Fri Sep 13 19:46:51 2013] [notice] Apache/2.2.23 (Win32) PHP/5.3.3 configured -- resuming normal operations
[Fri Sep 13 19:46:51 2013] [notice] Server built: Aug 24 2012 11:45:16
[Fri Sep 13 19:46:51 2013] [notice] Parent: Created child process 1184
httpd.exe: Could not reliably determine the server's fully qualified domain name, using ::1 for ServerName
httpd.exe: Could not reliably determine the server's fully qualified domain name, using ::1 for ServerName
[Fri Sep 13 19:46:52 2013] [notice] Child 1184: Child process is running
[Fri Sep 13 19:46:52 2013] [notice] Child 1184: Acquired the start mutex.
[Fri Sep 13 19:46:52 2013] [notice] Child 1184: Starting 64 worker threads.
[Fri Sep 13 19:46:52 2013] [notice] Child 1184: Starting thread to listen on port 80.
[Fri Sep 13 19:46:52 2013] [notice] Child 1184: Starting thread to listen on port 80.
[Fri Sep 13 19:46:53 2013] [notice] Parent: child process exited with status 255 -- Restarting.
httpd.exe: Could not reliably determine the server's fully qualified domain name, using ::1 for ServerName
[Fri Sep 13 19:46:54 2013] [notice] Apache/2.2.23 (Win32) PHP/5.3.3 configured -- resuming normal operations
[Fri Sep 13 19:46:54 2013] [notice] Server built: Aug 24 2012 11:45:16
[Fri Sep 13 19:46:54 2013] [notice] Parent: Created child process 4744
httpd.exe: Could not reliably determine the server's fully qualified domain name, using ::1 for ServerName
httpd.exe: Could not reliably determine the server's fully qualified domain name, using ::1 for ServerName
[Fri Sep 13 19:46:55 2013] [notice] Child 4744: Child process is running
[Fri Sep 13 19:46:55 2013] [notice] Child 4744: Acquired the start mutex.
[Fri Sep 13 19:46:55 2013] [notice] Child 4744: Starting 64 worker threads.
[Fri Sep 13 19:46:55 2013] [notice] Child 4744: Starting thread to listen on port 80.
[Fri Sep 13 19:46:55 2013] [notice] Child 4744: Starting thread to listen on port 80.

编辑2: 在下面的代码中,var_dump($objPHPExcel);结果就像闪回一样。但是在exit;之后添加var_dump($objPHPExcel);可以看到结果:

public function actionSearch()
{
    $inputFileName = "C:/Apache2/htdocs/filePurchase/files/file.xlsx";
    $objReader = new PHPExcel_Reader_Excel2007();
    //var_dump($objReader);
    $objPHPExcel = $objReader->load($inputFileName);
    var_dump($objPHPExcel); exit;
}

var_dump($objPHPExcel);结果:

object(PHPExcel)[23]
  private '_uniqueID' => string '5233fcc54c677' (length=13)
  private '_properties' => 
    object(PHPExcel_DocumentProperties)[39]
      private '_creator' => string 'TMP' (length=3)
      private '_lastModifiedBy' => string 'TMP' (length=3)
      private '_created' => int 1378454286
      private '_modified' => int 1379082907
      private '_title' => string '' (length=0)
      private '_description' => string '' (length=0)
      private '_subject' => string '' (length=0)
      private '_keywords' => string '' (length=0)
      private '_category' => string '' (length=0)
      private '_manager' => string '' (length=0)
      private '_company' => string 'Microsoft Corporation' (length=21)
      private '_customProperties' => 
        array
          empty
  private '_security' => 
    object(PHPExcel_DocumentSecurity)[40]
      private '_lockRevision' => boolean false
      private '_lockStructure' => boolean false
      private '_lockWindows' => boolean false
      private '_revisionsPassword' => string '' (length=0)
      private '_workbookPassword' => string '' (length=0)
  private '_workSheetCollection' => 
    array
      0 => 
        object(PHPExcel_Worksheet)[293]
          private '_parent' => 
            &object(PHPExcel)[23]
          private '_cellCollection' => 
            object(PHPExcel_CachedObjectStorage_Memory)[292]
              ...
          private '_rowDimensions' => 
            array
              ...
          private '_defaultRowDimension' => 
            object(PHPExcel_Worksheet_RowDimension)[302]
              ...
          private '_columnDimensions' => 
            array
              ...
          private '_defaultColumnDimension' => 
            object(PHPExcel_Worksheet_ColumnDimension)[303]
              ...
          private '_drawingCollection' => 
            object(ArrayObject)[299]
              ...
          private '_chartCollection' => 
            object(ArrayObject)[300]
              ...
          private '_title' => string 'Sheet1' (length=6)
          private '_sheetState' => string 'visible' (length=7)
          private '_pageSetup' => 
            object(PHPExcel_Worksheet_PageSetup)[295]
              ...
          private '_pageMargins' => 
            object(PHPExcel_Worksheet_PageMargins)[296]
              ...
          private '_headerFooter' => 
            object(PHPExcel_Worksheet_HeaderFooter)[297]
              ...
          private '_sheetView' => 
            object(PHPExcel_Worksheet_SheetView)[298]
              ...
          private '_protection' => 
            object(PHPExcel_Worksheet_Protection)[301]
              ...
          private '_styles' => 
            array
              ...
          private '_conditionalStylesCollection' => 
            array
              ...
          private '_cellCollectionIsSorted' => boolean false
          private '_breaks' => 
            array
              ...
          private '_mergeCells' => 
            array
              ...
          private '_protectedCells' => 
            array
              ...
          private '_autoFilter' => 
            object(PHPExcel_Worksheet_AutoFilter)[304]
              ...
          private '_freezePane' => string '' (length=0)
          private '_showGridlines' => boolean true
          private '_printGridlines' => boolean false
          private '_showRowColHeaders' => boolean true
          private '_showSummaryBelow' => boolean true
          private '_showSummaryRight' => boolean true
          private '_comments' => 
            array
              ...
          private '_activeCell' => string 'B4' (length=2)
          private '_selectedCells' => string 'B4' (length=2)
          private '_cachedHighestColumn' => string 'E' (length=1)
          private '_cachedHighestRow' => string '896' (length=3)
          private '_rightToLeft' => boolean false
          private '_hyperlinkCollection' => 
            array
              ...
          private '_dataValidationCollection' => 
            array
              ...
          private '_tabColor' => null
          private '_dirty' => boolean true
          private '_hash' => null
      1 => 
        object(PHPExcel_Worksheet)[291]
          private '_parent' => 
            &object(PHPExcel)[23]
          private '_cellCollection' => 
            object(PHPExcel_CachedObjectStorage_Memory)[3906]
              ...
          private '_rowDimensions' => 
            array
              ...
          private '_defaultRowDimension' => 
            object(PHPExcel_Worksheet_RowDimension)[3909]
              ...
          private '_columnDimensions' => 
            array
              ...
          private '_defaultColumnDimension' => 
            object(PHPExcel_Worksheet_ColumnDimension)[3910]
              ...
          private '_drawingCollection' => 
            object(ArrayObject)[1209]
              ...
          private '_chartCollection' => 
            object(ArrayObject)[3907]
              ...
          private '_title' => string 'Sheet2' (length=6)
          private '_sheetState' => string 'visible' (length=7)
          private '_pageSetup' => 
            object(PHPExcel_Worksheet_PageSetup)[1206]
              ...
          private '_pageMargins' => 
            object(PHPExcel_Worksheet_PageMargins)[1207]
              ...
          private '_headerFooter' => 
            object(PHPExcel_Worksheet_HeaderFooter)[318]
              ...
          private '_sheetView' => 
            object(PHPExcel_Worksheet_SheetView)[3904]
              ...
          private '_protection' => 
            object(PHPExcel_Worksheet_Protection)[3908]
              ...
          private '_styles' => 
            array
              ...
          private '_conditionalStylesCollection' => 
            array
              ...
          private '_cellCollectionIsSorted' => boolean false
          private '_breaks' => 
            array
              ...
          private '_mergeCells' => 
            array
              ...
          private '_protectedCells' => 
            array
              ...
          private '_autoFilter' => 
            object(PHPExcel_Worksheet_AutoFilter)[3911]
              ...
          private '_freezePane' => string '' (length=0)
          private '_showGridlines' => boolean true
          private '_printGridlines' => boolean false
          private '_showRowColHeaders' => boolean true
          private '_showSummaryBelow' => boolean true
          private '_showSummaryRight' => boolean true
          private '_comments' => 
            array
              ...
          private '_activeCell' => string 'A1' (length=2)
          private '_selectedCells' => string 'A1' (length=2)
          private '_cachedHighestColumn' => string 'A' (length=1)
          private '_cachedHighestRow' => int 1
          private '_rightToLeft' => boolean false
          private '_hyperlinkCollection' => 
            array
              ...
          private '_dataValidationCollection' => 
            array
              ...
          private '_tabColor' => null
          private '_dirty' => boolean true
          private '_hash' => null
      2 => 
        object(PHPExcel_Worksheet)[3903]
          private '_parent' => 
            &object(PHPExcel)[23]
          private '_cellCollection' => 
            object(PHPExcel_CachedObjectStorage_Memory)[3913]
              ...
          private '_rowDimensions' => 
            array
              ...
          private '_defaultRowDimension' => 
            object(PHPExcel_Worksheet_RowDimension)[3921]
              ...
          private '_columnDimensions' => 
            array
              ...
          private '_defaultColumnDimension' => 
            object(PHPExcel_Worksheet_ColumnDimension)[3922]
              ...
          private '_drawingCollection' => 
            object(ArrayObject)[3918]
              ...
          private '_chartCollection' => 
            object(ArrayObject)[3919]
              ...
          private '_title' => string 'Sheet3' (length=6)
          private '_sheetState' => string 'visible' (length=7)
          private '_pageSetup' => 
            object(PHPExcel_Worksheet_PageSetup)[3915]
              ...
          private '_pageMargins' => 
            object(PHPExcel_Worksheet_PageMargins)[3916]
              ...
          private '_headerFooter' => 
            object(PHPExcel_Worksheet_HeaderFooter)[307]
              ...
          private '_sheetView' => 
            object(PHPExcel_Worksheet_SheetView)[3917]
              ...
          private '_protection' => 
            object(PHPExcel_Worksheet_Protection)[3920]
              ...
          private '_styles' => 
            array
              ...
          private '_conditionalStylesCollection' => 
            array
              ...
          private '_cellCollectionIsSorted' => boolean false
          private '_breaks' => 
            array
              ...
          private '_mergeCells' => 
            array
              ...
          private '_protectedCells' => 
            array
              ...
          private '_autoFilter' => 
            object(PHPExcel_Worksheet_AutoFilter)[3923]
              ...
          private '_freezePane' => string '' (length=0)
          private '_showGridlines' => boolean true
          private '_printGridlines' => boolean false
          private '_showRowColHeaders' => boolean true
          private '_showSummaryBelow' => boolean true
          private '_showSummaryRight' => boolean true
          private '_comments' => 
            array
              ...
          private '_activeCell' => string 'A1' (length=2)
          private '_selectedCells' => string 'A1' (length=2)
          private '_cachedHighestColumn' => string 'A' (length=1)
          private '_cachedHighestRow' => int 1
          private '_rightToLeft' => boolean false
          private '_hyperlinkCollection' => 
            array
              ...
          private '_dataValidationCollection' => 
            array
              ...
          private '_tabColor' => null
          private '_dirty' => boolean true
          private '_hash' => null
  private '_calculationEngine' => 
    object(PHPExcel_Calculation)[24]
      private '_workbook' => 
        &object(PHPExcel)[23]
      private '_calculationCache' => 
        array
          empty
      private '_calculationCacheEnabled' => boolean true
      private 'debugLog' => null
      public 'suppressFormulaErrors' => boolean false
      public 'formulaError' => null
      private '_cyclicReferenceStack' => 
        object(PHPExcel_CalcEngine_CyclicReferenceStack)[25]
          private '_stack' => 
            array
              ...
      private '_cyclicFormulaCount' => int 0
      private '_cyclicFormulaCell' => string '' (length=0)
      public 'cyclicFormulaCount' => int 0
      private '_savedPrecision' => string '14' (length=2)
      public '_debugLog' => 
        object(PHPExcel_CalcEngine_Logger)[26]
          private '_writeDebugLog' => boolean false
          private '_echoDebugLog' => boolean false
          private '_debugLog' => 
            array
              ...
          private '_cellStack' => 
            object(PHPExcel_CalcEngine_CyclicReferenceStack)[25]
              ...
  private '_activeSheetIndex' => int 0
  private '_namedRanges' => 
    array
      empty
  private '_cellXfSupervisor' => 
    object(PHPExcel_Style)[41]
      protected '_font' => 
        object(PHPExcel_Style_Font)[42]
          protected '_name' => string 'Calibri' (length=7)
          protected '_size' => int 11
          protected '_bold' => boolean false
          protected '_italic' => boolean false
          protected '_superScript' => boolean false
          protected '_subScript' => boolean false
          protected '_underline' => string 'none' (length=4)
          protected '_strikethrough' => boolean false
          protected '_color' => 
            object(PHPExcel_Style_Color)[43]
              ...
          protected '_isSupervisor' => boolean true
          protected '_parent' => 
            &object(PHPExcel_Style)[41]
      protected '_fill' => 
        object(PHPExcel_Style_Fill)[44]
          protected '_fillType' => string 'none' (length=4)
          protected '_rotation' => int 0
          protected '_startColor' => 
            object(PHPExcel_Style_Color)[45]
              ...
          protected '_endColor' => 
            object(PHPExcel_Style_Color)[46]
              ...
          protected '_isSupervisor' => boolean true
          protected '_parent' => 
            &object(PHPExcel_Style)[41]
      protected '_borders' => 
        object(PHPExcel_Style_Borders)[47]
          protected '_left' => 
            object(PHPExcel_Style_Border)[48]
              ...
          protected '_right' => 
            object(PHPExcel_Style_Border)[50]
              ...
          protected '_top' => 
            object(PHPExcel_Style_Border)[52]
              ...
          protected '_bottom' => 
            object(PHPExcel_Style_Border)[54]
              ...
          protected '_diagonal' => 
            object(PHPExcel_Style_Border)[56]
              ...
          protected '_diagonalDirection' => int 0
          protected '_allBorders' => 
            object(PHPExcel_Style_Border)[58]
              ...
          protected '_outline' => 
            object(PHPExcel_Style_Border)[60]
              ...
          protected '_inside' => 
            object(PHPExcel_Style_Border)[62]
              ...
          protected '_vertical' => 
            object(PHPExcel_Style_Border)[64]
              ...
          protected '_horizontal' => 
            object(PHPExcel_Style_Border)[66]
              ...
          protected '_isSupervisor' => boolean true
          protected '_parent' => 
            &object(PHPExcel_Style)[41]
      protected '_alignment' => 
        object(PHPExcel_Style_Alignment)[68]
          protected '_horizontal' => string 'general' (length=7)
          protected '_vertical' => string 'bottom' (length=6)
          protected '_textRotation' => int 0
          protected '_wrapText' => boolean false
          protected '_shrinkToFit' => boolean false
          protected '_indent' => int 0
          protected '_isSupervisor' => boolean true
          protected '_parent' => 
            &object(PHPExcel_Style)[41]
      protected '_numberFormat' => 
        object(PHPExcel_Style_NumberFormat)[69]
          protected '_formatCode' => string 'General' (length=7)
          protected '_builtInFormatCode' => int 0
          protected '_isSupervisor' => boolean true
          protected '_parent' => 
            &object(PHPExcel_Style)[41]
      protected '_conditionalStyles' => 
        array
          empty
      protected '_protection' => 
        object(PHPExcel_Style_Protection)[70]
          protected '_locked' => string 'inherit' (length=7)
          protected '_hidden' => string 'inherit' (length=7)
          protected '_isSupervisor' => boolean true
          protected '_parent' => 
            &object(PHPExcel_Style)[41]
      protected '_index' => null
      protected '_isSupervisor' => boolean true
      protected '_parent' => 
        &object(PHPExcel)[23]
  private '_cellXfCollection' => 
    array
      0 => 
        object(PHPExcel_Style)[117]
          protected '_font' => 
            object(PHPExcel_Style_Font)[118]
              ...
          protected '_fill' => 
            object(PHPExcel_Style_Fill)[120]
              ...
          protected '_borders' => 
            object(PHPExcel_Style_Borders)[123]
              ...
          protected '_alignment' => 
            object(PHPExcel_Style_Alignment)[134]
              ...
          protected '_numberFormat' => 
            object(PHPExcel_Style_NumberFormat)[135]
              ...
          protected '_conditionalStyles' => 
            array
              ...
          protected '_protection' => 
            object(PHPExcel_Style_Protection)[136]
              ...
          protected '_index' => int 0
          protected '_isSupervisor' => boolean false
          protected '_parent' => null
      1 => 
        object(PHPExcel_Style)[143]
          protected '_font' => 
            object(PHPExcel_Style_Font)[144]
              ...
          protected '_fill' => 
            object(PHPExcel_Style_Fill)[146]
              ...
          protected '_borders' => 
            object(PHPExcel_Style_Borders)[149]
              ...
          protected '_alignment' => 
            object(PHPExcel_Style_Alignment)[160]
              ...
          protected '_numberFormat' => 
            object(PHPExcel_Style_NumberFormat)[161]
              ...
          protected '_conditionalStyles' => 
            array
              ...
          protected '_protection' => 
            object(PHPExcel_Style_Protection)[162]
              ...
          protected '_index' => int 1
          protected '_isSupervisor' => boolean false
          protected '_parent' => null
      2 => 
        object(PHPExcel_Style)[170]
          protected '_font' => 
            object(PHPExcel_Style_Font)[171]
              ...
          protected '_fill' => 
            object(PHPExcel_Style_Fill)[173]
              ...
          protected '_borders' => 
            object(PHPExcel_Style_Borders)[176]
              ...
          protected '_alignment' => 
            object(PHPExcel_Style_Alignment)[187]
              ...
          protected '_numberFormat' => 
            object(PHPExcel_Style_NumberFormat)[188]
              ...
          protected '_conditionalStyles' => 
            array
              ...
          protected '_protection' => 
            object(PHPExcel_Style_Protection)[189]
              ...
          protected '_index' => int 2
          protected '_isSupervisor' => boolean false
          protected '_parent' => null
      3 => 
        object(PHPExcel_Style)[197]
          protected '_font' => 
            object(PHPExcel_Style_Font)[198]
              ...
          protected '_fill' => 
            object(PHPExcel_Style_Fill)[200]
              ...
          protected '_borders' => 
            object(PHPExcel_Style_Borders)[203]
              ...
          protected '_alignment' => 
            object(PHPExcel_Style_Alignment)[214]
              ...
          protected '_numberFormat' => 
            object(PHPExcel_Style_NumberFormat)[215]
              ...
          protected '_conditionalStyles' => 
            array
              ...
          protected '_protection' => 
            object(PHPExcel_Style_Protection)[216]
              ...
          protected '_index' => int 3
          protected '_isSupervisor' => boolean false
          protected '_parent' => null
      4 => 
        object(PHPExcel_Style)[224]
          protected '_font' => 
            object(PHPExcel_Style_Font)[225]
              ...
          protected '_fill' => 
            object(PHPExcel_Style_Fill)[227]
              ...
          protected '_borders' => 
            object(PHPExcel_Style_Borders)[230]
              ...
          protected '_alignment' => 
            object(PHPExcel_Style_Alignment)[241]
              ...
          protected '_numberFormat' => 
            object(PHPExcel_Style_NumberFormat)[242]
              ...
          protected '_conditionalStyles' => 
            array
              ...
          protected '_protection' => 
            object(PHPExcel_Style_Protection)[243]
              ...
          protected '_index' => int 4
          protected '_isSupervisor' => boolean false
          protected '_parent' => null
  private '_cellStyleXfCollection' => 
    array
      0 => 
        object(PHPExcel_Style)[250]
          protected '_font' => 
            object(PHPExcel_Style_Font)[251]
              ...
          protected '_fill' => 
            object(PHPExcel_Style_Fill)[253]
              ...
          protected '_borders' => 
            object(PHPExcel_Style_Borders)[256]
              ...
          protected '_alignment' => 
            object(PHPExcel_Style_Alignment)[267]
              ...
          protected '_numberFormat' => 
            object(PHPExcel_Style_NumberFormat)[268]
              ...
          protected '_conditionalStyles' => 
            array
              ...
          protected '_protection' => 
            object(PHPExcel_Style_Protection)[269]
              ...
          protected '_index' => int 0
          protected '_isSupervisor' => boolean false
          protected '_parent' => null

2 个答案:

答案 0 :(得分:1)

我有类似的问题(代码读取excel 2007并写入MySQL),除非xlsx保持最小数量的列,例如9和2行,没有问题。如果我将列数增加到10或更多,它就会出现连接重置错误,即使mysql写入成功。 apache日志中没有显示任何内容。

System Linux hubble.dal.design.ti.com 2.6.9-89.0.16.ELxenU#1 SMP Tue Oct 27 04:12:25 EDT 2009 x86_64

配置命令'./configure'' - prefix = / web / server / pkgs / php-5.2.3'' - with-config-file-path = / web / conf / etc'' - with -mysql = / web / server / pkgs / mysql-5.0.33'' - with-apxs2 = / web / server / pkgs / apache-2.2.3 / bin / apxs'' - with-libxml-dir = / web / server / pkgs / libxml2-2.6.27'' - with-db4 = / web / server / pkgs / db-4.4.20'' - with-readline = / web / server / pkgs / readline-5.1' '--with-openssl = / web / server / pkgs / openssl-0.9.8g'' - with-oci8 = instantclient,/ web / server / pkgs / instantclient_10_2'' - with-gd = / web / server / pkgs / gd-2.0.33'' - with-xsl = / web / server / pkgs / libxslt-1.1.20'' - with-tidy = / web / server / pkgs / tidy-20070306'' - with -ldap = / web / server / pkgs / openldap-2.3.20'' - with-curl = / web / server / pkgs / curl-7.18.0'' - with-zlib-dir = / web / server / pkgs / zlib-1.2.3'' - with-mcrypt = / web / server / pkgs / libmcrypt-2.5.8''enable-mbstring'' - with-mysqli = / web / server / bin / mysql_config '' - get-gettext = / web / server / pkgs / gettext-0.17'' - with-mssql = / web / server / pkgs / freetds-0.82'' - with-png-dir = / web / server /pkgs/libpng-1.2.8'' - enable-zip' '--with-freetype-dir = / web / server / pkgs / freetype-2.4.0''enable-gd-native-ttf'' - enable-gd-imgstrttf'

答案 1 :(得分:1)

我遇到了同样的问题,但使用的是小型Excel文件。增加内存限制和执行时间没有帮助。最后我决定检查Excel文件,我发现问题出在文件中。这些文件是从第三方的电子邮件中下载的,所以我不知道它们是如何创建的。当我用MS Excel(2007)打开每个文件时,程序给出了一个"受保护的视图"警告并询问是否“启用编辑”。我启用了文件进行编辑,在某些情况下它有所帮助,因此我能够毫无问题地运行PHPExcel脚本。在另一种情况下,有必要使用MS Excel(2007)重新保存Excel文件,并且“重置连接”问题消失。 为了检查内存是否是问题,我将Excel文件的容量增加了3倍,并且PHPExcel脚本仍然运行良好。我花了一个星期的时间才意识到问题出在哪里,所以我希望它能帮到别人。