need to create xsl file with name and had different sheet name

Topics: Developer Forum, Project Management Forum, User Forum
Feb 5, 2011 at 4:26 AM

i need to create a excel file from php. When i create the file by default the file name and the sheet name are same.

My requirnment is that i want different sheet name from file name

eg (for what i am getting):

if filename:"Report_For_01_05_11.xsl"



eg (What i need):

if filename:"Report_For_01_05_11.xsl"



if anybody can help me


Thanks in advanve


Rajveer singh jhala

Feb 5, 2011 at 9:07 AM

I think that's it's actually the workbook filename you're referring to, and it doesn't have any default. It will be called whatever name you specify when you save.... and it shouldn't normally have an xsl extension... that's usually used for a totally different type of file.

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('Whatever name you want to give your workbook file.xls');

A worksheet name (or title) is the name displayed on the tab for that worksheet when you open it in Excel. This defaults to 'Worksheet' (or 'Worksheet1' if you add more worksheets to your workbook).

$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle('This is Worksheet #1');
$objPHPExcel->createSheet();  // Create a new sheet and set it as the active sheet
$objPHPExcel->getActiveSheet()->setTitle('This is Worksheet #2');
Feb 8, 2011 at 8:33 AM

thanks for reply

that really helps me

but i am using the below code and getting following error


$objPHPexcel = PHPExcel_IOFactory::load($file);
$objWorksheet = $objPHPexcel->setActiveSheetIndex(0);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPexcel, 'Excel2007');


Fatal error: Uncaught exception 'Exception' with message 'Could not open 'path of file'  in /home/gymflex/public_html/classes/PHPExcel/Writer/Excel2007.php:232

when i replace 'Excel2007'  with 'Excel5' the file is generated but the data with <table><tr><td>Number</td>.............. like present although it raplaces the worksheet name.


can you suggest me any thing about this?



Feb 8, 2011 at 8:44 AM

Is the value of $file a string containing 'path of file' ?


What type of file are you actually loading?

<table><tr><td>Number</td>.............. looks like HTML markup, not like any flavour of Excel file that I've ever come across.

Feb 8, 2011 at 12:29 PM

1. Yes $file contain complete path of the file

2. The file i am loding a excel file name="GymFlexUpload20110207.xls"


Feb 8, 2011 at 12:49 PM
rajveer wrote:

1. Yes $file contain complete path of the file

I have absolutely no idea what is happening here.  The error message should be displaying the value of $file, and instead it is displaying the string 'path of file'. I'm at a complete loss to explain how the filename could be changed in this way within the save() method.


rajveer wrote:

2. The file i am loding a excel file name="GymFlexUpload20110207.xls"

 Is it possible for you to upload a copy of this file to the upload link here so that I can run some tests.

Feb 9, 2011 at 8:28 AM
Edited Feb 9, 2011 at 8:29 AM

The file that you are trying to load is not an Excel file... it's an HTML file that has had the extension changed from .html to .xls If you open it in a text editor, you can see this for yourself.

This is a common method used to create "Excel" files by people who don't realise that .xls is not an html markup file but a binary file. They are fortunate that MS Excel can actually read html markup.

When you use the PHPExcel_IOFactory::load($file) method, PHPExcel attempts to identify the actual type of file based on its content (rather than its extension), and use the appropriate Reader. The default behaviour (if it does not match any of the recognised file types) is to treat it as a CSV file, and use the CSV Reader, which explains why you are getting content like "<table><tr><td>Number</td>..............." in the cells.

PHPExcel does not currently have an HTML Reader (I have done some work on this, but it isn't yet accurate enough to include in the library).