PHPExcel Remove cell (column) removeColumn() not working - php

i created export PHPExcel, but here i want to remove cell "A" here is piece of my code
$spareparts = $this->spareparts_m->getDataSpareparts();
if ($spareparts) {
foreach ($spareparts as $key => $sparepart)
{
$rownum = $key + $row_geser;
$this->excel->getActiveSheet()->getStyle('A' . $rownum.':W'. $rownum)->getFont()->setSize(10);
if(isset($flip[1])){
$this->excel->getActiveSheet()->setCellValue('A' . $rownum, $sparepart['part_no']);
}else{
$this->excel->getActiveSheet()->removeColumn("A");
}
/* Blah..blah.blah */
but here $this->excel->getActiveSheet()->removeColumn("A");
not working and my excel output became like this screenshot output excel
any solution please ?
Thank you

If you want to remove the column A start it with column B.
something like this...
$this->excel->getActiveSheet()->setCellValue('A', 'Sample');
To: $this->excel->getActiveSheet()->setCellValue('B', 'Sample');
then change the correspond letter to it. you need to set it manually + the row number added from your foreach function.

Related

Laravel excel validate two columns' combination is not duplicate

I have a requirement wherein I need to ensure that Excel file being uploaded by user does not have duplicate rows w.r.t. 2 particular columns.
Example:
In the snippet below, I want to flag out that row 1 and 2 contain duplicate combination of COMPANY_CODE and CLERK_CODE:
If such duplicate combination is found I want to reject the entire file being imported and let the user know where the problem is.
Any clues?
Not sure if Maat/Laravel Excel can solve this easily. So, I went ahead and created associative array with key as concatenation of two columns which I don't want to repeat in Excel.
Then I check manually using a foreach loop that if key exists in associative array, it means there is duplicate entry in Excel.
Some Sample code for reference below:
$array = Excel::toArray(new MyExcelImport(), request()->file);
$assoc_array = array();
foreach ($array[0] as $key => $value) {
$new_key = $value['company_code'] . $value['clerk_code'];
// Presence of combination of company_code and clerk_code in the assoc_array indicates that
// there is duplicate entry in the Excel being imported. So, abort the process and report this to user.
if (array_key_exists($new_key, $assoc_array)) {
return response()->json("Combination of company_code: " .
$value['company_code'] .
" and clerk_code: " .
$value['clerk_code'] .
" is duplicate in the file being imported. Please correct same and retry upload.", 422);
}
$assoc_array[$new_key] = $value;
}
Hope this helps someone with similar needs!

How to wrap PHPExcel generated cells with a link

The cv column holds filenames of files uploaded to server.
How to wrap the cell values with a link to pull the files from server.
This is my code.
$sql = "SELECT name,email,phone,cv from applicants";
$res = $this->db->query($sql);
$exceldata="";
foreach ($res->result_array() as $row){
$exceldata[] = $row;
}
$this->excel->getActiveSheet()->fromArray($exceldata, null, 'A3');
$this->excel->getActiveSheet()->getStyle('A3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$this->excel->getActiveSheet()->getStyle('B3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$this->excel->getActiveSheet()->getStyle('C3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$this->excel->getActiveSheet()->getStyle('D3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER)
I want the cell to show the value with link pointing to the file.
Desired output of the cells:
ex. href="http://examle.com/uploads/cv/ '. cell_value .'">cellvalue
Set the cell as a hyperlink, as described in the PHPExcel Documentation and shown in the examples like 05featuredemo
$cellValue = 123;
$objPHPExcel->getActiveSheet()
->getCell('E26')
->setValue($cellValue);
$objPHPExcel->getActiveSheet()
->getCell('E26')
->getHyperlink()
->setUrl('http://examle.com/uploads/cv/' . $cellValue)
->setTooltip('Click here to access file');
Have a look at the feature demo in the PHPExcel package, there is an example of how to add a link to a cell.
Here's some mockup code for this:
$yourcell = "A3";
$objPHPExcel->getActiveSheet()->setCellValue($yourcell, 'www.example.net');
$objPHPExcel->getActiveSheet()->getCell($yourcell)->getHyperlink()->setUrl('http://www.example.net');
$objPHPExcel->getActiveSheet()->getCell($yourcell)->getHyperlink()->setTooltip('Navigate to website');

Search a cell by string in PHPExcel

I want to know if it's possible to get a cell by its name in an xls document, I mean Ii have this info in a excel file:
Normally to get the coordinate of the cell with the value "ASUS"
$objPHPExcel->getActiveSheet()->getCell('B3')->getValue();
my problem is that users send my this excel file, and sometimes the rows are in disorder, e.g the row B3 sometimes appear in a different row like "B6" or "B7" or "B5", how I can get the cell "ASUS" getting by cell name "Modelo"
There is nothing built-in to PHPExcel to do a search, but using the following example, it can do well for your problem.
$foundInCells = array();
$searchTerm = 'ASUS';
foreach ($objPHPExcel->getWorksheetIterator() as $CurrentWorksheet) {
$ws = $CurrentWorksheet->getTitle();
foreach ($CurrentWorksheet->getRowIterator() as $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(true);
foreach ($cellIterator as $cell) {
if ($cell->getValue() == $searchTerm) {
$foundInCells[] = $ws . '!' . $cell->getCoordinate();
}
}
}
}
var_dump($foundInCells);
You'd have to loop through the entire file to find it just like searching a value in a 2D array.
Take a look at this answer

PHPEXCEL rows layout

I would like to write into a csv file using PHPEXCEL in the following format. The class in bold and a space after every class group.
But i have not been able to find a way to do it.
Here is what I could do.
I generate the data from an sql and loop thought it. Here is my code
$sql="SELECT * FROM table ORDER BY typeclass";
$query=mysql_query($sql);
$check='';
$i=1;
while($row=mysql_fetch_assoc($query))
{
if($row['class']!=$check)
{
$objPHPExcel->getActiveSheet()->setCellValue('A'.$i,$row['Class']);
$check=$row['class'];
}
$objPHPExcel->getActiveSheet()->setCellValue('B' .$i,$row['Name']);
$objPHPExcel->getActiveSheet()->setCellValue('C' .$i,$row['score']);
$i++;
}
Can anyone help me get the desired output.
How to format a cell bold:
$styleArray = array('font' => array('bold' => true));
$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);
You can insert a new Row after inserting the class name.
if($row['class']!=$check)
{
$objPHPExcel->getActiveSheet()->setCellValue('A'.$i,$row['Class']);
$check=$row['class'];
// Merge the cells with the class name
$objPHPExcel->getActiveSheet()->mergeCells('A'.$i.':B'.$i);
$i++; // Next Row
}
Then you can put the name and score into column A and B:
$objPHPExcel->getActiveSheet()->setCellValue('A' .$i,$row['Name']);
$objPHPExcel->getActiveSheet()->setCellValue('B' .$i,$row['score']);
That should do all the trick. For further questions using PHPEXCEL youre able to contact me via email.
By the way, poor Simon :(

Searching a CSV With PHP

I have a large CSV file. The first column contains the name of a processor. The second, the processor's benchmark score. EG:
Intel Pentium Dual T3400 # 2.16GHz,1322
Using a PHP script, I would like to search the first column for a string (EG. Pentium Dual T3400), and (assuming that there is only one result per search, else return an error message) create a variable containing the value of the second column.
I don't know if this will help, but I was sort of hoping it would look a little like this:
$cpuscore = csvsearch(CSV_file,query_string,column#_to_search,column#_to_return)
Where $cpuscore would contain the score of the processor name that matches the search query.
Feel free to suggest something that would produce similar results. I have MySQL, but I don't have the permissions to import tables from CSV.
You can use the php function fgetcsv(), http://php.net/manual/en/function.fgetcsv.php to traverse the csv file row by row. For instance:
$ch = fopen($path_to_file, "r");
$found = '';
/* If your csv file's first row contains Column Description you can use this to remove the first row in the while */
$header_row = fgetcsv($ch);
/* This will loop through all the rows until it reaches the end */
while(($row = fgetcsv($ch)) !== FALSE) {
/* $row is an array of columns from that row starting at 0 */
$first_column = $row[0];
/* Here you can do your search */
/* If found $found = $row[1]; */
/* Now $found will contain the 2nd column value (if found) */
}
I like to iterate through each line of a csv file and find the words i'm looking for, and compile a result from there. Here's something to get you started:
<?php
$query = "Intel Core i7 3600M"
$file = file('db.csv');
foreach($file as $value) {
if(stristr($value,$query)){
$items = explode(",", $value); echo $items[1];
};
};
?>

Categories