Hướng dẫn phpexcel get cell value not formula - phpexcel nhận giá trị ô không phải công thức

Tôi có tệp Excel sau:Excel file:

Show

Hướng dẫn phpexcel get cell value not formula - phpexcel nhận giá trị ô không phải công thức

Tôi đã đọc nó bằng cách lặp qua mọi ô và nhận giá trị với

// Set cell A1 with a string value
$spreadsheet->getActiveSheet()->setCellValue('A1', 'PhpSpreadsheet');

// Set cell A2 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A2', 12345.6789);

// Set cell A3 with a boolean value
$spreadsheet->getActiveSheet()->setCellValue('A3', TRUE);

// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);
5:looping over every cell and getting the value with
// Set cell A1 with a string value
$spreadsheet->getActiveSheet()->setCellValue('A1', 'PhpSpreadsheet');

// Set cell A2 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A2', 12345.6789);

// Set cell A3 with a boolean value
$spreadsheet->getActiveSheet()->setCellValue('A3', TRUE);

// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);
5:

$highestColumnAsLetters = $this->objPHPExcel->setActiveSheetIndex(0)->getHighestColumn(); //e.g. 'AK'
$highestRowNumber = $this->objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
$highestColumnAsLetters++;
for ($row = 1; $row < $highestRowNumber + 1; $row++) {
    $dataset = array();
    for ($columnAsLetters = 'A'; $columnAsLetters != $highestColumnAsLetters; $columnAsLetters++) {
        $dataset[] = $this->objPHPExcel->setActiveSheetIndex(0)->getCell($columnAsLetters.$row)->getValue();
        if ($row == 1)
        {
        $this->column_names[] = $columnAsLetters;
        }
    }
    $this->datasets[] = $dataset;
}

Tuy nhiên, mặc dù nó đọc trong dữ liệu tốt, nhưng nó đọc trong các tính toán theo nghĩa đen:literally:

Hướng dẫn phpexcel get cell value not formula - phpexcel nhận giá trị ô không phải công thức

Tôi hiểu từ các cuộc thảo luận như thế này mà tôi có thể sử dụng

// Set cell A1 with a string value
$spreadsheet->getActiveSheet()->setCellValue('A1', 'PhpSpreadsheet');

// Set cell A2 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A2', 12345.6789);

// Set cell A3 with a boolean value
$spreadsheet->getActiveSheet()->setCellValue('A3', TRUE);

// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);
6 cho các ô được tính toán.

Vấn đề là trong các tờ Excel tôi đang nhập, tôi không biết trước các ô nào được tính toán và cái nào không.

Có cách nào để tôi đọc trong giá trị của một ô theo cách tự động nhận được giá trị nếu nó có giá trị đơn giản và có được kết quả của phép tính nếu đó là một tính toán?

Answer:

Hóa ra

// Set cell A1 with a string value
$spreadsheet->getActiveSheet()->setCellValue('A1', 'PhpSpreadsheet');

// Set cell A2 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A2', 12345.6789);

// Set cell A3 with a boolean value
$spreadsheet->getActiveSheet()->setCellValue('A3', TRUE);

// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);
6 hoạt động cho tất cả các ô, khiến tôi tự hỏi tại sao đây không phải là mặc định cho
// Set cell A1 with a string value
$spreadsheet->getActiveSheet()->setCellValue('A1', 'PhpSpreadsheet');

// Set cell A2 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A2', 12345.6789);

// Set cell A3 with a boolean value
$spreadsheet->getActiveSheet()->setCellValue('A3', TRUE);

// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);
8 vì tôi nghĩ người ta thường muốn giá trị của các tính toán thay vì chính phương trình, trong mọi trường hợp điều này hoạt động:

...->getCell($columnAsLetters.$row)->getCalculatedValue();

Hướng dẫn phpexcel get cell value not formula - phpexcel nhận giá trị ô không phải công thức

Tùy chọn, hành vi mặc định của phpspreadsheet có thể được sửa đổi, cho phép nhập dữ liệu dễ dàng hơn. Ví dụ, một lớp

// Set cell A9 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A9', 1513789642);
// Set a number format mask to display the value as 11 digits with leading zeroes
$spreadsheet->getActiveSheet()->getStyle('A9')
    ->getNumberFormat()
    ->setFormatCode(
        '00000000000'
    );
2 có sẵn. Nó tự động chuyển đổi tỷ lệ phần trăm, số ở định dạng khoa học và ngày được nhập dưới dạng chuỗi theo định dạng chính xác, cũng đặt thông tin kiểu của ô. Ví dụ sau đây cho thấy cách đặt chất kết dính giá trị trong phpspreadsheet:

Ngoài ra, một lớp

// Set cell A9 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A9', 1513789642);
// Set a number format mask to display the value as 11 digits with leading zeroes
$spreadsheet->getActiveSheet()->getStyle('A9')
    ->getNumberFormat()
    ->setFormatCode(
        '00000000000'
    );
3 có sẵn nếu bạn muốn lưu giữ tất cả nội dung dưới dạng chuỗi. Điều này có thể phù hợp nếu bạn đang tải một tệp chứa các giá trị có thể được hiểu là số (ví dụ: các số có dấu hiệu hàng đầu như số điện thoại quốc tế như
// Set cell A9 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A9', 1513789642);
// Set a number format mask to display the value as 11 digits with leading zeroes
$spreadsheet->getActiveSheet()->getStyle('A9')
    ->getNumberFormat()
    ->setFormatCode(
        '00000000000'
    );
4), nhưng nên được giữ lại dưới dạng chuỗi (số điện thoại phi quốc tế với số 0 là đã được duy trì dưới dạng chuỗi).

$objWriter->setPreCalculateFormulas(true);

Theo mặc định, StringValuebinder sẽ chuyển bất kỳ kiểu dữ liệu nào được chuyển cho nó thành một chuỗi. Tuy nhiên, có một số cài đặt cho phép bạn chỉ định rằng một số kiểu dữ liệu nhất định không nên được chuyển vào chuỗi, nhưng còn lại "như là":
' . date('H:i:s') . ' Writing Non-Exception File
';

Tạo chất kết dính giá trị của riêng bạn
';

Tạo chất kết dính giá trị của riêng bạn tương đối đơn giản. Khi yêu cầu ràng buộc giá trị chuyên dụng hơn, bạn có thể triển khai giao diện

