John Davidson

php - PhpSpreadsheet how to get data from cells?

0 comments
Message:


I'm really noob in programing...
I need PHP script to make a changes in xmls file using PhpSpreadsheet.



  1. To Data from column B "First Name" add data from column C "Second Name" and write in column "D" together

  2. From column E "SKU" where data have 9 numbers, take only 6 numbers and put to column F


My code


<?php

require 'vendor/autoload.php';


use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();

$spreadsheet = $reader->load("hello world.xlsx");

$sheet = $spreadsheet->getActiveSheet();

$sheet->setCellValue('D2', '=(B2& " " &C2)');
//how to do this in loop for all cells?

$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');


My Answer:
To get data from cells in a specific column using PhpSpreadsheet, you can use the following code snippet:

php
require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\IOFactory;

// Load the Excel spreadsheet
$spreadsheet = IOFactory::load('your_excel_file.xlsx');

// Get the active sheet
$sheet = $spreadsheet->getActiveSheet();

// Get the highest row number
$highestRow = $sheet->getHighestRow();

// Loop through each row in column B and get the data
for ($row = 1; $row <= $highestRow; $row++) {
$data = $sheet->getCell('B' . $row)->getValue();

// Do something with the data from column B
echo $data . "\n";
}


This code will load an Excel file, get the data from each cell in column B, and output it. You can modify the code to make changes to the XML file based on the data retrieved from the Excel file.

Rate this post

4 of 5 based on 2817 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us