Hướng dẫn read excel file in php - đọc tệp excel bằng php

Thêm từ AMPERSAND ACADEMY

Ampersand Academy cung cấp đào tạo lớp học & trực tuyến cho các phân tích dữ liệu bằng SAS, R & Python, Khoa học dữ liệu sử dụng R & Python, Deep Learning, Ionic, & Tableau. Chúng tôi cũng cung cấp các hướng dẫn và plugin miễn phí. Viết cho chúng tôi. Gửi liên kết bài viết của bạn đến

Tôi đang cố đọc một tập tin Excel (Office 2003). Có một tệp Excel cần được tải lên và nội dung của nó được phân tích cú pháp.

Thông qua Google, tôi chỉ có thể tìm thấy câu trả lời cho các chủ đề liên quan này (và không đủ): Tạo các tệp Excel, đọc các tệp XML Excel, đọc các tệp CSV Excel hoặc các dự án bị bỏ rơi không đầy đủ. Tôi sở hữu Office 2003 vì vậy nếu tôi cần bất kỳ tệp nào từ đó, chúng có sẵn. Nó được cài đặt trên hộp của tôi nhưng không và không thể được cài đặt trên máy chủ được chia sẻ của tôi.

Chỉnh sửa: Cho đến nay tất cả các câu trả lời đều chỉ ra Php-ExcelReader và/hoặc bài viết bổ sung này về cách sử dụng nó. so far all answers point to PHP-ExcelReader and/or this additional article about how to use it.

KANGEL

251 Huy hiệu bạc8 Huy hiệu đồng1 silver badge8 bronze badges

hỏi ngày 19 tháng 2 năm 2009 lúc 1:53Feb 19, 2009 at 1:53

Hướng dẫn read excel file in php - đọc tệp excel bằng php

3

Bạn có 2 lựa chọn theo như tôi biết:

  1. Bảng tính_excel_reader, biết định dạng nhị phân văn phòng 2003
  2. PhpExcel, biết cả Office 2003 cũng như Excel 2007 (XML). (Theo liên kết và bạn sẽ thấy họ đã nâng cấp thư viện này lên PHPSPREADSHEET)

PHPExcel sử dụng bảng tính_excel_reader cho định dạng Office 2003.

Cập nhật: Tôi đã từng phải đọc một số tệp Excel nhưng tôi đã sử dụng định dạng XML Office 2003 để đọc chúng và nói với những người đang sử dụng ứng dụng để lưu và tải lên loại tệp Excel đó.

Hướng dẫn read excel file in php - đọc tệp excel bằng php

Đã trả lời ngày 19 tháng 2 năm 2009 lúc 1:57Feb 19, 2009 at 1:57

Ionuț G. Stanionuț G. StanIonuț G. Stan

Phim thương hiệu vàng 172K1818 gold badges187 silver badges199 bronze badges

3

Tôi sử dụng Php-ExcelReader để đọc các tệp XLS và hoạt động tuyệt vời.

User229044 ♦

227K40 Huy hiệu vàng326 Huy hiệu bạc334 Huy hiệu đồng40 gold badges326 silver badges334 bronze badges

Đã trả lời ngày 19 tháng 2 năm 2009 lúc 3:07Feb 19, 2009 at 3:07

Luis Melgrattiluis MelgrattiLuis Melgratti

11.7K2 Huy hiệu vàng29 Huy hiệu bạc32 Huy hiệu đồng2 gold badges29 silver badges32 bronze badges

7

Đọc XLSX (Excel 2003+) https://github.com/shuchkin/simplexlsx
https://github.com/shuchkin/simplexlsx

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

Đầu ra

Array (
    [0] => Array
        (
            [0] => ISBN
            [1] => title
            [2] => author
            [3] => publisher
            [4] => ctry
        )
    [1] => Array
        (
            [0] => 618260307
            [1] => The Hobbit
            [2] => J. R. R. Tolkien
            [3] => Houghton Mifflin
            [4] => USA
       )

)

