S
S
svetlov972018-04-20 00:02:55
PHP
svetlov97, 2018-04-20 00:02:55

How to create xls/xlsx using php?

You need to use php to create an excel table. No styles, functions, etc. The simplest board. I looked at the phpSpeedSheet library, but I don't have enough knowledge to understand how to use it. Are there any other libraries that would just include via require and work.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander Sharomet, 2018-04-20
@svetlov97

https://github.com/PHPOffice/PHPExcel
I used this fiddle like this:

$data // массив с данными (имя, email и т п)
$fullName = 		isset($data['FullName']) ? $data['FullName'] : '';
$accommodation =        isset($data['Accommodation']) ? $data['Accommodation'] : '';
$email = 			isset($data['Email']) ? $data['Email'] : '';
$phone = 			isset($data['Phone']) ? $data['Phone'] : '';
$date = 			date('Y-m-d, H:i:s');

require_once 'app/PHPExcel.php'; //Подключаем библиотеку

$fileType = 'Excel5';
$fileName = 'название -_файла.xls';

$objReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objReader->load($fileName);
$objPHPExcel->setActiveSheetIndex(0);

//Получаем последний номер строки
$num_rows = $objPHPExcel->getActiveSheet()->getHighestRow() + 1;

//Добавляем данные
$objPHPExcel->getActiveSheet()->setCellValue('A'.$num_rows, $fullName); //A,B,C,D - столбцы
$objPHPExcel->getActiveSheet()->setCellValue('B'.$num_rows, $accommodation);
$objPHPExcel->getActiveSheet()->setCellValue('C'.$num_rows, $email);
$objPHPExcel->getActiveSheet()->setCellValue('D.$num_rows, $phone);
$objPHPExcel->getActiveSheet()->setCellValue('E'.$num_rows, $date);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
$objWriter->save($fileName);

S
Sergey, 2018-04-20
@gangstarcj

She is quite easy to learn. Easier already nowhere.
Start small https://phpspreadsheet.readthedocs.io/en/develop/#... See the example, try it out

L
Lone Ice, 2018-04-20
@daemonhk

What did PHPExcel not please?

S
Sergey Shchuchkin, 2020-09-05
@shuchkin

SimpleXLSXGen

$books = [
  ['ISBN', 'title', 'author', 'publisher', 'ctry' ],
  [618260307, 'The Hobbit', 'J. R. R. Tolkien', 'Houghton Mifflin', 'USA'],
  [908606664, 'Slinky Malinki', 'Lynley Dodd', 'Mallinson Rendel', 'NZ']
];
$xlsx = SimpleXLSXGen::fromArray( $books );
$xlsx->saveAs('books.xlsx');
// $xlsx->downloadAs('books.xlsx');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question