PHPexcel CSV master file - php

I would like to have multiple csv's and add them in a master csv file. It when I add an extra csv file in the array it throws a exception error.
Uncaught exception 'Exception' with message 'Workbook already contains a worksheet named 'Worksheet'. Rename the external sheet first.' in
Please find below my code
include'../Classes/PHPExcel.php';
include'../Classes/PHPExcel/IOFactory.php';
$filenames = array('Sheet1.csv','Sheet2.csv');
$bigExcel = new PHPExcel();
$bigExcel->removeSheetByIndex(0);
$reader = new PHPExcel_Reader_CSV();
foreach ($filenames as $filename) {
$excel = $reader->load($filename);
foreach ($excel->getAllSheets() as $sheet) {
$bigExcel->addExternalSheet($sheet);
}
foreach ($excel->getNamedRanges() as $namedRange) {
$bigExcel->addNamedRange($namedRange);
}
}
$writer = new PHPExcel_Writer_CSV($bigExcel);
$writer->save('2007-write.csv');

The problem is with the following code:
foreach ($excel->getAllSheets() as $sheet) {
$bigExcel->addExternalSheet($sheet);
}
For file 1 you add a sheet called worksheet, and then for file two you try to add a sheet with the same name.
I suggest you check if a sheetname has already been added, and if so you can use "setActiveSheetIndex" to switch to the existing sheet.
Another option is to change the name to worksheet(2) if you find one that already exists.
But since you called it CSV I dont think you can use worksheets. So in the end it should all just go in a single worksheet, so no need to keep adding new workheets. Just create one and add all the data to that one.

Related

Phpspreadsheet Converted File should have additional new Column containing same value

Following is the code in which I am using the PhpSpreadsheet library to convert xlsx file to csv. I am trying to achieve the ability that the converted file should have one extra column called ClientID which should contain the same value for all the number of rows present in that file. Please let me know is it possible to do? Any help would be highly appreciated.
use \PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use \PhpOffice\PhpSpreadsheet\Writer\Csv;
$reader = new Xlsx();
$uploaded_xls_file = $_FILES['file']['tmp_name'];
$spreadsheet = $reader->load($uploaded_xls_file);
$loadedSheetNames = $spreadsheet->getSheetNames();
$writer = new Csv($spreadsheet);
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
$writer->setSheetIndex($sheetIndex);
$writer->setPreCalculateFormulas(false);
$savingpath = $loadedSheetName.'.csv';
$writer->save($savingpath);
//$writer->save($loadedSheetName.'.csv');
}

Generate .xlsx file using fromArray for a big amount of data

I need to write in a .xlsx file about 111.100 rows, using fromArray() but I have a strange error
I use phpspreadsheet library
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$columnLetter = 'A';
foreach ($columnNames as $columnName) {
// Allow to access AA column if needed and more
$sheet->setCellValue($columnLetter.'1', $columnName);
$columnLetter++;
}
$i = 2; // Beginning row for active sheet
$columnLetter = 'A';
foreach ($columnValues as $columnValue) {
$sheet->fromArray(array_values($columnValue), NULL, $columnLetter.$i);
$i++;
$columnLetter++;
}
// Create your Office 2007 Excel (XLSX Format)
$writer = new Xlsx($spreadsheet);
// In this case, we want to write the file in the public directory
// e.g /var/www/project/public/my_first_excel_symfony4.xlsx
$excelFilepath = $directory . '/'.$filename.'.xlsx';
// Create the file
$writer->save($excelFilepath);
And I get the exception :
message: "Invalid cell coordinate AAAA18272"
#code: 0
#file: "./vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/Coordinate.php"
Can you help me please ?
Excel pages are limited. The limit is huge but still limited. This is a correct filter so you can't write if there is no space for it.
Anyway you shouldnt use excel pages for such a big amount of data, you can try fragmenting it into smaller pieces, but databases should be the way to manipulate such amount of information

Writing data read from Excel with Spout fails