Đọc XLS (Excel 97-2003) https://github.com/shuchkin/simplexls
https://github.com/shuchkin/simplexls

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

Người đọc CSV PHP https://github.com/shuchkin/simplecsv
https://github.com/shuchkin/simplecsv

if ( $csv = SimpleCSV::import('book.csv') ) {
    print_r( $csv );
}

Đã trả lời ngày 28 tháng 12 năm 2018 lúc 17:56Dec 28, 2018 at 17:56

Hướng dẫn read excel file in php - đọc tệp excel bằng php

1

Nó phụ thuộc vào cách bạn muốn sử dụng dữ liệu trong tệp Excel. Nếu bạn muốn nhập nó vào MySQL, bạn chỉ cần lưu nó dưới dạng tệp được định dạng CSV và sau đó sử dụng FGETCSV để phân tích nó.

Larry k

46.5K14 Huy hiệu vàng83 Huy hiệu bạc136 Huy hiệu đồng14 gold badges83 silver badges136 bronze badges

Đã trả lời ngày 21 tháng 4 năm 2013 lúc 21:09Apr 21, 2013 at 21:09

JimbojimboJimbo

4814 Huy hiệu bạc5 Huy hiệu Đồng4 silver badges5 bronze badges

// 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

Đã trả lời ngày 30 tháng 9 năm 2015 lúc 23:40Sep 30, 2015 at 23:40

VickyvickyVicky

871 Huy hiệu bạc5 Huy hiệu đồng1 silver badge5 bronze badges

1

Thử cái này...

Tôi đã sử dụng mã sau để đọc "XLS và XLSX"

    read('excel_file.xls');   // reads and stores the excel file data

    // Test to see the excel data stored in $sheets property
    echo '
';
    var_export($excel->sheets);

    echo '
'; or echo '
';
    print_r($excel->sheets);

    echo '
';

Reference:http://coursesweb.net/php-mysql/read-excel-file-data-php_pc

Đã trả lời ngày 4 tháng 7 năm 2015 lúc 11:42Jul 4, 2015 at 11:42

2

Tôi đang sử dụng URL tệp Excel bên dưới: https://github.com/inventorbala/sample-excel-files/blob/master/sample-excel-files.xlsx

Output:

