<?php
require_once("PHPExcel/Classes/PHPExcel.php");
require_once("PHPExcel/Classes/PHPExcel/Writer/Excel2007.php");
require_once("PHPExcel/Classes/PHPExcel/IOFactory.php");
$objPHPExcel = new PHPExcel();
$inputFileName = 'R1.xlsx';
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objPHPExcel->setActiveSheetIndex(0);
$objWriter->save('php://output');
This is my code its good to get excel Sheet1 output from R1.xlsx file to browser. But R1.xlsx contain more than one sheets how to show them by link or vertically to show sheet2 and sheet3 data?
Thanks
PS.
I tried
$objPHPExcel->setActiveSheetIndex(X); by changing X value
By default, the HTML Writer will only generate output for a single worksheet
You can specify which sheet to write by calling
$objWriter->setSheetIndex(2);
specifying the individual sheet that you want to output
But you can also tell it to generate an output for all sheets instead using
$objWriter->writeAllSheets();
before the save
You can use a for loop, in each loop, output the content of each table.
Related
Can anyone let me know if there are any other library choices available for me where I can do what I wanted.. that is open the excel template file (.xlsx or even .xlsm) in php and populate the cells and keep all the charts, pivot table, even macros, forms, buttons, etc. intact. All the php will do is just populate the cells with new data from database. In other words, using the same terminology that you have used.. please point me to the library for which I can use to "edit" workbook files.
When i try the below code.. the chart itself goes missing from the resulting file. After trying various ways.. still failed.
Thank you so much again.
<?php
date_default_timezone_set('Europe/London');
require_once 'excel/PHPExcel/IOFactory.php';
require_once 'excel/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setIncludeCharts(TRUE);
$objPHPExcel = $objReader->load('WorkingFile_Daily.xlsx');
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('B11', 4)
->setCellValue('B12', 5)
->setCellValue('B13', 6)
->setCellValue('B14', 7);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save('WorkingFile_Daily New.xlsx');
?>
I need to add the images from a windows folder to the excel sheet such that each image is placed in front of the row entry containing the name of the file.
Something like this- (the name of the file is same as the id column entry in that row)
What kind of code/language could i use to do this?
I also have this database in Mysql, is it possible to include images in excel using php.
Thanks a lot
Let's suppose you are including your phpExcel library in your project ,
include 'PHPExcel.php';
// you create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set properties
$objPHPExcel->getProperties()->setCreator("Yassine CHABLI");
$objPHPExcel->getProperties()->setLastModifiedBy("Yassine CHABLI");
$objPHPExcel->getProperties()->setTitle("make whatever you want");
$objPHPExcel->getProperties()->setSubject("whatever you want");
$objPHPExcel->getProperties()->setDescription("including images test (example)");
// Add some data
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setTitle('image example');
$Image = imagecreatefromjpeg('yassine.jpg');
$objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
$objDrawing->setName('image');
$objDrawing->setDescription('image');
$objDrawing->setImageResource($Image);
$objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
$objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
$objDrawing->setHeight(150);
$objDrawing->setCoordinates('A1');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
For more information , visit the link bellow:
https://packagist.org/packages/phpoffice/phpexcel
notice: That will create an “xlsx” formatted file because it uses
2007 excel classes. if you want “xls” format just try with 2005 class
and do not forget to change the file format to “xls” while using 2005.
Im Using the PHPExcel library to convert a uploaded .xlsx file to a html page.
The code works but the html page also has alot of empty cells in it. what can i do so PHPExcel wil not create these?
the convert
$inputFileName = $upload_dir;
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objWriter->save($basename.".html");
the result
I'm currently using PHP Excel to output an excel file in html. I was wondering if it was possible to edit the source code so that instead of outputting a table, it outputs an input or textarea instead. I'm not quite sure where to begin. I'm trying to take a look at all the classes in PHPExcel and can't find where the tables are being outputted so I can change them. If anyone can help me that would be great! Thanks
$inputFileName = "file";
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objWriter->writeAllSheets();
$objWriter->save('php://output');
Apparently it's written in /Classes/PHPExcel/Writer/HTML.php.
Maybe you could create your own writer, by extending PHPExcel_Writer_Abstract and implementing the interface IWriter?
I think it would be simpler to create your own method to be able to add all your specific needs (image, layout, etc ..). And then share it ;)
I am having a .xlsx file. In the .xlsx file there are 4 sheets "activity",
"performance", "store", "display".
I want to load only one sheet in the memory at a time and after adding data to
report write it. my code is below
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$worksheet_names = $objReader->listWorksheetNames('/tmp/ac.xlsx');
$objReader->setLoadSheetsOnly('store');
$objPHPExcel = $objReader->load('/tmp/ac.xlsx');
$objPHPExcel->setActiveSheetIndexByName('store');
$sheet = $objPHPExcel->getActiveSheet();
$max_row = $sheet->getHighestRow();
$sheet->setCellValue("A$max_row", "Data");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->setPreCalculateFormulas(false);
$objWriter->save('/tmp/ac.xlsx');
$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);
the problem is it is writing on store sheet and deleting all the other sheets.
How do i make remain all the sheets while updating 'store sheet'
is there any function which will write one sheet at a time while
retaining the other sheets.
Only what you have loaded into the PHPExcel object will be placed into your new file.
The createWriter isn't editing the file and retaining the sheets, it's just writing a fresh copy of what you pass in to the file name you give it. In this case, it will overwrite the file because it is the same file that you have just opened to read from. So, you must take some caution to grab the entire workbook first, then alter what you want (from the entire worksheet). After that, write everything to the file with the new changes.
The code below should help you out with retaining the other sheets. To edit only specific sheets just place the sheet names you want to edit in the $editable_worksheets array. I was very descriptive with the comments, so hopefully they will clarify step by step how this is done.
// Load your PHPExcel class
require_once 'classes/PHPExcel/Classes/PHPExcel.php';
// Set variables for file location and type to make code more portable and
// less memory intensive
$file = '/tmp/ac.xlsx';
$file_type = 'Excel2007';
// Open file for reading
$objReader = PHPExcel_IOFactory::createReader($file_type);
// Take all exisiting worksheets in open file and place their names into an array
$worksheet_names = $objReader->listWorksheetNames($file);
// Array of worksheet names that should be editable
$editable_worksheets = array('activity', 'store');
// You will need to load ALL worksheets if you intend on saving to the same
// file name, so we will pass setLoadSheetsOnly() the array of worksheet names
// we just created.
$objReader->setLoadSheetsOnly($worksheet_names);
// Load the file
$objPHPExcel = $objReader->load($file);
// Loop through each worksheet in $worksheet_names array
foreach($worksheet_names as $worksheet_name) {
// Only edit the worksheets with names we've allowed in
// the $editable_worksheets array
if(in_array($worksheet_name, $editable_worksheets)) {
// Take each sheet, one at a time, and set it as the active sheet
$objPHPExcel->setActiveSheetIndexByName($worksheet_name);
// Grab the sheet you just made active
$sheet = $objPHPExcel->getActiveSheet();
// Grab the highest row from the current active sheet
$max_row = $sheet->getHighestRow();
// Set the value of column "A" in the last row to the text "Data"
$sheet->setCellValue("A" . $max_row, "Data");
}
// Foreach loop will repeat until all sheets in the workbook have been looped
// through
}
// Unset variables to free up memory
unset($worksheet_names, $worksheet_name, $sheet, $max_row);
// Prepare to write a new file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $file_type);
// Tell excel not to precalculate any formulas
$objWriter->setPreCalculateFormulas(false);
// Save the file
$objWriter->save($file);
// This must be called before unsetting to prevent memory leaks
$objPHPExcel->disconnectWorksheets();
// Again, unset variables to free up memory
unset($file, $file_type, $objReader, $objPHPExcel);
You're loading only a single sheet, so the PHPExcel object in memory contains only that sheet. When you save, you're overwriting the exoisting file with the workbook in memory (not editing the original file). If you want to save with the same name, and retain all four worksheet; you need to lpoad all four worksheets.