After looking over the documentation for Box/Spout I do not see a way to format cells for currency.
NOTE: PhpExcel and PhpSpreadsheet are not an option.
Is there a way to format a cell?
While styling, is there a way to set the width of the cells?
This is my code so far.
It works, but is lacking currency formatting and width:
$writer = WriterFactory::create(Type::XLSX); // for XLSX files
$writer->openToFile($tmpfile); // write data to a file or to a PHP stream
$writer_sheet = $writer->getCurrentSheet();
$writer_sheet->setName($invoiceSheetName);
$headerStyle = (new StyleBuilder())
->setFontBold()
->setFontUnderline()
->build();
// add header with style
$writer->addRowWithStyle($header, $headerStyle);
$writer->addRows($resultsArray); // add multiple rows at a time
$writer->close();
$excelOutput = file_get_contents($tmpfile);
Documentation:
Spout Documentation
Help is greatly appreciated.
Decided to implement my own implementation with unzipping the xlsx file and modifying the correct xml file.
Related
I am trying to create an Excel spreadsheet using PHPExcel.
I would like to validate a specific column. I know how to validate a cell, but I can't seem to find a method for validating a column. Can I do better than manually looping through all cells of a column?
require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';<br/>
$filename='test.xlsx';<br/>
if(file_exists($filename)){<br/>
unlink($filename);<br/>
}
$objExcel = new PHPExcel();<br/>
$objWriter = new PHPExcel_Writer_Excel5($objExcel);<br/>
$objExcel->setActiveSheetIndex(0);<br/>
$objActSheet = $objExcel->getActiveSheet();<br/>
$objValidation = $objActSheet->getCell("A1")->getDataValidation(); //这一句为要设置数据有效性的单元格<br/>
$objValidation->setType(PHPExcel_Cell_DataValidation::TYPE_LIST)<br/>
->setErrorStyle(PHPExcel_Cell_DataValidation::STYLE_INFORMATION)<br/>
->setAllowBlank(false)<br/>
->setShowInputMessage(true)<br/>
->setShowErrorMessage(true)<br/>
->setShowDropDown(true)<br/>
->setErrorTitle('输入的值有误')<br/>
->setError('您输入的值不在下拉框列表内.')<br/>
->setFormula1('"列表项1,列表项2,列表项3"')<br/>
->setPromptTitle('设备类型');<br/>
$objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');<br/>
$objWriter->save('test.xlsx');<br/>
Yes, you can do better!
For example if you want to apply validation on cells A1:A10, this will do the job:
$objActSheet->setDataValidation("A1:A10", $objValidation);
Sources:
PHPExcel Github issue
Relevant SO answer
PhpSpreadsheet Update (after 2019)
Since PHPExcel was archived in 2019 and should not be used anymore (see PHPExcel Repository), I'll provide an update for people finding this question while using the official PHPExcel successor PhpSpreadsheet.
You'd now create a validation and then apply it to a whole cell range like that:
$validation = $productSheet
->getCell("A1")
->getDataValidation()
->setType(DataValidation::TYPE_LIST)
->setFormula1('"列表项1,列表项2,列表项3"')
->setAllowBlank(false)
->setShowDropDown(true)
->setShowInputMessage(true)
->setPromptTitle('设备类型')
->setShowErrorMessage(true)
->setErrorStyle(DataValidation::STYLE_INFORMATION)
->setErrorTitle('输入的值有误')
->setError('您输入的值不在下拉框列表内.');
// Apply the validation to the rest of column A
$validation->setSqref('A2:A1048576');
Link to the official documentation
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
HI i am exporting some data from database in .xls format but when i try to save my file it alerts me that file type is not .xls format. I need to store it in excel 97-2003 Workbook. when i pretend for save as option, default file type is in as tab delimited.. how can i overcome it?? below is my code..
if ( $_POST["frmDownload"] == "Excel") {
header("Content-disposition: attachment; filename=employee_details.xls");
header("Content-Type: application/vnd.ms-excel");
header("Content-type: application/x-msexcel");
}
use phpExcel which is excellent for all excel manipulation.
Here is the list of examples.
use the method fromArray to write excel from array which is fetched from persistent data.
use the method toArray to form a php array from excel sheet.
The learning curve is very simple first try first example only from official site.
Then try the fromArray and toArray its very easy and also phpExcel have lots of functionality like gradient background for a cell, border size increasing, formula manipulation and new worksheet also easy to create.
Important: read documentation on when ever you get free time
The format is tab delimited... You are just 'naming' it excel in this way... Look at gvgvgvijayan's answer for the solution to your problem.
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 tried to use phpexcel with my own template file. phpexcel loads the file and writes data to some cells A2, A3, A4 for example.. and opens an output file with the new data.
my template file has chart built-in.. all i want to phpexcel to do is to populate values in cells and don't touch the chart. And, open the new file. (Please note that I don't want to make the chart in code.. I want the chart to pre-exist with in my template in same format as I created originally). Only the data should update.
But, when i try to do this.. the chart itself goes missing from the resulting file. After trying various ways.. still failed.
And, i found the following code from http://phpexcel.codeplex.com/discussions/397263
require_once 'Classes/PHPExcel.php';
/** PHPExcel_IOFactory */
include 'Classes/PHPExcel/IOFactory.php';
$target ='Results/';
$fileType = 'Excel2007';
$InputFileName = $target.'Result.xlsx';
$OutputFileName = $target . '_Result.xlsx';
//Read the file (including chart template)
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objReader->setIncludeCharts(TRUE);
$objPHPExcel = $objReader->load($InputFileName);
//Change the file
$objPHPExcel->setActiveSheetIndex(0)
// Add data
->setCellValue('C3','10' )
->setCellValue('C4','20' )
->setCellValue('C5','30')
->setCellValue('C5','40' );
//Write the file (including chart)
PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
$objWriter->setIncludeCharts(TRUE);
$objWriter->save($OutputFileName);
The above code works in excel 2010 and now keeps my chart in tact... but still when I try to use filetype "Excel5" it doesn't work.
It throws the following error:
Fatal error: Call to undefined method PHPExcel_Reader_Excel5::setIncludeCharts()
in D:\IT\bfstools\PHPExcel\MyExamples\test1.php on line 16
Please provide a simple solution where I want my template file to work with .xls and .xlsx and all my original chart in the template file should stay intact. I do not want the chart removed it from the resulting file. Neither do I plan to create the chart using phpexcel code. (why write unnecessary code when excel can do all the work for you).
I want the easiest way out which is just to use everything with in my template and just populate cells with new data. And, my existing chart in the template comes live automatically. I don't want to write unnecessary code while I can safely rely on excel template and charting functions.
Please help.
There's a very good reason for this:
Charting is only implemented in core, and for the Excel2007 Readers and Writers at this point in time, so all of the other readers or writers will ignore charts, treat them as though they simply don't exist. The intention is to roll out charting to the other readers/writers over the coming year.
EDIT
I see from your comment that you don't understand how PHPExcel works at all, so I have a lot of explaining to do.
PHPExcel is not a library for "editing" workbook files: you're not using PHPExcel to change a file, you're changing a PHPExcel object that can be loaded from a file, and can subsequently be written to a file.
PHPExcel Core is an in-memory representation of the spreadsheet, with the different constituent objects such as worksheets, cells, images, styles, etc all represented as PHP Objects.
The PHPExcel Readers parse a spreadsheet file and load all the components from a file that they have been programmed to recognise, and create the appropriate PHPExcel core objects from those file components. If there is no equivalent PHPExcel Core object (such as Pivot Tables), then that file component can't be "loaded"; if the loader hasn't been programmed to recognise a file component, then it can't be loaded. In these cases, those elements from the file are simply ignored. Once the Reader has done it's job, a PHPExcel object exists, and the spreadsheet file is closed and forgotten.
When a PHPExcel Core object exists in memory, you have a set of methods allowing you to manipulate and change it, to add, modify or delete Core elements; but these work purely on the "in memory" collection of worksheet, cell, style objects that comprise the PHPExcel Core. The Core exists without knowledge of having been loaded from a file or having been created using a PHP "new PHPExcel()" statement; it makes no changes to files in any way.
When writing, the reverse is true. Each Writer takes the PHPExcel core objects, and writes them to a file in the appropriate format (Excel BIFF, OfficeOpenXML, HTML, etc). Like the Readers, each writer can only write those PHPExcel Core objects that it has been programmed to write. If it has not been programmed to write (for example, charts) then any charts defined in the PHPExcel Core will be ignored because that writer simply doesn't know how to write them yet. Likewise, features that exist in PHPExcel Core that are not supported by the file format that is being written to (such as cell styles for the CSV Writer) are ignored.
So to support a spreadsheet feature such as charts, it is necessary for the PHPExcel Core object collection to have been modified to provide an "in memory" representation of those elements, and for the different Readers to have been programmed to recognise those elements in the file they are loading and to convert them to the appropriate PHPExcel Core objects, and for the different Writers to have been programmed to convert the PHPExcel core representation to the appropriate file representation.
Each Reader and each Writer needs to be programmed individually. Charts is a relatively new feature, only added to the PHPExcel Core in the 1.7.7 release, and at this point only the Reader and Writer for the Excel2007 format have been programmed to recognise chart elements.
While it is the intention of the developers to extend this to cover the other formats as well, the necessary code isn't created automagically. Programming each individual Reader and Writer takes time and effort. While the Chart code for the Excel2007 Reader and Writer has now stabilised to the point where it is now no longer considered "experimental", and development focus is turning to writing the necessary code for chart handling in the Excel5 Reader and Writer, it is work that has not yet been completed.
If you can use Golang, try Excelize. Support save file without losing original charts of XLSX.
Try set setIncludeCharts
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
// Tell the reader to include charts when it loads a file
$objReader->setIncludeCharts(TRUE);
// Load the file
$objPHPExcel = $objReader->load($filePath);