Array
    (
        [0] => Array
            (
                [store_id] => 3716
                [employee_uid] => 664368
                [opus_id] => zh901j
                [item_description] => PRE ATT $75 PNLS 90EXP
                [opus_transaction_date] => 2019-10-18
                [opus_transaction_num] => X2MBV1DJKSLQW
                [opus_invoice_num] => O3716IN3409
                [customer_name] => BILL PHILLIPS
                [mobile_num] => 4052380136
                [opus_amount] => 75
                [rq4_amount] => 0
                [difference] => -75
                [ocomment] => Re-Upload: We need RQ4 transaction for October.  If you're unable to provide the October invoice, it will be counted as EPin shortage.
                [mark_delete] => 0
                [upload_date] => 2019-10-20
            )

        [1] => Array
            (
                [store_id] => 2710
                [employee_uid] => 75899
                [opus_id] => dc288t
                [item_description] => PRE ATT $50 PNLS 90EXP
                [opus_transaction_date] => 2019-10-18
                [opus_transaction_num] => XJ90419JKT9R9
                [opus_invoice_num] => M2710IN868
                [customer_name] => CALEB MENDEZ
                [mobile_num] => 6517672079
                [opus_amount] => 50
                [rq4_amount] => 0
                [difference] => -50
                [ocomment] => No Response.  Re-Upload
                [mark_delete] => 0
                [upload_date] => 2019-10-20
            )

        [2] => Array
            (
                [store_id] => 0136
                [employee_uid] => 70167
                [opus_id] => fv766x
                [item_description] => PRE ATT $50 PNLS 90EXP
                [opus_transaction_date] => 2019-10-18
                [opus_transaction_num] => XQ57316JKST1V
                [opus_invoice_num] => GONZABP25622
                [customer_name] => FAUSTINA CASTILLO
                [mobile_num] => 8302638628
                [opus_amount] => 100
                [rq4_amount] => 50
                [difference] => -50
                [ocomment] => Re-Upload: We have been charged in opus for $100. Provide RQ4 invoice number for remaining amount
                [mark_delete] => 0
                [upload_date] => 2019-10-20
            )

        [3] => Array
            (
                [store_id] => 3264
                [employee_uid] => 23723
                [opus_id] => aa297h
                [item_description] => PRE ATT $25 PNLS 90EXP
                [opus_transaction_date] => 2019-10-19
                [opus_transaction_num] => XR1181HJKW9MP
                [opus_invoice_num] => C3264IN1588
                [customer_name] => SOPHAT VANN
                [mobile_num] => 9494668372
                [opus_amount] => 70
                [rq4_amount] => 25
                [difference] => -45
                [ocomment] => No Response.  Re-Upload
                [mark_delete] => 0
                [upload_date] => 2019-10-20
            )

        [4] => Array
            (
                [store_id] => 4166
                [employee_uid] => 568494
                [opus_id] => ab7598
                [item_description] => PRE ATT $40 RTR
                [opus_transaction_date] => 2019-10-20
                [opus_transaction_num] => X8F58P3JL2RFU
                [opus_invoice_num] => I4166IN2481
                [customer_name] => KELLY MC GUIRE
                [mobile_num] => 6189468180
                [opus_amount] => 40
                [rq4_amount] => 0
                [difference] => -40
                [ocomment] => Re-Upload: The invoice number that you provided (I4166IN2481) belongs to September transaction.  We need RQ4 transaction for October.  If you're unable to provide the October invoice, it will be counted as EPin shortage.
                [mark_delete] => 0
                [upload_date] => 2019-10-21
            )

        [5] => Array
            (
                [store_id] => 4508
                [employee_uid] => 552502
                [opus_id] => ec850x
                [item_description] => $30 RTR
                [opus_transaction_date] => 2019-10-20
                [opus_transaction_num] => XPL7M1BJL1W5D
                [opus_invoice_num] => M4508IN6024
                [customer_name] => PREPAID CUSTOMER
                [mobile_num] => 6019109730
                [opus_amount] => 30
                [rq4_amount] => 0
                [difference] => -30
                [ocomment] => Re-Upload: The invoice number you provided (M4508IN7217) belongs to a different phone number.  We need RQ4 transaction for the phone number in question.  If you're unable to provide the RQ4 invoice for this transaction, it will be counted as EPin shortage.
                [mark_delete] => 0
                [upload_date] => 2019-10-21
            )

        [6] => Array
            (
                [store_id] => 3904
                [employee_uid] => 35818
                [opus_id] => tj539j
                [item_description] => PRE $45 PAYG PINLESS REFILL
                [opus_transaction_date] => 2019-10-20
                [opus_transaction_num] => XM1PZQSJL215F
                [opus_invoice_num] => N3904IN1410
                [customer_name] => DORTHY JONES
                [mobile_num] => 3365982631
                [opus_amount] => 90
                [rq4_amount] => 45
                [difference] => -45
                [ocomment] => Re-Upload: Please email the details to Treasury and confirm
                [mark_delete] => 0
                [upload_date] => 2019-10-21
            )

        [7] => Array
            (
                [store_id] => 1820
                [employee_uid] => 59883
                [opus_id] => cb9406
                [item_description] => PRE ATT $25 PNLS 90EXP
                [opus_transaction_date] => 2019-10-20
                [opus_transaction_num] => XTBJO14JL25OE
                [opus_invoice_num] => SEVIEIN19013
                [customer_name] => RON NELSON
                [mobile_num] => 8653821076
                [opus_amount] => 25
                [rq4_amount] => 5
                [difference] => -20
                [ocomment] => Re-Upload: We have been charged in opus for $25. Provide RQ4 invoice number for remaining amount
                [mark_delete] => 0
                [upload_date] => 2019-10-21
            )

        [8] => Array
            (
                [store_id] => 0178
                [employee_uid] => 572547
                [opus_id] => ms5674
                [item_description] => PRE $45 PAYG PINLESS REFILL
                [opus_transaction_date] => 2019-10-21
                [opus_transaction_num] => XT29916JL4S69
                [opus_invoice_num] => T0178BP1590
                [customer_name] => GABRIEL LONGORIA JR
                [mobile_num] => 4322133450
                [opus_amount] => 45
                [rq4_amount] => 0
                [difference] => -45
                [ocomment] => Re-Upload: Please email the details to Treasury and confirm
                [mark_delete] => 0
                [upload_date] => 2019-10-22
            )

        [9] => Array
            (
                [store_id] => 2180
                [employee_uid] => 7842
                [opus_id] => lm854y
                [item_description] => $30 RTR
                [opus_transaction_date] => 2019-10-21
                [opus_transaction_num] => XC9U712JL4LA4
                [opus_invoice_num] => KETERIN1836
                [customer_name] => PETE JABLONSKI
                [mobile_num] => 9374092680
                [opus_amount] => 30
                [rq4_amount] => 40
                [difference] => 10
                [ocomment] => Re-Upload: Credit the remaining balance to customers account in OPUS and email confirmation to Treasury
                [mark_delete] => 0
                [upload_date] => 2019-10-22
            )


      .
      .
      .
 [63] => Array
            (
                [store_id] => 0175
                [employee_uid] => 33738
                [opus_id] => ph5953
                [item_description] => PRE ATT $40 RTR
                [opus_transaction_date] => 2019-10-21
                [opus_transaction_num] => XE5N31DJL51RA
                [opus_invoice_num] => T0175IN4563
                [customer_name] => WILLIE TAYLOR
                [mobile_num] => 6822701188
                [opus_amount] => 40
                [rq4_amount] => 50
                [difference] => 10
                [ocomment] => Re-Upload: Credit the remaining balance to customers account in OPUS and email confirmation to Treasury
                [mark_delete] => 0
                [upload_date] => 2019-10-22
            ) 

    )