I'm reading an excelsheet with Spout and directly writing this data to a sheet results in a error.
Trying to add a value with an unsupported type: object in vendor/box/spout/src/Spout/Writer/XLSX/Internal/Worksheet.php on line 231
Does anybody have the solution?
$reader = ReaderFactory::create(Type::XLSX);
$reader->open($sFileNameExcel);
$writer = WriterFactory::create(Type::XLSX);
$writer->openToFile($sWritePath.$sWriteFileName);
foreach ($reader->getSheetIterator() as $sheet)
{
if ($sheet->getName()=='mysheet')
{
foreach ($sheet->getRowIterator() as $row)
{
$writer->addRow($row);
}
}
}
$writer->close();
$reader->close();
My data consists of string, integer and double fields.
The sheet you're reading probably contains a date. The writer can't interpret dates though, hence the error.
Try configuring your reader this way: $reader->setShouldFormatDates(true); before calling open

php excel trying to add sheets with data dynamically in one workbook

I am trying to write the data to the multiple sheets in one excel file using phpExcel. I am getting data as array and i am looping it in that i am creating and writing the data .Problem is that it is creating the multiple sheets but it is writing all the data in one sheet. Below is my code:
function getcsv($data, $cirval, $opval)
{
global $objPHPExcel;
$myWorkSheet = new PHPExcel_Worksheet($objPHPExcel, $cirval);
$objPHPExcel->addSheet($myWorkSheet);
$objPHPExcel->setActiveSheetIndex(1);
$sheet = $objPHPExcel->getActiveSheet();
$sheet->setCellValue('A1', 'Denomination');
$sheet->setCellValue('B1', 'Validity');
$sheet->setCellValue('C1', 'Data Plan');
$rowcounter = 1;
foreach($data as $v1){
$rowcounter++;
$sheet->setCellValue('A' . $rowcounter, $v1['Denomination']);
$sheet->setCellValue('B' . $rowcounter, $v1['Validity']);
$sheet->setCellValue('C' . $rowcounter, $v1['Data Plan']);
}
}
Remove the line
$objPHPExcel->setActiveSheetIndex(1);
When you add a new worksheet using addSheet(), then PHPExcel automatically makes that the current active sheet.... you're explicitly overriding that behaviour and resetting the active sheet so that it is always sheet id #1, so it will always write everything to sheet id #1 no matter how many new sheets you add

convert an EXCEL file to CSV file in PHP [duplicate]

This question already has answers here:
How to convert Excel XLS to CSV using PHP
(5 answers)
Closed 7 years ago.
I want to convert Excel files (.xls) into CSV file (.csv) with a script PHP ?
I tried many codes but it didn't work like this one !
No errors appear but it wont work Any idea or any other lines of codes that I can try ?
<?php
echo("it works");
require_once '../batchs/Classes/PHPExcel/IOFactory.php';
$inputFileType = 'Excel5';
$inputFileName = '../public/aixstream/stock.xls';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcelReader = $objReader->load($inputFileName);
$loadedSheetNames = $objPHPExcelReader->getSheetNames();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcelReader, 'CSV');
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
$objWriter->setSheetIndex($sheetIndex);
$objWriter->save($loadedSheetName.'.csv');
}
?>
Thank you
As already stated in this answer, you can use the PHP-ExcelReader function to read the xls file. After that, you may easily convert it into CSV (or any other other format) using the following code, also available here.
Reading the xls file
//You will obviously need to import the function
//by downloading the file from the link above.
$reader=new Spreadsheet_Excel_Reader(); //Instantiate the function
$reader->setUTFEncoder('iconv'); // Set Encoder
$reader->setOutputEncoding('UTF-8'); // Set Output Encoding Type
$reader->read($filename); // Read the xls file
Data Output
/***
* Information about sheets is stored in boundsheets variable.
* This code displays each sheet's name.
***/
foreach ($reader->boundsheets as $k=>$sheet) //Run loop for all sheets in the file
{
echo "\n$k: $sheet";
}
//Now just save the data in the array as csv
/***
* Data of the sheets is stored in sheets variable.
* For every sheet, a two dimensional array holding table is created.
* This code saves all data to CSV file.
***/
foreach($reader->sheets as $k=>$data) // Run loop for all items.
{
echo "\n\n ".$reader->boundsheets[$k]."\n\n"; //Print Title
foreach($data['cells'] as $row) // Loop for all items
{
foreach($row as $cell) // Loop for every cell
{
echo "$cell".","; //Add a comma after each value
}
}
}
//It works! :D

Categories