在PHP中读取Excel文件

Din*_*nah 94 php import-from-excel

我正在尝试读取Excel文件(Office 2003).有一个Excel文件需要上传并解析其内容.

通过谷歌,我只能找到这些相关(和不充分的主题)的答案:生成Excel文件,阅读Excel XML文件,阅读Excel CSV文件或不完整的废弃项目.我拥有Office 2003,所以如果我需要任何文件,它们都可用.它安装在我的盒子上,但不能也不能安装在我的共享主机上.

编辑:到目前为止,所有答案都指向PHP-ExcelReader和/或关于如何使用它的这篇附加文章.

Ion*_*tan 59

据我所知,你有两个选择:

  1. Spreadsheet_Excel_Reader,它知道Office 2003二进制格式
  2. PHPExcel,它既了解Office 2003,也了解Excel 2007(XML).(按照链接,你会看到他们将这个库升级到PHPSpreadSheet)

PHPExcel使用Spreadsheet_Excel_Reader作为Office 2003格式.

更新:我曾经阅读过一些Excel文件,但我使用Office 2003 XML格式来阅读它们并告诉使用该应用程序的人保存并仅上传该类型的Excel文件.

  • PHPExcel始终能够读取和编写一系列电子表格格式.从主页的第一个非标题行引用:"为PHP编程语言提供一组类的项目,允许您__写入___和__read从___不同的文件格式,如Excel 2007,PDF,HTML"(我的重点) ) (4认同)

Lui*_*tti 48