Hướng dẫn read excel file in php - đọc tệp excel bằng php

Milo

3.2679 Huy hiệu vàng28 Huy hiệu bạc43 Huy hiệu đồng9 gold badges28 silver badges43 bronze badges

Đã trả lời ngày 5 tháng 11 năm 2019 lúc 18:45Nov 5, 2019 at 18:45

Hướng dẫn read excel file in php - đọc tệp excel bằng php

Tôi đã sử dụng mã sau để đọc "XLS và XLSX":

    include 'PHPExcel/IOFactory.php';

    $location='sample-excel-files.xlsx';

    $objPHPExcel = PHPExcel_IOFactory::load($location);
    $sheet = $objPHPExcel->getSheet(0);
    $total_rows = $sheet->getHighestRow();
    $total_columns = $sheet->getHighestColumn();
    $set_excel_query_all=array();
    for($row =2; $row <= $total_rows; $row++) {
        $singlerow = $sheet->rangeToArray('A' . $row . ':' . $total_columns . $row, NULL, TRUE, FALSE);
        $single_row=$singlerow[0];
        $set_excel_query['store_id']=$single_row[0];
        $set_excel_query['employee_uid']=$single_row[1];
        $set_excel_query['opus_id']=$single_row[2];
        $set_excel_query['item_description']=$single_row[3];
        if($single_row[4])
        {
            $set_excel_query['opus_transaction_date']= date('Y-m-d', PHPExcel_Shared_Date::ExcelToPHP($single_row[4]));
        }
        $set_excel_query['opus_transaction_num']=$single_row[5];
        $set_excel_query['opus_invoice_num']=$single_row[6];
        $set_excel_query['customer_name']=$single_row[7];
        $set_excel_query['mobile_num']=$single_row[8];
        $set_excel_query['opus_amount']=$single_row[9];
        $set_excel_query['rq4_amount']=$single_row[10];
        $set_excel_query['difference']=$single_row[11];
        $set_excel_query['ocomment']=$single_row[12];
        $set_excel_query['mark_delete']=$single_row[13];
        if($single_row[14])
        {
            $set_excel_query['upload_date']= date('Y-m-d', PHPExcel_Shared_Date::ExcelToPHP($single_row[14]));
        }
        $set_excel_query_all[]=$set_excel_query;
    }

   print_r($set_excel_query_all); 