// Set cell A8 with a numeric value, but tell PhpSpreadsheet it should be treated as a string
$spreadsheet->getActiveSheet()->setCellValueExplicit(
    'A8',
    "01513789642",
    \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING
);
9 hoặc mở rộng các lớp
// Set cell A9 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A9', 1513789642);
// Set a number format mask to display the value as 11 digits with leading zeroes
$spreadsheet->getActiveSheet()->getStyle('A9')
    ->getNumberFormat()
    ->setFormatCode(
        '00000000000'
    );
6 hoặc
// Set cell A9 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A9', 1513789642);
// Set a number format mask to display the value as 11 digits with leading zeroes
$spreadsheet->getActiveSheet()->getStyle('A9')
    ->getNumberFormat()
    ->setFormatCode(
        '00000000000'
    );
2 hiện có.

Thank-You

Mark, tôi đang gặp vấn đề tương tự tôi đã thử thêm dòng dưới đây và nó không giúp ích gì cả. Đây là mã mà tôi sử dụng:

$ objwriter = phpExcel_iofactory :: createWriter ($ objphpwrite, "excel2007");

tiếng vang ''. ngày ('h: i: s'). 'Viết tệp không Exception';

Echo 'Tệp đầu ra là:'. $ outputFile. '';

$ objwriter-> lưu (str_replace ('. php', '.xlsx', $ outputFile));

Terry Titus

