Hướng dẫn how to get cell value in phpspreadsheet? - làm thế nào để lấy giá trị ô trong phpspreadsheet?

Tôi đang sử dụng phpspreadsheet để dễ dàng đọc từ tài liệu XLS và chèn vào DB sau một số tính toán. Tôi đã thành công khi sử dụng các ví dụ từ tài liệu, nhưng tôi thấy nó rất phức tạp Tôi chắc chắn rằng tôi đã bỏ lỡ một cái gì đó và nó có thể được thực hiện dễ dàng hơn nhiều.

$worksheet = $this->getWorksheet("file.xls");
foreach ($worksheet->getRowIterator() as $row) {
  $cellIterator = $row->getCellIterator();
  $cellIterator->setIterateOnlyExistingCells(FALSE);
  foreach ($cellIterator as $key => $cell) {
    $cellValue = $cell->getValue();

    if($key == 'A')
      $field1 = $cellValue;
    if($key == 'B') {
      $dateTime = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($cellValue);
      $date = $dateTime->format("Y-m-d");
    }
    if($key == 'C')
      $field2 = $cellValue;
    if($key == 'D')
      $field3 = $cellValue;
    if($key == 'E')
      $field4 = $cellValue;
  }
}

Tôi đã mong đợi một cái gì đó như $row->getCell("A")->getValue() sẽ có sẵn.

Vậy ... tôi đã bỏ lỡ điều gì?

Hỏi ngày 1 tháng 4 năm 2018 lúc 9:47Apr 1, 2018 at 9:47

Hướng dẫn how to get cell value in phpspreadsheet? - làm thế nào để lấy giá trị ô trong phpspreadsheet?

Jeremy Belolojeremy BeloloJeremy Belolo

4.0456 Huy hiệu vàng39 Huy hiệu bạc81 Huy hiệu đồng6 gold badges39 silver badges81 bronze badges

Xem các tài liệu về việc nhận các giá trị theo cột và hàng trực tiếp thay vì kiểm tra các khóa

Từ ví dụ:

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();

Hy vọng điều đó sẽ giúp

Đã trả lời ngày 3 tháng 4 năm 2018 lúc 22:05Apr 3, 2018 at 22:05

1