Hướng dẫn read excel file in php - đọc tệp excel bằng php

Awais Qarni

16.6K24 Huy hiệu vàng74 Huy hiệu bạc135 Huy hiệu Đồng24 gold badges74 silver badges135 bronze badges

Đã trả lời ngày 5 tháng 11 năm 2019 lúc 18:41Nov 5, 2019 at 18:41

Hướng dẫn read excel file in php - đọc tệp excel bằng php

Chúng ta có thể đọc tệp excel trong PHP không?

PHPSPREADSHEET là một thư viện được viết bằng PHP thuần túy và cung cấp một tập hợp các lớp cho phép bạn đọc và viết các định dạng tệp bảng tính khác nhau như Excel và LibreOffice Calc. Trong hướng dẫn này, chúng tôi sẽ tìm hiểu cách đọc và viết tệp XLSX. Bạn có thể tích hợp nó với cơ sở dữ liệu của bạn nếu bạn cần.. In this tutorial, we are going learn how to read and write the xlsx file. You can integrate it with your database if you need.

Làm cách nào để mở tệp XLSX trong PHP?

Đọc một tệp excel (xlsx) Mở tệp XSLX với $ reader-> mở ($ path) duyệt từng trang tính của bảng tính với $ reader-> getSheetIterator () Duyệt từng hàng của trang tính với $ feet-> getrowiterator ()Duyệt từng ô của hàng với $ row-> getCells ()$reader->open($path) Browse each Sheet of the spreadsheet with $reader->getSheetIterator() Browse each Row of the Sheet with $sheet->getRowIterator() Browse each Cell of the Row with $row->getCells()

Làm thế nào để bạn đọc và viết tệp excel trong PHP?

Chúng tôi sẽ sử dụng thư viện phpspreadsheet để đọc/ghi các tệp excel và chúng tôi sẽ tìm hiểu cách đọc các ô, tìm một chuỗi cụ thể theo một hàng hoặc cột, ghi vào các ô, đặt kiểu dáng và định dạng ô (như đặt đường viền, màu nền, màuvv) Và chúng ta sẽ xem cách chèn hình ảnh.Hãy xem làm thế nào để làm điều đó trong ví dụ dưới đây.

Làm thế nào kết nối Excel với PHP?

Thiết lập kết nối Mở kết nối đến Excel bằng cách gọi các phương thức ODBC_Connect hoặc ODBC_PConnect.Để đóng các kết nối, sử dụng ODBC_CLOSE hoặc ODBC_CLOSE_ALL.$ Conn = ODBC_Connect ("Cdata ODBC Excel Nguồn", "Người dùng", "Mật khẩu");Các kết nối được mở với ODBC_Connect được đóng khi tập lệnh kết thúc.Open the connection to Excel by calling the odbc_connect or odbc_pconnect methods. To close connections, use odbc_close or odbc_close_all. $conn = odbc_connect("CData ODBC Excel Source","user","password"); Connections opened with odbc_connect are closed when the script ends.