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

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 đó.

Đã 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+] //github.com/shuchkin/simplexlsx
//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] //github.com/shuchkin/simplexls
//github.com/shuchkin/simplexls

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

Người đọc CSV PHP //github.com/shuchkin/simplecsv
//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

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"

    

Bài Viết Liên Quan

Chủ Đề