Đây là những gì tôi tìm thấy

        $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
        $spreadsheet = $reader->load("test.xlsx");
        $sheet = $spreadsheet->getSheet(0);

        $nb = 0;

        foreach ($sheet->getRowIterator() as $row) {

            echo $sheet->getCell("A$nb")->getValue();
            echo "
"; $nb++; }

Đã trả lời ngày 3 tháng 12 năm 2020 lúc 13:27Dec 3, 2020 at 13:27

Hướng dẫn how to get cell value in phpspreadsheet? - làm thế nào để lấy giá trị ô trong phpspreadsheet?

Bạn phải truy cập bằng cách nào đó mỗi ô. Ví dụ: Đối với trường hợp khi bạn không biết kích thước của các hàng và cột (... có thể kết hợp các chữ cái qua các chữ cái bảng chữ cái đơn), hãy lặp lại cho mỗi hàng và sau đó cho mỗi cột, và cuối cùng bạn có thể có Dữ liệu được phân tích cú pháp vào mảng đa chiều:

        $results = [];
        $reader = new Xlsx(); //   PhpOffice\PhpSpreadsheet\Reader\Xlsx
        $reader->setReadDataOnly(true);
        $spreadsheet = $reader->load(storage_path('app/temp/' . $file));
        $sheet = $spreadsheet->getActiveSheet();

        foreach ($sheet->getRowIterator() as $index => $row) {
            $cellIterator = $row->getCellIterator();
            $cellIterator->setIterateOnlyExistingCells(FALSE); // This loops through all cells, even if a cell value is not set.
            $row_content = [];
            foreach ($cellIterator as $cell) {
                array_push($row_content, $cell->getValue());
            }

            $results[] = $row_content;
        }

Đã trả lời ngày 10 tháng 6 lúc 10:28Jun 10 at 10:28

Hướng dẫn how to get cell value in phpspreadsheet? - làm thế nào để lấy giá trị ô trong phpspreadsheet?

setValueExplicit ()

Đặt giá trị cho một ô, với kiểu dữ liệu rõ ràng được truyền vào phương thức (bỏ qua mọi cách sử dụng chất kết dính giá trị).

private mixed $calculatedValue 1  : mixed Calculated value of the cell (used for caching) This returns the value last calculated by MS Excel or whichever spreadsheet program was used to create the original spreadsheet file. $dataType  : string Type of the cell data. $formulaAttributes  : mixed Attributes of the formula. $parent  : CellsThe collection of cells that this cell belongs to (i.e. The Cell Collection for the parent Worksheet). $value  : mixed Value of the cell. $valueBinder  : IValueBinderValue binder to use. $xfIndex  : int Index to the cellXf reference for the styling of this cell. __clone()  : mixed Implement PHP __clone to create a deep clone, not just a shallow copy. __construct()  : mixed Create a new Cell. __toString()  : string Convert to string. attach()  : void compareCells()  : int Compare 2 cells. detach()  : void getAppliedStyle()  : StyleGet cell style. getCalculatedValue()  : mixed Get calculated cell value. getColumn()  : string Get cell coordinate column. getCoordinate()  : string Get cell coordinate. getDataType()  : string Get cell data type. getDataValidation()  : DataValidationGet Data validation rules. getFormattedValue()  : string Get cell value with formatting. getFormulaAttributes()  : mixed Get the formula attributes. getHyperlink()  : HyperlinkGet Hyperlink. getMergeRange()  : false|string If this cell is in a merge range, then return the range. getOldCalculatedValue()  : mixed Get old calculated value (cached) This returns the value last calculated by MS Excel or whichever spreadsheet program was used to create the original spreadsheet file. getParent()  : CellsGet cell collection. getRow()  : int Get cell coordinate row. getStyle()  : StyleGet cell style. getValue()  : mixed Get cell value. getValueBinder()  : IValueBinderGet value binder to use. getWorksheet()  : WorksheetGet parent worksheet. getXfIndex()  : int Get index to cellXf. hasDataValidation()  : bool Does this cell contain Data validation rules? hasHyperlink()  : bool Does this cell contain a Hyperlink? hasValidValue()  : bool Does this cell contain valid value? isFormula()  : bool Identify if the cell contains a formula. isInMergeRange()  : bool Is this cell in a merge range. isInRange()  : bool Is cell in a specific range? isMergeRangeValueCell()  : bool Is this cell the master (top left cell) in a merge range (that holds the actual data value). rebindParent()  : self Re-bind parent. setCalculatedValue()  : self Set old calculated value (cached). setDataType()  : self Set cell data type. setDataValidation()  : self Set Data validation rules. setFormulaAttributes()  : $this Set the formula attributes. setHyperlink()  : self Set Hyperlink. setValue()  : $this Set cell value. setValueBinder()  : void Set value binder to use. setValueExplicit()  : CellSet the value for a cell, with the explicit data type passed to the method (bypassing any use of the value binder). setXfIndex()  : self Set index to cellXf. updateInCollection()  : $this Update the cell into the cell collection.

$ Tính toán

Giá trị được tính toán của ô (được sử dụng để lưu trữ), điều này trả về giá trị cuối cùng được tính bởi MS Excel hoặc bất kỳ chương trình bảng tính nào được sử dụng để tạo tệp bảng tính gốc.

private mixed $calculatedValue

Lưu ý rằng giá trị này không được đảm bảo để phản ánh giá trị được tính toán thực tế vì có thể tính toán tự động đã bị vô hiệu hóa trong bảng tính gốc và các giá trị dữ liệu cơ bản được sử dụng bởi công thức đã thay đổi kể từ lần cuối cùng được tính toán.

$ DataType

Loại dữ liệu tế bào.

private string $dataType

$ formulaAttribution

Thuộc tính của công thức.

private mixed $formulaAttributes

$ cha mẹ

Bộ sưu tập các ô mà tế bào này thuộc về (nghĩa là bộ sưu tập ô cho bảng tính cha mẹ).

________số 8

$ giá trị

Giá trị của tế bào.

private mixed $value

$ valueBinder

Giá trị chất kết dính để sử dụng.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
0

$ xfindex

Chỉ mục vào tham chiếu cellXF cho kiểu dáng của ô này.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
1

__dòng vô tính()

Thực hiện PHP __clone để tạo ra một bản sao sâu, không chỉ là một bản sao nông.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
2
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Thông số
$ value: hỗn hợp $ DataType: String | NULL $ Bảng tính: Bảng tính : mixed $dataType : string|null $worksheet : Worksheet
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.
Thông số
$ value: hỗn hợp $ DataType: String | NULL $ Bảng tính: Bảng tính : Cells
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Thông số
$ value: hỗn hợp $ DataType: String | NULL $ Bảng tính: Bảng tính : Cell

__tostring ()

Chuyển đổi thành chuỗi. : Cell

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
4

Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Thông số
$ value: hỗn hợp $ DataType: String | NULL $ Bảng tính: Bảng tính : bool = true

__tostring ()

Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3

Lưu ý rằng giá trị này không được đảm bảo để phản ánh giá trị được tính toán thực tế vì có thể tính toán tự động đã bị vô hiệu hóa trong bảng tính gốc và các giá trị dữ liệu cơ bản được sử dụng bởi công thức đã thay đổi kể từ lần cuối cùng được tính toán.

Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Thông số
$ value: hỗn hợp $ DataType: String | NULL $ Bảng tính: Bảng tính : string

__tostring ()

Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Thông số
$ value: hỗn hợp $ DataType: String | NULL $ Bảng tính: Bảng tính : Worksheet
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Thông số
$ value: hỗn hợp $ DataType: String | NULL $ Bảng tính: Bảng tính : mixed

__tostring ()

Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Thông số
$ value: hỗn hợp $ DataType: String | NULL $ Bảng tính: Bảng tính : string

__tostring ()

Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Thông số
$ value: hỗn hợp $ DataType: String | NULL $ Bảng tính: Bảng tính : DataValidation|null = null
Trả về giá trị
Trộn -

__xây dựng()

Tạo một ô mới.

// Get the value from cell B5
$cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 5)->getValue();
3
Thông số
$ value: hỗn hợp $ DataType: String | NULL $ Bảng tính: Bảng tính : mixed
Trả về giá trị
$ cái này -

