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 :(
Related
I would like someone to help me find a solution to my two problems: I have an excel file with 2 columns (A and B)
Column A contains barcode numbers that I must assign to an order number (in column B). Below an example:
COLUMN A....................COLUMN B
barcode.........................order number
12345678*******************35098
98765432 ******************35433
45454545
66554433
98766334 *******************43221
What I am looking for is the following:
from a given order number (in a variable), I would like to know if it exists in column B, if yes, I would like to recover its bar code.
If it does not exist, I would like to add this order number in one of the empty cells in column B and then retrieve the barcode that has been assigned to it.
All this using PHPExcel ... I tried some, but I'm blocking ... If anyone could help me, I'd be very grateful to him.
Thank you
PS: A small precision, if you have to know the number of non-empty line of column A, we will say that there are 1000 lines
Here what I did. This code only check if the order number exist. But I'm not sure if this is the best way:
with this code, I know if the ordernumber exist in file.
My problem now is how to get the bar code number if the ordernumber exist in column B, or if it doesn't exist, how to add the order number in one of the empty cells and retrieve the corresponding barcode
Thanks to everyone for your help
<?php
$orderNumber = 12341;
function searchOrder ($num){
/** PHPExcel_IOFactory */
require_once 'Classes/PHPExcel/IOFactory.php';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load("barreCode_ordernumber.xlsx");
$foundInCells = array();
$ordernumber = $num;
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
foreach ($worksheet->getRowIterator() as $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
foreach ($cellIterator as $cell) {
$numligne = $cell->getRow();
$numcolonne = $cell->getColumn();
if ($cell->getValue() == $ordernumber) {
return true;
}
}
}
}
}
?>
I have a problem when creating excel files with the phpExcel library.
I want to create groups on certain lines based on the sales column (with same sales name).
I have made the file manually by using the Subtotal feature on the Data tab in Excel.
Is it possible that phpexcel has such feature?
You can see the sample file that I mean on the link / image that I uploaded.
You can group the rows like this
$objPHPExcel->getActiveSheet()->getRowDimension('5')->setOutlineLevel(1);
For more details you can visit here.
You can also go here and here for working examples
Using the PHPExcel , you can use the methods :
setOutlineLevel($level) // the level of the current row
setVisible(false) // show or hide the group
setCollapsed(true) // add collapse to group (as figure out in the picture above )
THe point here is to define group condition , example :
We suppose that the file excel is already generated with PHPExcel and
now you are going to read the file again for grouping rows .
First let's read the column C that contain name of salesman and store them inside the $salemans :
$salesmans = array();
/*
the output of $salesmans seems to be something like :
[0] ==> "Abdul Karim",
[1] ==> "Apan Total",
[2] ==> ""Ari Total
*/
The array $salesman should have distinct value , so when trying to
insert inside the array verify if the current value doesn't exist
already in the $salesmans.
Example :
$salesmans=array();
if (!in_array($currentSalesman, $salesmans))
{
$array[] = $value;
}
Here , we are going to to set the level of each row by getting the value of the current salesman with the key in the $salesmans
Caution : please try to modify this section because i'm not getting
how you manage fetch rows.I'm just making code more clear to understand easy
for ($row = 0; $row <= 10000; ++$row) {
$currentsalesman = $row['c']; //
$keylevel = array_search($currentsalesman, $salesmans);// this will return the key in $salesmans array
$objPHPExcel->getActiveSheet()
->getRowDimension($row)
->setOutlineLevel($keylevel) // set here the level .
->setVisible(false)
->setCollapsed(true);
}
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.
I am having trouble saving the excel file in mysql database, It contains enter(new line) inside a cell and symbols & superscripts as well. But it stores as plain text only.
$objReader = new PHPExcel_Reader_Excel5();
$objPHPExcel = $objReader->load($inputFileName);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
then i read some data and use
reset($sheetData);
to reset the pointer.
and again use foreach() loop, to add the data into an array and insert that array to mysql table. Does any of these steps remove pre-formatting (superscript/subscript/new line inside a cell and bold/italics)? and How can I put the data in the table exactly as in the excel?
Edit: I am using v1.8 of PHPExcel, v5.4 of PHP and MySQL v5.6
include APPPATH.'/spreadsheetreader/php-excel-reader/excel_reader2.php';
require(APPPATH.'/spreadsheetreader/SpreadsheetReader.php');
require(APPPATH.'/spreadsheetreader/SpreadsheetReader_CSV.php');
require(APPPATH.'/spreadsheetreader/SpreadsheetReader_ODS.php');
require(APPPATH.'/spreadsheetreader/SpreadsheetReader_XLS.php');
require(APPPATH.'/spreadsheetreader/SpreadsheetReader_XLSX.php');
class Dashboard extends REST_Controller
{
public function __construct()
{
parent:: __construct();
$this->load->library("PHPExcel");
}
public function dashboard_post()
{
$Reader = new SpreadsheetReader('./upload/'.$filename);
$totalSheet = count($Reader->sheets());
//print_r($totalSheet);exit;
// For Loop for all sheets
if($totalSheet>0)
{
for($i=0;$i<$totalSheet;$i++)
{
$Reader->ChangeSheet($i);
foreach ($Reader as $Row)
{
$data=array(
'your table column name'=>isset($Row[1]) ? $Row[1] : '',
'your table column name'=>isset($Row[2]) ? $Row[2] : '',
'your table column name'=>isset($Row[3]) ? $Row[3] : '',
'your table column name'=>isset($Row[4]) ? $Row[4] : '',);
}
The toArray() method is intended to provide a simple function to get the plain text from data cells in a spreadsheet; so all "formatting" of rich text (cells that contain different styles, colours, newlines and font information for different parts of the cell content) is removed to provide that plain text.
If you want to access that style information, then you need to get the data from the individual cells yourself using the cell's getValue() method; so you'll need to write your own loop to do that for all cells in the sheet; and you'll need to decide how you're going to store things like superscripts or bold/italic/underline in your database, and parse rich-text cell data accordingly
I want to upload an Excel file into our webpage, then corresponding data store it in database. And then I want to retrieve all data and display it in table format. I have one code but using that I can't upload all Excel files. Only a single format can be upload.
Below is the function. But there is some restriction.
public function check_excel($filename)
{
$path='./assets/uploads/excel/'.$filename;
$this->load->library('excel');
$inputFileType = PHPExcel_IOFactory::identify($path);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = PHPExcel_IOFactory::load($path);
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$xf[]='';
$result[]='';
$first_check='';
$var_check=0;
for ($row = 13; $row <= $highestRow; $row++)
{
$xf[$row]=$objPHPExcel->getActiveSheet()->getCell('A'.$row)->getXfIndex(); // Get sheet index value
if($row>13 && $row<16) //This block check first kpi data expand or not
{
if($xf[$row-1]==$xf[$row]) //check parent and child sheet index value same
$first_check='false';
if ($row==15)
{
if($xf[$row]==$xf[$row-1] || $xf[$row]==$xf[$row-2]) // check the grand-child sheet index value same in parent and child
$first_check='false';
else
{
$first_check='true';
$a=$row-2;
$b=$row-1;
$check_kpi=$objPHPExcel->getActiveSheet()->getCell('A'.$a)->getXfIndex();
$check_unit=$objPHPExcel->getActiveSheet()->getCell('A'.$b)->getXfIndex();
$check_sub_unit=$objPHPExcel->getActiveSheet()->getCell('A'.$row)->getXfIndex();
}
}
}
if($first_check=='true') //This block check second kpi to upto last kpi data expand or not
{
if($row>15)
{
if($var_check==1) // This block check the child data expand or not
{
if($check_unit!=$objPHPExcel->getActiveSheet()->getCell('A'.$row)->getXfIndex())
{
$result[$row]='false';
break;
}
}
if($var_check==2) // this block check the grand - child data expand or not
{
if($check_sub_unit!=$objPHPExcel->getActiveSheet()->getCell('A'.$row)->getXfIndex())
{
$result[$row]='false';
break;
}
}
if($xf[$row]!=$check_sub_unit)
{
if($xf[$row]!=$check_unit)
$var_check=1; // var_check value is one, the kpi is present
else
$var_check=2; // var_check value is two, the unit is present
}
else
$var_check=0; // var_check value is zero, the sub_unit is present
}
}
else if($first_check=='false')
{
$result[$row]='false';
break;
}
}
$return='true';
for ($row = 13; $row <= $highestRow; $row++)
{
if(!empty($result[$row]))
{
if($result[$row]=='false'){
$return='false';
break;
}
}
}
return $return;
}
It sounds like you are using a relational DB (e.g. MySQL, Postgres, etc), which uses fixed column tables.
You should probably use a Document-based DB (e.g. CouchDB, Mongo, etc). This would be the best solution.
But, if you're stuck using a relational DB, you can use an EAV model.
Here is a basic example:
Create a table for the entity (excel file): EntityID, ExcelFileName
Create a table for the attribute (column info): AttributeID, EntityID, AttributeName
Create a table for the value (excel row/column): ValueID, RowNumber, AttributeID, AttributeValue
The downside is that the AttributeValue isn't specifically typed (it's just varchar/text). You can solve this by adding a "AttributeType" to the attribute table that is then used in your code to know what type of data that column should contain. BUT, unless you know the contents/format of the Excel file in advance, you'll probably have to GUESS what the type of a column is...which isn't hard as long as the excel file isn't messed up.
If you're just displaying the data that was imported, this probably isn't a big deal.
There are other (more complex) ways to implement EAV, including one with typed columns, if you have such a need.
Have you tried PHPExcel?
They also have a codeigniter library.
And this post might interest you : how to use phpexcel to read data and insert into database?
You can use PHPExcel of course, but have a look at other data format. Using comma-separated or tab-separated values can help you to solve your problem easily. Excel can save datasheets in these simple formats. Anyway, you cannot save formulas or conditional formatting in you database Moreover, it is much faster and robust and you can import CSV files with LOAD DATA INFILE query.