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');
}
Related
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
How do I find out how many rows I have loaded using PHPSpreadsheet\Reader\Xlsx::load() method?
I cannot find methods (or properties) for getting row count in Spreadsheet or Worksheet classes either.
BTW I am using following code:
$filename = 'test.xlsx';
$inputFileType = \PhpOffice\PhpSpreadsheet\IOFactory::identify($filename);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader->setReadDataOnly(true);
$reader->setLoadSheetsOnly($sheet);
$this->spreadsheet = $reader->load($filename);
$this->worksheet = $this->spreadsheet->getActiveSheet();
Using the worksheet's getHighestRow() method
$highestRow = $this->spreadsheet->getActiveSheet()->getHighestRow();
or getHighestDataRow() if you're only interested in rows where cells contain data and not any blank rows at the end of the worksheet
Mission: I need add 2 .csv files to separate sheets
Problem: Second import removes second created sheet and puts information on first sheet
$inputFileType = 'CSV';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setDelimiter(';');
$objPHPExcel = $objReader->load('fail1.csv');
$objPHPExcel->getActiveSheet()->setTitle('laoseis');
//teine leht
$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex(1);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setDelimiter(';');
$objPHPExcel->setActiveSheetIndex(1);
$objPHPExcel = $objReader->load('fail2.csv');
$date = new DateTime($_GET['startDate']);
$objPHPExcel->getActiveSheet()->setTitle('Müük W'.$date->format("W").'');
How can I solve this problem?
PHPExcel doesn't load the second file to your current sheet in an existing PHPExcel object, it creates a new PHPExcel object and loads the file to that, so you're replacing the first completely when you try to load the second.
Load each csv to a separate PHPExcel instance, then copy the worksheet from the second instance to the first.
$objReader1 = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel1 = $objReader1->load('fail1.csv');
$objReader2 = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel2 = $objReader2->load('fail2.csv');
$objPHPExcel2->getActiveSheet()->setTitle('Worksheet 2');
$objPHPExcel1->addExternalSheet($objPHPExcel2->getActiveSheet());
Now $objPHPExcel1 has both worksheets, and you can save that
I created a PHPWriter through PHPExcel and was able to write to my excel file. now after writing to my excel file i want to retrieve the value from cell C9 and display it to screen. What am i doing wrong ? . How can i display the value of C9 ! Thanks
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcelReader = $objReader->load('test.xlsx');
$objPHPExcelReader->setActiveSheetIndex(1);
$objPHPExcel->getActiveSheet()->getCell('C9')->getValue();
$objReader = new PHPExcel_Reader_Excel2007($objPHPExcel);
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.