I am using simplexlsx to read xlsx sheet,
I have multiple tabs in my xlsx file, When I am trying to read data, its shows data only for first tab,
for better understanding i have attach print screen.
Try This,
$this->load->library('simplexlsx');
$xlsx = new SimpleXLSX( $file_path );
$data['csv_data'] = $xlsx->rows();
It is because when you select something from an excel file it performs that action with the active sheet which is by default is the first one.
You need to change that to the required sheet
Use the following code to do that
$objPHPExcel->setActiveSheetIndex($count); // Use no of the sheet you want to select -1 as count
Related
I need to create an excel table with help of php. Is that possible? The php code below creates and write to an excel file. I would like to create an excel table with the data, see picture below. I'm using PhpSpreadsheet (sorry I forgot to say that)
Edit: The code below uses library PhpSpreadsheet to ceate a excel file with some content.
I want to create an excel table:
The code works and create the content as the second picture below shows. This is NOT an excel table, just plain text in cells.
But that is not what I want. I want to be able to create the content as the first picture below shows. This is an excel table. When you create an excel table by hand you can choose colrs etc. I do not care about the colors. Excel add the column name and push the content down.
What I have tried is to add: $sheet->setAutoFilter('A1:B5'); to the code, but this does not create an excel table as shown in the third picture below.
So the question is: What do I need to add to the code above to be able to create the content as shown in the first picture below
The fourth picture below shows how to crate an excel table in excel (and this is what I want the php code to do)
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'A1');
$sheet->setCellValue('A2', 'A2');
$sheet->setCellValue('A3', 'A3');
$sheet->setCellValue('A4', 'A4');
$sheet->setCellValue('A5', 'A5');
//$sheet->setCellValue('A6', 'A6');
$sheet->setCellValue('B1', 'B1');
$sheet->setCellValue('B2', 'B2');
$sheet->setCellValue('B3', 'B3');
$sheet->setCellValue('B4', 'B4');
$sheet->setCellValue('B5', 'B5');
$writer = new Xlsx($spreadsheet);
$writer->save('CreateExcelTable.xlsx');
The picture below show the tableI would like to create
With the code above this is created:
With the code added:
$sheet->setAutoFilter('A1:B5');
The picture below show what is created.It is not a table
Insert/Table is simply a GUI "shortcut" method for styling and setting autofilters against a block of cells. Both of these can be done as individual tasks using PHPSpreadSheet, but the library does not provide a "shortcut" way of doing this with a single method call.
Take a look at section of the Developer documentation.
I have a similar problem.
My Excel Template had some array formulas that refer to Named Data Tables,
(instead of writing in the array formula A2:A150 i would simply write NameOfMyTable[NameOfColumn])
When i inserted some data in my template using PhpSpreadSheet and downloaded the produced excel file, Each instance of NameOfMyTable[NameOfColumn] was replaced with REF!
Instead of searching how make or preserve Named Data Tables while using PHPSpreasheet. I simply converted all my Named tables back to simple ranges. (you can easily do this conversion through TableTools in excel without having to change every formula in your Excel Template).
I have a code I wrote, use it if you want
look at the code
using
$excelExport = new ExcelExport(['name' => 'dılo'], 'file');
$excelExport->download();
shortest excel creation example with php
basic example
<?php
$file="demo.xls";
$test="<table ><tr><td>Cell 1</td><td>Cell 2</td></tr></table>";
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$file");
echo $test;
?>
I am using PHPExcel for editing a large XLSX file (~230kb, 20 sheets). They way I do it yet is:
load the file (create the PHPExcel object)
do the changes on the PHPExcel object
overwrite the XLSX file with a new one
This is very slow (~6 seconds), although there are only small changes (some cells in just one worksheet).
Is there a way to save just the changes in the file? Or if not, with another library?
You can try to do it with Spout: https://github.com/box/spout. It should not take more than a second and your code should look like this:
$reader = ReaderFactory::create(Type::XLSX);
$reader->open('path/to/file/to/read.xlsx');
$writer = WriterFactory::create(Type::XLSX);
$writer->openToFile('path/to/file/to/write.xlsx'); // needs to be different than the one read for now
while ($reader->hasNextSheet()) {
$reader->nextSheet();
while ($reader->hasNextRow()) {
$row = $reader->nextRow();
// change the row here if needed
$writer->addRow($row);
}
}
$reader->close();
$writer->close();
// And at the end, you can replace the old file with the new one:
rename('path/to/file/to/read.xlsx', 'path/to/file/to/write.xlsx');
Hope that helps!
I wanted to append data in the pre formated excel sheet that is basically header footer in the excel sheet I wanted to append the contents. And will create many files dynamically.
A simple workaround is:
create a html table with the formatting you need
add values in php to the table (or generate table with php)
save file as .xls (filled with content from html table)
open file (will show formatted table in Excel)
Reason:
handling XLS files is very complex and many libraries have big limits (only available on windows servers....)
html table saved as .xls can be opened in Excel.
Thanks I have found the way PHPExcel is a good library.
In order to get PHPExcel http://www.codeplex.com/PHPExcel working with CodeIgniter, there are a few steps you must take to ensure compatibility with CodeIgniter's naming standards.
1: Class names must match the file names. PHPExcel has a few files(such as PHPExcel/IOFactory.php) that have names like PHPExcel_IOFactory. Change these names by removing the "PHPExcel_" part. These constructors in these files must be public in order for CI to access them.
$this->load->library('phpexcel');
$this->load->library('PHPExcel/iofactory');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("title")
->setDescription("description");
// Assign cell values
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'cell value here');
// Save it as an excel 2003 file
$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save("nameoffile.xls");
I have an excel sheet that is loaded with a dynamic result set of data.I need to validate the excel sheet before insert into mysql table.Validation such as Is there any duplicate entry,email validation etc.Any idea about how can validate using php.
As easy as :
parse your excel using phpexcel for example
make an array with all entries.
use array_unique to discart duplicates
then validate email fields
How do you parse the xls itself? For me the most simple solution is to parse the whole xls into an array then validate that. You can easily check for duplicates then iterate through with one foreach and validate the remaining.
You can use COM functions under PHP
$excel = new COM("excel.application") or die("Unable to instanciate excel");
//Open your file
$excel->Workbooks->Open("files/test.xls");
$Workbook = $excel->Worksheets(1); //Select the wortksheet
foreach($Workbook = $excel->Worksheets as $Worksheet)
{
//Loop each page in the book
$Worksheet->Activate; //Activate Sheet 1
for($row=0;$row<=$Worksheet->rows;$row++)
{
$row_item = $Worksheet->rows[$row];
//Hmm, i forgot the rest but you can do that ;)
}
}
You will have to read more about it, as i have never used it to open Excel sheets.
http://www.php.net/manual/en/class.com.php
Is it possible to delete rows in excel sheet using PHP ?
Only works if your PHP is hosted under Windows.
This should be a good start if PHPExcel* doesn't work.
Open
$this->excel = new COM("Excel.sheet") or $this->error("error");
$this->workbook = $this->excel->application->Workbooks->Open($path) or $this->error("error");
Close
$this->excel->application->ActiveWorkbook->Close("False");
unset($this->workbook);
unset($this->excel);
*I've never used PHPExcel, but it doesn't look bad
If it's Excel 2007 you can take a look at http://www.phpexcel.net