SethyperLink ()

Đặt siêu liên kết.

$row->getCell("A")->getValue()8
Thông số
$ Hyperlink: Hyperlink | null = null : Hyperlink|null = null
Trả về giá trị
bản thân -

đặt giá trị()

Đặt giá trị ô.

$row->getCell("A")->getValue()9

Đặt giá trị cho một ô, tự động xác định kiểu dữ liệu bằng cách sử dụng chất kết dính giá trị

Thông số
$ Hyperlink: Hyperlink | null = null : mixed

Trả về giá trị

Trả về giá trị
$ cái này -

bản thân -

đặt giá trị()

Đặt giá trị ô.
Thông số
$ Hyperlink: Hyperlink | null = null : IValueBinder
Trả về giá trị
bản thân -

đặt giá trị()

Đặt giá trị ô.

$row->getCell("A")->getValue()9
Thông số
$ Hyperlink: Hyperlink | null = null : mixed

Trả về giá trị

bản thân - : string

đặt giá trị()

Trả về giá trị
bản thân -

đặt giá trị()

Đặt giá trị ô.

$row->getCell("A")->getValue()9
Thông số
$ Hyperlink: Hyperlink | null = null : int
Trả về giá trị
bản thân -

đặt giá trị()

Đặt giá trị ô.

$row->getCell("A")->getValue()9
Trả về giá trị
$ cái này -

    Làm cách nào để tải xuống phpspreadsheet?

    Sử dụng trình soạn thảo để cài đặt phpspreadsheet vào dự án của bạn.Hoặc cũng tải xuống tài liệu và mẫu nếu bạn có kế hoạch sử dụng chúng.Một cách tốt để bắt đầu là chạy một số mẫu.Đừng quên tải chúng thông qua-Cờ của nhà soạn nhạc nguồn hàng.. Or also download the documentation and samples if you plan to use them. A good way to get started is to run some of the samples. Don't forget to download them via --prefer-source composer flag.

    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 cách nào để thay đổi kích thước phông chữ trong phpexcel?

    $objPHPExcel->getActiveSheet()->getStyle("F1:G1")->getFont()->setFontSize(16);