I am done with basic import to database. I have explained in the following example where I got stuck up ...
For example,
I have an xls file named project.xls and it has 6 sheets. I have populated all 6 sheet names in dropdown. If I select sheet2 and click button, it should import sheet2 data into db and sheet3 so on.
How can I do this ...? please help me...
You can access to differents sheets of a file with PHPExcelReader this way:
$filename = "path/to/filename.xls";
$reader = new Spreadsheet_Excel_Reader(); // Your PHPEXCELREDER Class
$reader->setOutputEncoding('UTF8');
// Read XLS File
$reader->read($filename);
$sheet = 2; // Your sheet
// Then walk trough your wanted sheet:
for ($i = 2; $i <= $reader->sheets[$sheet]['numRows']; $i++) {
// Do something
}
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
I have to create an Excel file with a data sheet that will vary according to a database and a sheet containing multiple PivotTables that have their own PivotCharts.
"data.xlsx" contains a sheet with all new data.
"graph.xlsx" contains a sheet with old data and a sheet with PivotTables.
My goal is to have "graph.xlsx" containing a sheet with all new data and the sheet with PivotTables.
I found a perfect lib to do this : https://github.com/svrnm/exceldatatables
But I block on the use of it, I would open "graph.xlsx" delete its sheet named "brut data", then to add a new sheet named "brut data" initialized with the new data contained in "data.xlsx".
To do it I saw this function from ExcelWorkbook.php a Class of this lib.
public function addWorksheet(ExcelWorksheet $worksheet, $id = null, $name = null)
But I don't understand how to use it.
(I'm the author of the mentioned library)
There are two steps you need to take, to achieve your goal, the first is reading the data.xlsx. The second is writing that data into your graph.xlsx. The library is solving step two:
require_once('../vendor/autoload.php');
$dataTable = new Svrnm\ExcelDataTables\ExcelDataTable();
$in = 'graph.xlsx';
$out = 'out.xlsx';
$data = /* ... step 1 ... */
$dataTable->showHeaders()->addRows($data)->attachToFile($in, $out);
For step 1 you could leverage PHPExcel. I haven't tested it, but something similar like this:
$r = PHPExcel_IOFactory::createReader('Excel2007');
$data = $r->load($filename)->getActiveSheet()->toArray(null, true, true);
There is another option: You could unpack both graph.xlsx and data.xlsx and merge the sheets.
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
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
I have 2 excel files birds.xlsx and bees.xlsx both of which have the same number of columns and same type of column header. I've seen how PHPExcel does wonders with excel files but is there some way to combine 2 separate files into the same worksheet and saving it as a new file? The analogy that comes to mind is something like the SQL UNION command.
Something like:
// Load both spreadsheet files
$objPHPExcel1 = PHPExcel_IOFactory::load("birds.xlsx");
$objPHPExcel2 = PHPExcel_IOFactory::load("bees.xlsx");
// Find the last cell in the second spreadsheet
$findEndDataRow = $objPHPExcel2->getActiveSheet->getHighestRow();
$findEndDataColumn = $objPHPExcel2->getActiveSheet->getHighestColumn();
$findEndData = $findEndDataColumn . $findEndDataRow;
// Read all the data from second spreadsheet to a normal PHP array
// skipping the headers in row 1
$beeData = $objPHPExcel2->getActiveSheet->rangeToArray('A2:' . $findEndData);
// Identify the row in the first spreadsheet where we want to start
// adding merged bee data without overwriting any bird data
$appendStartRow = $objPHPExcel1->getActiveSheet->getHighestRow() + 1;
// Add bee data from the PHP array into the bird data
$objPHPExcel1->getActiveSheet->fromArray($beeData, null, 'A' . $appendStartRow);
// Save the spreadsheet with the merged data
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel1, 'Excel2007');
$objWriter->save(str_replace('animals.xlsx');