我使用PHP-ExcelReader来读取xls文件,效果很好.

  • 它不适合我,我有PHP 5.2+,给我错误"通过引用分配新的返回值已被弃用" (6认同)
  • 嘿伙计们,我解决了一些问题,使它与最近的PHP 5版本兼容:http://pastebin.com/YNUZANcs所有学分都归原始开发者所有. (6认同)
  • 据我了解,PHP-ExcelReader是一个可以处理.xls文件的类,但不能处理.xlsx文件.PHPExcel可以处理许多不同的格式(包括.xls和.xlsx)(https://github.com/PHPOffice/Phpexcel)并且处于稳定版本.PHPSpreadsheet是PHPExcel的进一步开发,但不稳定(因为我写的是这样):https://github.com/PHPOffice/PhpSpreadsheet (4认同)

小智 19

这取决于您希望如何使用excel文件中的数据.如果要将其导入mysql,只需将其保存为CSV格式的文件,然后使用fgetcsv进行解析.


Ser*_*kin 12

阅读XLSX(Excel 97-2003)
https://github.com/shuchkin/simplexls

if ( $xls = SimpleXLS::parse('book.xls') ) {
    print_r( $xls->rows() );
} else {
    echo SimpleXLS::parseError();
}
Run Code Online (Sandbox Code Playgroud)

阅读XLSX(Excel 2003+)
https://github.com/shuchkin/simplexlsx

if ( $xlsx = SimpleXLSX::parse('book.xlsx') ) {
    print_r( $xlsx->rows() );
} else {
    echo SimpleXLSX::parseError();
}
Run Code Online (Sandbox Code Playgroud)

输出量

数组(
    [0] =>数组
        (
            [0] => ISBN
            [1] =>标题
            [2] =>作者
            [3] =>发布者
            [4] => ctry
        )
    [1] =>数组
        (
            [0] => 618260307
            [1] =>霍比特人
            [2] => JRR托尔金
            [3] =>霍顿·米夫林
            [4] =>美国
       )

)

CSV php阅读器
https://github.com/shuchkin/simplecsv


小智 5

// Here is the simple code using COM object in PHP
class Excel_ReadWrite{

    private $XLSHandle;
    private $WrkBksHandle;
    private $xlBook;

    function __construct() {
        $this->XLSHandle = new COM("excel.application") or die("ERROR: Unable to instantaniate COM!\r\n"); 
    }

    function __destruct(){
        //if already existing file is opened
        if($this->WrkBksHandle != null)
        {   
            $this->WrkBksHandle->Close(True);
            unset($this->WrkBksHandle);
            $this->XLSHandle->Workbooks->Close();
        }
        //if created new xls file
        if($this->xlBook != null)
        {
            $this->xlBook->Close(True);
            unset($this->xlBook);
        }
        //Quit Excel Application
        $this->XLSHandle->Quit();
        unset($this->XLSHandle);
    }

    public function OpenFile($FilePath)
    {
        $this->WrkBksHandle = $this->XLSHandle->Workbooks->Open($FilePath);
    }

    public function ReadData($RowNo, $ClmNo)
    {
       $Value = $this->XLSHandle->ActiveSheet->Cells($RowNo, $ClmNo)->Value;
       return $Value;
    }  

    public function SaveOpenedFile()
    {
        $this->WrkBksHandle->Save(); 
    }  

    /***********************************************************************************
    * Function Name:- WriteToXlsFile() will write data based on row and column numbers
    * @Param:- $CellData- cell data
    * @Param:- $RowNumber- xlsx file row number
    * @Param:- $ColumnNumber- xlsx file column numbers
   ************************************************************************************/
   function WriteToXlsFile($CellData, $RowNumber, $ColumnNumber)
   {
       try{
               $this->XLSHandle->ActiveSheet->Cells($RowNumber,$ColumnNumber)->Value = $CellData;
           }
       catch(Exception $e){
               throw new Exception("Error:- Unable to write data to xlsx sheet");
           }
   }


   /****************************************************************************************
    * Function Name:- CreateXlsFileWithClmName() will initialize xls file with column Names
    * @Param:- $XlsColumnNames- Array of columns data
    * @Param:- $XlsColumnWidth- Array of columns width
   *******************************************************************************************/
   function CreateXlsFileWithClmNameAndWidth($WorkSheetName = "Raman", $XlsColumnNames = null, $XlsColumnWidth = null)
   {
       //Hide MS Excel application window
       $this->XLSHandle->Visible = 0;
       //Create new document
       $this->xlBook = $this->XLSHandle->Workbooks->Add();

       //Create Sheet 1
       $this->xlBook->Worksheets(1)->Name = $WorkSheetName;
       $this->xlBook->Worksheets(1)->Select;

       if($XlsColumnWidth != null)
       {
           //$XlsColumnWidth = array("A1"=>15,"B1"=>20);
           foreach($XlsColumnWidth as $Clm=>$Width)
           {
               //Set Columns Width
               $this->XLSHandle->ActiveSheet->Range($Clm.":".$Clm)->ColumnWidth = $Width;
           }    
       }
       if($XlsColumnNames != null)
       {
           //$XlsColumnNames = array("FirstColumnName"=>1, "SecondColumnName"=>2);
           foreach($XlsColumnNames as $ClmName=>$ClmNumber)
           {
               // Cells(Row,Column)
               $this->XLSHandle->ActiveSheet->Cells(1,$ClmNumber)->Value = $ClmName;
               $this->XLSHandle->ActiveSheet->Cells(1,$ClmNumber)->Font->Bold = True;
               $this->XLSHandle->ActiveSheet->Cells(1,$ClmNumber)->Interior->ColorIndex = "15";
           }
       }
   }
   //56 is for xls 8
    public function SaveCreatedFile($FileName, $FileFormat = 56)
    {
        $this->xlBook->SaveAs($FileName, $FileFormat);
    }

    public function MakeFileVisible()
    {
       //Hide MS Excel application window`enter code here`
       $this->XLSHandle->Visible = 1;
    }
}//end of EXCEL class
Run Code Online (Sandbox Code Playgroud)

  • 如果您的 PHP 服务器在 Windows 上运行并且安装了 Excel,这看起来不错。 (3认同)