Làm cách nào để kết nối Excel với php?

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

Làm cách nào để kết nối Excel với php?

Bharathiraja

Làm theo

18 Tháng mười hai, 2020

·

3 phút đọc

·

Chỉ dành cho thành viên

PHP đọc và ghi tệp Excel bằng PhpSpreadsheet

Gần đây tôi có một yêu cầu trong PHP để đọc và viết một tệp excel

  1. Yêu cầu đầu tiên là đọc file excel và đẩy dữ liệu lên cơ sở dữ liệu
  2. Yêu cầu thứ hai là, Tạo tệp Excel bằng dữ liệu từ cơ sở dữ liệu. Tạo Excel ở phía máy chủ. Không phải phía khách hàng

php có thể tương tác với excel không?

PHP cung cấp thư viện để xử lý các tệp Excel . Nó được gọi là thư viện PHP Excel. Nó cho phép bạn đọc và viết bảng tính ở nhiều định dạng khác nhau bao gồm csv, xls, ods và xlsx. Bạn sẽ cần đảm bảo rằng bạn có phiên bản nâng cấp của PHP không cũ hơn PHP 5. 2.

Làm cách nào để sử dụng trang tính Excel trong php?

3 bước dễ dàng để tạo bảng tính Excel trong PHP .
Bước 1. Loại nội dung. Điều đầu tiên bạn phải làm là thêm một ứng dụng/vnd. .
Bước 2. Thêm dữ liệu. Dữ liệu chỉ là đơn giản. .
Bước 3. Tải xuống bảng tính. Bây giờ bạn đã đặt loại nội dung và tạo dữ liệu, chỉ cần mở tệp PHP trong trình duyệt

Làm cách nào để lấy dữ liệu từ trang tính Excel trong php?

$data = new Spreadsheet_Excel_Reader("example. xls"); $rows = $data->sheets[0]['numRows']; for($i=0;$isheets);$i++) // Loop to get all sheets in a file.