Hỗ trợ IT
Sent: Friday, March 25, 2016 10:53 AM
To: PHPOffice/PHPExcel
Subject: Re: [PHPExcel] Impossible to have calculated cells (formulas) with PHPExcel_Writer_Excel2007 (#846)

Seattle Bulk Shipping, Inc

E-mail:
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub #846 (comment) https://github.com/notifications/beacon/ABX8y_17VFFNBxFd8tD_CQBjW7jqLrlNks5pxCEVgaJpZM4Hn8Ii.gif


Điện thoại: 206-652-1356 ext 111
https://www.avast.com/antivirus

Tùy chọn, hành vi mặc định của phpspreadsheet có thể được sửa đổi, cho phép nhập dữ liệu dễ dàng hơn. Ví dụ, một lớp

// Set cell A9 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A9', 1513789642);
// Set a number format mask to display the value as 11 digits with leading zeroes
$spreadsheet->getActiveSheet()->getStyle('A9')
    ->getNumberFormat()
    ->setFormatCode(
        '00000000000'
    );
2 có sẵn. Nó tự động chuyển đổi tỷ lệ phần trăm, số ở định dạng khoa học và ngày được nhập dưới dạng chuỗi theo định dạng chính xác, cũng đặt thông tin kiểu của ô. Ví dụ sau đây cho thấy cách đặt chất kết dính giá trị trong phpspreadsheet:

Ngoài ra, một lớp // Set cell A9 with a numeric value $spreadsheet->getActiveSheet()->setCellValue('A9', 1513789642); // Set a number format mask to display the value as 11 digits with leading zeroes $spreadsheet->getActiveSheet()->getStyle('A9') ->getNumberFormat() ->setFormatCode( '00000000000' ); 3 có sẵn nếu bạn muốn lưu giữ tất cả nội dung dưới dạng chuỗi. Điều này có thể phù hợp nếu bạn đang tải một tệp chứa các giá trị có thể được hiểu là số (ví dụ: các số có dấu hiệu hàng đầu như số điện thoại quốc tế như // Set cell A9 with a numeric value $spreadsheet->getActiveSheet()->setCellValue('A9', 1513789642); // Set a number format mask to display the value as 11 digits with leading zeroes $spreadsheet->getActiveSheet()->getStyle('A9') ->getNumberFormat() ->setFormatCode( '00000000000' ); 4), nhưng nên được giữ lại dưới dạng chuỗi (số điện thoại phi quốc tế với số 0 là đã được duy trì dưới dạng chuỗi).

Theo mặc định, StringValuebinder sẽ chuyển bất kỳ kiểu dữ liệu nào được chuyển cho nó thành một chuỗi. Tuy nhiên, có một số cài đặt cho phép bạn chỉ định rằng một số kiểu dữ liệu nhất định không nên được chuyển vào chuỗi, nhưng còn lại "như là":

// Set cell A1 with a string value
$spreadsheet->getActiveSheet()->setCellValue('A1', 'PhpSpreadsheet');

// Set cell A2 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A2', 12345.6789);

// Set cell A3 with a boolean value
$spreadsheet->getActiveSheet()->setCellValue('A3', TRUE);

// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);

Tạo chất kết dính giá trị của riêng bạn

$spreadsheet->getActiveSheet()
    ->getCell('B8')
    ->setValue('Some value');

Tạo chất kết dính giá trị của riêng bạn tương đối đơn giản. Khi yêu cầu ràng buộc giá trị chuyên dụng hơn, bạn có thể triển khai giao diện // Set cell A8 with a numeric value, but tell PhpSpreadsheet it should be treated as a string $spreadsheet->getActiveSheet()->setCellValueExplicit( 'A8', "01513789642", \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING ); 9 hoặc mở rộng các lớp // Set cell A9 with a numeric value $spreadsheet->getActiveSheet()->setCellValue('A9', 1513789642); // Set a number format mask to display the value as 11 digits with leading zeroes $spreadsheet->getActiveSheet()->getStyle('A9') ->getNumberFormat() ->setFormatCode( '00000000000' ); 6 hoặc // Set cell A9 with a numeric value $spreadsheet->getActiveSheet()->setCellValue('A9', 1513789642); // Set a number format mask to display the value as 11 digits with leading zeroes $spreadsheet->getActiveSheet()->getStyle('A9') ->getNumberFormat() ->setFormatCode( '00000000000' ); 2 hiện có.

Truy cập các tế bào trong một bảng tính nên khá đơn giản. Chủ đề này liệt kê một số tùy chọn để truy cập một ô.

Đặt giá trị ô theo tọa độ

Đặt giá trị ô theo tọa độ có thể được thực hiện bằng phương pháp

// Set cell A1 with a string value
$spreadsheet->getActiveSheet()->setCellValue('A1', 'PhpSpreadsheet');

// Set cell A2 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A2', 12345.6789);

// Set cell A3 with a boolean value
$spreadsheet->getActiveSheet()->setCellValue('A3', TRUE);

// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);
9 của bảng tính.

Ngoài ra, bạn có thể truy xuất đối tượng ô và sau đó gọi phương thức ô ____

$spreadsheet->getActiveSheet()
    ->getCell('B8')
    ->setValue('Some value');
0:

Tạo một ô mới

Nếu bạn thực hiện cuộc gọi đến

$spreadsheet->getActiveSheet()
    ->getCell('B8')
    ->setValue('Some value');
1 và ô chưa tồn tại, thì phpspreadsheet sẽ tạo ô đó cho bạn.

Vì vậy, khi chúng tôi cố gắng hiển thị giá trị và địa chỉ lần thứ hai, chúng tôi có thể hiển thị giá trị của nó, nhưng cố gắng hiển thị địa chỉ/tọa độ của nó sẽ ném một ngoại lệ vì liên kết đó đã được đặt thành NULL.

Lưu ý: Có một số phương thức nội bộ sẽ tìm nạp các ô khác từ bộ sưu tập và điều này cũng sẽ tách liên kết đến bộ sưu tập khỏi bất kỳ ô nào mà bạn có thể đã gán cho một biến. There are some internal methods that will fetch other cells from the collection, and this too will detach the link to the collection from any cell that you might have assigned to a variable.

Kiểu dữ liệu Excel

MS Excel hỗ trợ 7 kiểu dữ liệu cơ bản:

  • sợi dây
  • con số
  • boolean
  • null
  • công thức
  • lỗi
  • Chuỗi nội tuyến (hoặc văn bản phong phú)

Theo mặc định, khi bạn gọi phương thức

// Set cell A1 with a string value
$spreadsheet->getActiveSheet()->setCellValue('A1', 'PhpSpreadsheet');

// Set cell A2 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A2', 12345.6789);

// Set cell A3 with a boolean value
$spreadsheet->getActiveSheet()->setCellValue('A3', TRUE);

// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);
9 của bảng tính hoặc phương thức
$spreadsheet->getActiveSheet()
    ->getCell('B8')
    ->setValue('Some value');
0 của ô, phpspreadsheet sẽ sử dụng kiểu dữ liệu thích hợp cho các null, booleans, floats hoặc số nguyên của PHP; hoặc đúc bất kỳ giá trị dữ liệu chuỗi nào mà bạn chuyển đến phương thức vào kiểu dữ liệu phù hợp nhất, do đó các chuỗi số sẽ được chuyển thành số, trong khi các giá trị chuỗi bắt đầu bằng
$spreadSheet = new Spreadsheet();
$workSheet = $spreadSheet->getActiveSheet();

// Set details for the formula that we want to evaluate, together with any data on which it depends
$workSheet->fromArray(
    [1, 2, 3],
    null,
    'A1'
);

$cellC1 = $workSheet->getCell('C1');
echo 'Value: ', $cellC1->getValue(), '; Address: ', $cellC1->getCoordinate(), PHP_EOL;

$cellA1 = $workSheet->getCell('A1');
echo 'Value: ', $cellA1->getValue(), '; Address: ', $cellA1->getCoordinate(), PHP_EOL;

echo 'Value: ', $cellC1->getValue(), '; Address: ', $cellC1->getCoordinate(), PHP_EOL;
2 sẽ được chuyển đổi thành công thức. Các chuỗi không phải là số, hoặc không bắt đầu bằng một
$spreadSheet = new Spreadsheet();
$workSheet = $spreadSheet->getActiveSheet();

// Set details for the formula that we want to evaluate, together with any data on which it depends
$workSheet->fromArray(
    [1, 2, 3],
    null,
    'A1'
);

$cellC1 = $workSheet->getCell('C1');
echo 'Value: ', $cellC1->getValue(), '; Address: ', $cellC1->getCoordinate(), PHP_EOL;

$cellA1 = $workSheet->getCell('A1');
echo 'Value: ', $cellA1->getValue(), '; Address: ', $cellA1->getCoordinate(), PHP_EOL;

echo 'Value: ', $cellC1->getValue(), '; Address: ', $cellC1->getCoordinate(), PHP_EOL;
2 hàng đầu sẽ được coi là giá trị chuỗi chính hãng.

Lưu ý rằng một chuỗi số bắt đầu bằng số 0 hàng đầu (không ngay lập tức được theo sau bởi một dấu phân cách thập phân) sẽ không được chuyển đổi thành một số, do đó các giá trị như số điện thoại (ví dụ: `01615991375``will vẫn là chuỗi).

"Chuyển đổi" này được xử lý bởi một "chất kết dính giá trị" ô và bạn có thể viết "các chất kết dính giá trị" tùy chỉnh để thay đổi hành vi của "chuyển đổi" này. Gói PhpsPreadsheet tiêu chuẩn cũng cung cấp "chất kết dính giá trị tiên tiến" xử lý một số chuyển đổi phức tạp hơn, chẳng hạn như chuyển đổi chuỗi với định dạng phân số như "3/4" thành giá trị số (0,75 trong trường hợp này) và đặt một bộ phận thích hợp " Phân số "Mặt nạ định dạng số. Tương tự, các chuỗi như "5%" sẽ được chuyển đổi thành giá trị 0,05 và mặt nạ định dạng số phần trăm được áp dụng và các chuỗi chứa các giá trị trông giống như ngày sẽ được chuyển đổi thành các giá trị DatetImestamp tuần tự hóa Excel và mặt nạ tương ứng được áp dụng. Điều này đặc biệt hữu ích khi tải dữ liệu từ các tệp CSV hoặc đặt giá trị ô từ cơ sở dữ liệu.

Các định dạng được xử lý bởi chất kết dính giá trị nâng cao bao gồm:

  • Đúng hoặc sai (phụ thuộc vào cài đặt địa phương) được chuyển đổi thành booleans.
  • Các chuỗi số được xác định là định dạng khoa học (hàm mũ) được chuyển đổi thành số.
  • Phân số và phân số thô tục được chuyển đổi thành số và mặt nạ định dạng số thích hợp được áp dụng.
  • Tỷ lệ phần trăm được chuyển đổi thành các số, chia cho 100 và mặt nạ định dạng số thích hợp được áp dụng.
  • Ngày và thời gian được chuyển đổi thành các giá trị dấu thời gian Excel (số) và mặt nạ định dạng số thích hợp được áp dụng.
  • Khi các chuỗi chứa một ký tự dòng mới (
    $spreadSheet = new Spreadsheet();
    $workSheet = $spreadSheet->getActiveSheet();
    
    // Set details for the formula that we want to evaluate, together with any data on which it depends
    $workSheet->fromArray(
        [1, 2, 3],
        null,
        'A1'
    );
    
    $cellC1 = $workSheet->getCell('C1');
    echo 'Value: ', $cellC1->getValue(), '; Address: ', $cellC1->getCoordinate(), PHP_EOL;
    
    $cellA1 = $workSheet->getCell('A1');
    echo 'Value: ', $cellA1->getValue(), '; Address: ', $cellA1->getCoordinate(), PHP_EOL;
    
    echo 'Value: ', $cellC1->getValue(), '; Address: ', $cellC1->getCoordinate(), PHP_EOL;
    
    4), thì kiểu dáng ô được đặt thành bọc.

Về cơ bản, nó cố gắng bắt chước hành vi của GUI MS Excel.

Bạn có thể đọc thêm về các chất kết dính giá trị sau này trong phần này của tài liệu.

Đặt công thức trong ô

Như đã nêu ở trên, nếu bạn lưu trữ một giá trị chuỗi với ký tự đầu tiên là

$spreadSheet = new Spreadsheet();
$workSheet = $spreadSheet->getActiveSheet();

// Set details for the formula that we want to evaluate, together with any data on which it depends
$workSheet->fromArray(
    [1, 2, 3],
    null,
    'A1'
);

$cellC1 = $workSheet->getCell('C1');
echo 'Value: ', $cellC1->getValue(), '; Address: ', $cellC1->getCoordinate(), PHP_EOL;

$cellA1 = $workSheet->getCell('A1');
echo 'Value: ', $cellA1->getValue(), '; Address: ', $cellA1->getCoordinate(), PHP_EOL;

echo 'Value: ', $cellC1->getValue(), '; Address: ', $cellC1->getCoordinate(), PHP_EOL;
2 trong ô. PhpsPreadsheet sẽ coi giá trị đó là một công thức và sau đó bạn có thể đánh giá công thức đó bằng cách gọi
// Set cell A1 with a string value
$spreadsheet->getActiveSheet()->setCellValue('A1', 'PhpSpreadsheet');

// Set cell A2 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A2', 12345.6789);

// Set cell A3 with a boolean value
$spreadsheet->getActiveSheet()->setCellValue('A3', TRUE);

// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);
6 so với ô.

Mặc dù vậy, có thể có nhiều lúc, khi bạn muốn lưu trữ một giá trị bắt đầu bằng

$spreadSheet = new Spreadsheet();
$workSheet = $spreadSheet->getActiveSheet();

// Set details for the formula that we want to evaluate, together with any data on which it depends
$workSheet->fromArray(
    [1, 2, 3],
    null,
    'A1'
);

$cellC1 = $workSheet->getCell('C1');
echo 'Value: ', $cellC1->getValue(), '; Address: ', $cellC1->getCoordinate(), PHP_EOL;

$cellA1 = $workSheet->getCell('A1');
echo 'Value: ', $cellA1->getValue(), '; Address: ', $cellA1->getCoordinate(), PHP_EOL;

echo 'Value: ', $cellC1->getValue(), '; Address: ', $cellC1->getCoordinate(), PHP_EOL;
2 dưới dạng chuỗi và bạn không muốn phpspreadsheet đánh giá như thể đó là một công thức.

Để làm điều này, bạn cần "thoát" giá trị bằng cách đặt nó thành "văn bản được trích dẫn".

// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);
$spreadsheet->getActiveSheet()->getCell('A4')
    ->getStyle()->setQuotePrefix(true);

Sau đó, ngay cả khi bạn yêu cầu PhpsPreadsheet trả về giá trị được tính toán cho ô

$spreadSheet = new Spreadsheet();
$workSheet = $spreadSheet->getActiveSheet();

// Set details for the formula that we want to evaluate, together with any data on which it depends
$workSheet->fromArray(
    [1, 2, 3],
    null,
    'A1'
);

$cellC1 = $workSheet->getCell('C1');
echo 'Value: ', $cellC1->getValue(), '; Address: ', $cellC1->getCoordinate(), PHP_EOL;

$cellA1 = $workSheet->getCell('A1');
echo 'Value: ', $cellA1->getValue(), '; Address: ', $cellA1->getCoordinate(), PHP_EOL;

echo 'Value: ', $cellC1->getValue(), '; Address: ', $cellC1->getCoordinate(), PHP_EOL;
8, nó sẽ trả về
$spreadSheet = new Spreadsheet();
$workSheet = $spreadSheet->getActiveSheet();

// Set details for the formula that we want to evaluate, together with any data on which it depends
$workSheet->fromArray(
    [1, 2, 3],
    null,
    'A1'
);

$cellC1 = $workSheet->getCell('C1');
echo 'Value: ', $cellC1->getValue(), '; Address: ', $cellC1->getCoordinate(), PHP_EOL;

$cellA1 = $workSheet->getCell('A1');
echo 'Value: ', $cellA1->getValue(), '; Address: ', $cellA1->getCoordinate(), PHP_EOL;

echo 'Value: ', $cellC1->getValue(), '; Address: ', $cellC1->getCoordinate(), PHP_EOL;
9 dưới dạng chuỗi và không cố gắng đánh giá công thức.

Đặt giá trị ngày và/hoặc thời gian trong ô

Giá trị ngày hoặc thời gian được giữ dưới dạng dấu thời gian trong Excel (giá trị điểm nổi đơn giản) và mặt nạ định dạng số được sử dụng để hiển thị giá trị đó nên được định dạng như thế nào; Vì vậy, nếu chúng tôi muốn lưu trữ một ngày trong một ô, chúng tôi cần tính toán dấu thời gian Excel chính xác và đặt mặt nạ định dạng số.

// Get the current date/time and convert to an Excel date/time
$dateTimeNow = time();
$excelDateValue = \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel( $dateTimeNow );
// Set cell A6 with the Excel date/time value
$spreadsheet->getActiveSheet()->setCellValue(
    'A6',
    $excelDateValue
);
// Set the number format mask so that the excel timestamp will be displayed as a human-readable date/time
$spreadsheet->getActiveSheet()->getStyle('A6')
    ->getNumberFormat()
    ->setFormatCode(
        \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME
    );

Đặt một số với số 0 hàng đầu

Theo mặc định, PHPSPREADSHEET sẽ tự động phát hiện loại giá trị và đặt nó thành kiểu dữ liệu số Excel thích hợp. Chuyển đổi loại này được xử lý bởi một chất kết dính giá trị, như được mô tả trong phần của tài liệu này có tên "Sử dụng các chất kết dính giá trị để tạo điều kiện nhập dữ liệu".

Các số không có số 0 hàng đầu, vì vậy nếu bạn cố gắng đặt giá trị số có số 0 hàng đầu (chẳng hạn như số điện thoại) thì chúng sẽ bị mất thường được hiển thị là 1513789642.

Có hai cách bạn có thể buộc PHPSPREADSHEET để ghi đè hành vi này.

Đầu tiên, bạn có thể đặt kiểu dữ liệu một cách rõ ràng dưới dạng chuỗi để nó không được chuyển đổi thành một số.

// Set cell A8 with a numeric value, but tell PhpSpreadsheet it should be treated as a string
$spreadsheet->getActiveSheet()->setCellValueExplicit(
    'A8',
    "01513789642",
    \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING
);

Ngoài ra, bạn có thể sử dụng mặt nạ định dạng số để hiển thị giá trị với số 0 đầu.

// Set cell A9 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A9', 1513789642);
// Set a number format mask to display the value as 11 digits with leading zeroes
$spreadsheet->getActiveSheet()->getStyle('A9')
    ->getNumberFormat()
    ->setFormatCode(
        '00000000000'
    );

Với mặt nạ định dạng số, bạn thậm chí có thể chia các chữ số thành các nhóm để làm cho giá trị dễ đọc hơn.

// Set cell A10 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A10', 1513789642);
// Set a number format mask to display the value as 11 digits with leading zeroes
$spreadsheet->getActiveSheet()->getStyle('A10')
    ->getNumberFormat()
    ->setFormatCode(
        '0000-000-0000'
    );

Hướng dẫn phpexcel get cell value not formula - phpexcel nhận giá trị ô không phải công thức

Lưu ý: Không phải tất cả các mặt nạ định dạng phức tạp như cái này sẽ hoạt động khi truy xuất giá trị được định dạng để hiển thị "trên màn hình" hoặc cho một số nhà văn như HTML hoặc PDF, nhưng nó sẽ hoạt động với các nhà văn bảng tính thực sự (XLSX và XLS) . that not all complex format masks such as this one will work when retrieving a formatted value to display "on screen", or for certain writers such as HTML or PDF, but it will work with the true spreadsheet writers (Xlsx and Xls).

Đặt một loạt các ô từ một mảng

Cũng có thể đặt một loạt các giá trị ô trong một cuộc gọi bằng cách chuyển một mảng các giá trị cho phương thức

// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);
$spreadsheet->getActiveSheet()->getCell('A4')
    ->getStyle()->setQuotePrefix(true);
0.

...->getCell($columnAsLetters.$row)->getCalculatedValue();
0

Hướng dẫn phpexcel get cell value not formula - phpexcel nhận giá trị ô không phải công thức

Nếu bạn vượt qua một mảng 2 chiều, thì điều này sẽ được coi là một loạt các hàng và cột. Một mảng 1-D sẽ được coi là một hàng, đặc biệt hữu ích nếu bạn tìm nạp một mảng dữ liệu từ cơ sở dữ liệu.

...->getCell($columnAsLetters.$row)->getCalculatedValue();
1

Hướng dẫn phpexcel get cell value not formula - phpexcel nhận giá trị ô không phải công thức

Nếu bạn có một mảng 1-D đơn giản và muốn viết nó dưới dạng cột, thì những điều sau đây sẽ chuyển đổi nó thành một mảng 2-D có cấu trúc phù hợp có thể được đưa vào phương thức

// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);
$spreadsheet->getActiveSheet()->getCell('A4')
    ->getStyle()->setQuotePrefix(true);
0:

...->getCell($columnAsLetters.$row)->getCalculatedValue();
2

Hướng dẫn phpexcel get cell value not formula - phpexcel nhận giá trị ô không phải công thức

Lấy giá trị ô bằng cách tọa độ

Để truy xuất giá trị của một ô, trước tiên tế bào nên được lấy từ bảng tính bằng phương pháp

$spreadsheet->getActiveSheet()
    ->getCell('B8')
    ->setValue('Some value');
1. Giá trị của ô có thể được đọc bằng phương pháp
// Set cell A1 with a string value
$spreadsheet->getActiveSheet()->setCellValue('A1', 'PhpSpreadsheet');

// Set cell A2 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A2', 12345.6789);

// Set cell A3 with a boolean value
$spreadsheet->getActiveSheet()->setCellValue('A3', TRUE);

// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);
8.

...->getCell($columnAsLetters.$row)->getCalculatedValue();
3

Điều này sẽ lấy lại giá trị thô, không định dạng có trong ô.

Nếu một ô chứa một công thức và bạn cần truy xuất giá trị được tính toán thay vì chính công thức, thì hãy sử dụng phương thức

// Set cell A1 with a string value
$spreadsheet->getActiveSheet()->setCellValue('A1', 'PhpSpreadsheet');

// Set cell A2 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A2', 12345.6789);

// Set cell A3 with a boolean value
$spreadsheet->getActiveSheet()->setCellValue('A3', TRUE);

// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);
6 của ô. Điều này được giải thích thêm trong công cụ tính toán.

...->getCell($columnAsLetters.$row)->getCalculatedValue();
4

Ngoài ra, nếu bạn muốn xem giá trị với bất kỳ định dạng ô nào được áp dụng (ví dụ: đối với giá trị ngày hoặc thời gian có thể đọc được của con người), thì bạn có thể sử dụng phương thức ____55 của ô.

...->getCell($columnAsLetters.$row)->getCalculatedValue();
5

Đặt giá trị ô theo cột và hàng

Đặt giá trị ô theo tọa độ có thể được thực hiện bằng phương pháp

// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);
$spreadsheet->getActiveSheet()->getCell('A4')
    ->getStyle()->setQuotePrefix(true);
6 của bảng tính.

...->getCell($columnAsLetters.$row)->getCalculatedValue();
6

Lưu ý: Tài liệu tham khảo cột đó bắt đầu bằng

// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);
$spreadsheet->getActiveSheet()->getCell('A4')
    ->getStyle()->setQuotePrefix(true);
7 cho cột
// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);
$spreadsheet->getActiveSheet()->getCell('A4')
    ->getStyle()->setQuotePrefix(true);
8.
that column references start with
// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);
$spreadsheet->getActiveSheet()->getCell('A4')
    ->getStyle()->setQuotePrefix(true);
7 for column
// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);
$spreadsheet->getActiveSheet()->getCell('A4')
    ->getStyle()->setQuotePrefix(true);
8.

Lấy giá trị ô theo cột và hàng

Để lấy giá trị của một ô, trước tiên tế bào nên được lấy từ bảng tính bằng phương pháp

// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);
$spreadsheet->getActiveSheet()->getCell('A4')
    ->getStyle()->setQuotePrefix(true);
9. Một giá trị di động có thể được đọc lại bằng cách sử dụng dòng mã sau:

...->getCell($columnAsLetters.$row)->getCalculatedValue();
7

Nếu bạn cần giá trị tính toán của một ô, hãy sử dụng mã sau. Điều này được giải thích thêm trong công cụ tính toán.

...->getCell($columnAsLetters.$row)->getCalculatedValue();
8

Lấy một loạt các giá trị ô đến một mảng

Cũng có thể truy xuất một loạt các giá trị ô vào một mảng trong một cuộc gọi bằng các phương thức

// Get the current date/time and convert to an Excel date/time
$dateTimeNow = time();
$excelDateValue = \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel( $dateTimeNow );
// Set cell A6 with the Excel date/time value
$spreadsheet->getActiveSheet()->setCellValue(
    'A6',
    $excelDateValue
);
// Set the number format mask so that the excel timestamp will be displayed as a human-readable date/time
$spreadsheet->getActiveSheet()->getStyle('A6')
    ->getNumberFormat()
    ->setFormatCode(
        \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME
    );
0,
// Get the current date/time and convert to an Excel date/time
$dateTimeNow = time();
$excelDateValue = \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel( $dateTimeNow );
// Set cell A6 with the Excel date/time value
$spreadsheet->getActiveSheet()->setCellValue(
    'A6',
    $excelDateValue
);
// Set the number format mask so that the excel timestamp will be displayed as a human-readable date/time
$spreadsheet->getActiveSheet()->getStyle('A6')
    ->getNumberFormat()
    ->setFormatCode(
        \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME
    );
1 hoặc
// Get the current date/time and convert to an Excel date/time
$dateTimeNow = time();
$excelDateValue = \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel( $dateTimeNow );
// Set cell A6 with the Excel date/time value
$spreadsheet->getActiveSheet()->setCellValue(
    'A6',
    $excelDateValue
);
// Set the number format mask so that the excel timestamp will be displayed as a human-readable date/time
$spreadsheet->getActiveSheet()->getStyle('A6')
    ->getNumberFormat()
    ->setFormatCode(
        \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME
    );
2.

...->getCell($columnAsLetters.$row)->getCalculatedValue();
9

Các phương thức này đều sẽ trả về một mảng 2 chiều của các hàng và cột. Phương pháp

// Get the current date/time and convert to an Excel date/time
$dateTimeNow = time();
$excelDateValue = \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel( $dateTimeNow );
// Set cell A6 with the Excel date/time value
$spreadsheet->getActiveSheet()->setCellValue(
    'A6',
    $excelDateValue
);
// Set the number format mask so that the excel timestamp will be displayed as a human-readable date/time
$spreadsheet->getActiveSheet()->getStyle('A6')
    ->getNumberFormat()
    ->setFormatCode(
        \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME
    );
0 sẽ trả về toàn bộ bảng tính;
// Get the current date/time and convert to an Excel date/time
$dateTimeNow = time();
$excelDateValue = \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel( $dateTimeNow );
// Set cell A6 with the Excel date/time value
$spreadsheet->getActiveSheet()->setCellValue(
    'A6',
    $excelDateValue
);
// Set the number format mask so that the excel timestamp will be displayed as a human-readable date/time
$spreadsheet->getActiveSheet()->getStyle('A6')
    ->getNumberFormat()
    ->setFormatCode(
        \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME
    );
1 sẽ trả về một phạm vi hoặc ô được chỉ định; trong khi
// Get the current date/time and convert to an Excel date/time
$dateTimeNow = time();
$excelDateValue = \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel( $dateTimeNow );
// Set cell A6 with the Excel date/time value
$spreadsheet->getActiveSheet()->setCellValue(
    'A6',
    $excelDateValue
);
// Set the number format mask so that the excel timestamp will be displayed as a human-readable date/time
$spreadsheet->getActiveSheet()->getStyle('A6')
    ->getNumberFormat()
    ->setFormatCode(
        \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME
    );
2 sẽ trả về các ô trong một
// Get the current date/time and convert to an Excel date/time
$dateTimeNow = time();
$excelDateValue = \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel( $dateTimeNow );
// Set cell A6 with the Excel date/time value
$spreadsheet->getActiveSheet()->setCellValue(
    'A6',
    $excelDateValue
);
// Set the number format mask so that the excel timestamp will be displayed as a human-readable date/time
$spreadsheet->getActiveSheet()->getStyle('A6')
    ->getNumberFormat()
    ->setFormatCode(
        \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME
    );
6 được xác định.

Vòng lặp qua các tế bào

Vòng lặp qua các tế bào bằng cách sử dụng trình lặp

Cách dễ nhất để lặp các ô là bằng cách sử dụng trình lặp. Sử dụng trình lặp lại, người ta có thể sử dụng foreach để lặp lại bảng tính, hàng trong một bảng tính và các ô trong một hàng.

Dưới đây là một ví dụ trong đó chúng tôi đọc tất cả các giá trị trong bảng tính và hiển thị chúng trong bảng.

// Set cell A1 with a string value
$spreadsheet->getActiveSheet()->setCellValue('A1', 'PhpSpreadsheet');

// Set cell A2 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A2', 12345.6789);

// Set cell A3 with a boolean value
$spreadsheet->getActiveSheet()->setCellValue('A3', TRUE);

// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);
0

Lưu ý rằng chúng tôi đã đặt

// Get the current date/time and convert to an Excel date/time
$dateTimeNow = time();
$excelDateValue = \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel( $dateTimeNow );
// Set cell A6 with the Excel date/time value
$spreadsheet->getActiveSheet()->setCellValue(
    'A6',
    $excelDateValue
);
// Set the number format mask so that the excel timestamp will be displayed as a human-readable date/time
$spreadsheet->getActiveSheet()->getStyle('A6')
    ->getNumberFormat()
    ->setFormatCode(
        \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME
    );
7 của Cell Iterator thành FALSE. Điều này làm cho vòng lặp lặp tất cả các ô trong phạm vi bảng tính, ngay cả khi chúng chưa được đặt.

Trình lặp ô sẽ trả về

// Get the current date/time and convert to an Excel date/time
$dateTimeNow = time();
$excelDateValue = \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel( $dateTimeNow );
// Set cell A6 with the Excel date/time value
$spreadsheet->getActiveSheet()->setCellValue(
    'A6',
    $excelDateValue
);
// Set the number format mask so that the excel timestamp will be displayed as a human-readable date/time
$spreadsheet->getActiveSheet()->getStyle('A6')
    ->getNumberFormat()
    ->setFormatCode(
        \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME
    );
8 vì giá trị ô nếu nó không được đặt trong bảng tính. Đặt
// Get the current date/time and convert to an Excel date/time
$dateTimeNow = time();
$excelDateValue = \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel( $dateTimeNow );
// Set cell A6 with the Excel date/time value
$spreadsheet->getActiveSheet()->setCellValue(
    'A6',
    $excelDateValue
);
// Set the number format mask so that the excel timestamp will be displayed as a human-readable date/time
$spreadsheet->getActiveSheet()->getStyle('A6')
    ->getNumberFormat()
    ->setFormatCode(
        \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME
    );
7 của Bộ lặp ô thành
// Set cell A8 with a numeric value, but tell PhpSpreadsheet it should be treated as a string
$spreadsheet->getActiveSheet()->setCellValueExplicit(
    'A8',
    "01513789642",
    \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING
);
0 sẽ lặp lại tất cả các ô trong bảng tính có thể có sẵn tại thời điểm đó. Điều này sẽ tạo ra các ô mới nếu được yêu cầu và tăng sử dụng bộ nhớ! Chỉ sử dụng nó nếu nó được dự định để lặp tất cả các ô có thể có sẵn.

Vòng lặp qua các ô bằng cách sử dụng các chỉ mục

Người ta có thể sử dụng khả năng truy cập các giá trị ô theo chỉ mục cột và hàng như

// Set cell A8 with a numeric value, but tell PhpSpreadsheet it should be treated as a string
$spreadsheet->getActiveSheet()->setCellValueExplicit(
    'A8',
    "01513789642",
    \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING
);
1 thay vì
// Set cell A8 with a numeric value, but tell PhpSpreadsheet it should be treated as a string
$spreadsheet->getActiveSheet()->setCellValueExplicit(
    'A8',
    "01513789642",
    \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING
);
2 để đọc và ghi các giá trị ô trong các vòng lặp.

LƯU Ý: Trong chỉ mục cột và chỉ mục hàng của PHPSPREADSHEET là dựa trên 1. Điều đó có nghĩa là

// Set cell A8 with a numeric value, but tell PhpSpreadsheet it should be treated as a string
$spreadsheet->getActiveSheet()->setCellValueExplicit(
    'A8',
    "01513789642",
    \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING
);
2 ~
// Set cell A8 with a numeric value, but tell PhpSpreadsheet it should be treated as a string
$spreadsheet->getActiveSheet()->setCellValueExplicit(
    'A8',
    "01513789642",
    \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING
);
1
In PhpSpreadsheet column index and row index are 1-based. That means
// Set cell A8 with a numeric value, but tell PhpSpreadsheet it should be treated as a string
$spreadsheet->getActiveSheet()->setCellValueExplicit(
    'A8',
    "01513789642",
    \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING
);
2 ~
// Set cell A8 with a numeric value, but tell PhpSpreadsheet it should be treated as a string
$spreadsheet->getActiveSheet()->setCellValueExplicit(
    'A8',
    "01513789642",
    \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING
);
1

Dưới đây là một ví dụ trong đó chúng tôi đọc tất cả các giá trị trong bảng tính và hiển thị chúng trong bảng.

// Set cell A1 with a string value
$spreadsheet->getActiveSheet()->setCellValue('A1', 'PhpSpreadsheet');

// Set cell A2 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A2', 12345.6789);

// Set cell A3 with a boolean value
$spreadsheet->getActiveSheet()->setCellValue('A3', TRUE);

// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);
1

Lưu ý rằng chúng tôi đã đặt

// Get the current date/time and convert to an Excel date/time
$dateTimeNow = time();
$excelDateValue = \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel( $dateTimeNow );
// Set cell A6 with the Excel date/time value
$spreadsheet->getActiveSheet()->setCellValue(
    'A6',
    $excelDateValue
);
// Set the number format mask so that the excel timestamp will be displayed as a human-readable date/time
$spreadsheet->getActiveSheet()->getStyle('A6')
    ->getNumberFormat()
    ->setFormatCode(
        \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME
    );
7 của Cell Iterator thành FALSE. Điều này làm cho vòng lặp lặp tất cả các ô trong phạm vi bảng tính, ngay cả khi chúng chưa được đặt.

// Set cell A1 with a string value
$spreadsheet->getActiveSheet()->setCellValue('A1', 'PhpSpreadsheet');

// Set cell A2 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A2', 12345.6789);

// Set cell A3 with a boolean value
$spreadsheet->getActiveSheet()->setCellValue('A3', TRUE);

// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);
2

Trình lặp ô sẽ trả về

// Get the current date/time and convert to an Excel date/time
$dateTimeNow = time();
$excelDateValue = \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel( $dateTimeNow );
// Set cell A6 with the Excel date/time value
$spreadsheet->getActiveSheet()->setCellValue(
    'A6',
    $excelDateValue
);
// Set the number format mask so that the excel timestamp will be displayed as a human-readable date/time
$spreadsheet->getActiveSheet()->getStyle('A6')
    ->getNumberFormat()
    ->setFormatCode(
        \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME
    );
8 vì giá trị ô nếu nó không được đặt trong bảng tính. Đặt
// Get the current date/time and convert to an Excel date/time
$dateTimeNow = time();
$excelDateValue = \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel( $dateTimeNow );
// Set cell A6 with the Excel date/time value
$spreadsheet->getActiveSheet()->setCellValue(
    'A6',
    $excelDateValue
);
// Set the number format mask so that the excel timestamp will be displayed as a human-readable date/time
$spreadsheet->getActiveSheet()->getStyle('A6')
    ->getNumberFormat()
    ->setFormatCode(
        \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME
    );
7 của Bộ lặp ô thành
// Set cell A8 with a numeric value, but tell PhpSpreadsheet it should be treated as a string
$spreadsheet->getActiveSheet()->setCellValueExplicit(
    'A8',
    "01513789642",
    \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING
);
0 sẽ lặp lại tất cả các ô trong bảng tính có thể có sẵn tại thời điểm đó. Điều này sẽ tạo ra các ô mới nếu được yêu cầu và tăng sử dụng bộ nhớ! Chỉ sử dụng nó nếu nó được dự định để lặp tất cả các ô có thể có sẵn.

Vòng lặp qua các ô bằng cách sử dụng các chỉ mục

Người ta có thể sử dụng khả năng truy cập các giá trị ô theo chỉ mục cột và hàng như

// Set cell A8 with a numeric value, but tell PhpSpreadsheet it should be treated as a string
$spreadsheet->getActiveSheet()->setCellValueExplicit(
    'A8',
    "01513789642",
    \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING
);
1 thay vì
// Set cell A8 with a numeric value, but tell PhpSpreadsheet it should be treated as a string
$spreadsheet->getActiveSheet()->setCellValueExplicit(
    'A8',
    "01513789642",
    \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING
);
2 để đọc và ghi các giá trị ô trong các vòng lặp.

Tùy chọn, hành vi mặc định của phpspreadsheet có thể được sửa đổi, cho phép nhập dữ liệu dễ dàng hơn. Ví dụ, một lớp

// Set cell A9 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A9', 1513789642);
// Set a number format mask to display the value as 11 digits with leading zeroes
$spreadsheet->getActiveSheet()->getStyle('A9')
    ->getNumberFormat()
    ->setFormatCode(
        '00000000000'
    );
2 có sẵn. Nó tự động chuyển đổi tỷ lệ phần trăm, số ở định dạng khoa học và ngày được nhập dưới dạng chuỗi theo định dạng chính xác, cũng đặt thông tin kiểu của ô. Ví dụ sau đây cho thấy cách đặt chất kết dính giá trị trong phpspreadsheet:

// Set cell A1 with a string value
$spreadsheet->getActiveSheet()->setCellValue('A1', 'PhpSpreadsheet');

// Set cell A2 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A2', 12345.6789);

// Set cell A3 with a boolean value
$spreadsheet->getActiveSheet()->setCellValue('A3', TRUE);

// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);
3

Ngoài ra, một lớp

// Set cell A9 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A9', 1513789642);
// Set a number format mask to display the value as 11 digits with leading zeroes
$spreadsheet->getActiveSheet()->getStyle('A9')
    ->getNumberFormat()
    ->setFormatCode(
        '00000000000'
    );
3 có sẵn nếu bạn muốn lưu giữ tất cả nội dung dưới dạng chuỗi. Điều này có thể phù hợp nếu bạn đang tải một tệp chứa các giá trị có thể được hiểu là số (ví dụ: các số có dấu hiệu hàng đầu như số điện thoại quốc tế như
// Set cell A9 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A9', 1513789642);
// Set a number format mask to display the value as 11 digits with leading zeroes
$spreadsheet->getActiveSheet()->getStyle('A9')
    ->getNumberFormat()
    ->setFormatCode(
        '00000000000'
    );
4), nhưng nên được giữ lại dưới dạng chuỗi (số điện thoại phi quốc tế với số 0 là đã được duy trì dưới dạng chuỗi).

Theo mặc định, StringValuebinder sẽ chuyển bất kỳ kiểu dữ liệu nào được chuyển cho nó thành một chuỗi. Tuy nhiên, có một số cài đặt cho phép bạn chỉ định rằng một số kiểu dữ liệu nhất định không nên được chuyển vào chuỗi, nhưng còn lại "như là":

// Set cell A1 with a string value
$spreadsheet->getActiveSheet()->setCellValue('A1', 'PhpSpreadsheet');

// Set cell A2 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A2', 12345.6789);

// Set cell A3 with a boolean value
$spreadsheet->getActiveSheet()->setCellValue('A3', TRUE);

// Set cell A4 with a formula
$spreadsheet->getActiveSheet()->setCellValue(
    'A4',
    '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
);
4

Tạo chất kết dính giá trị của riêng bạn

Tạo chất kết dính giá trị của riêng bạn tương đối đơn giản. Khi yêu cầu ràng buộc giá trị chuyên dụng hơn, bạn có thể triển khai giao diện

// Set cell A8 with a numeric value, but tell PhpSpreadsheet it should be treated as a string
$spreadsheet->getActiveSheet()->setCellValueExplicit(
    'A8',
    "01513789642",
    \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING
);
9 hoặc mở rộng các lớp
// Set cell A9 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A9', 1513789642);
// Set a number format mask to display the value as 11 digits with leading zeroes
$spreadsheet->getActiveSheet()->getStyle('A9')
    ->getNumberFormat()
    ->setFormatCode(
        '00000000000'
    );
6 hoặc
// Set cell A9 with a numeric value
$spreadsheet->getActiveSheet()->setCellValue('A9', 1513789642);
// Set a number format mask to display the value as 11 digits with leading zeroes
$spreadsheet->getActiveSheet()->getStyle('A9')
    ->getNumberFormat()
    ->setFormatCode(
        '00000000000'
    );
2 hiện có.