2

At present I'm storing data in a variable which contains HTML table. I got this idea from How to format excel file with styles, fonts, colors, tables etc with pure PHP?

$table = '<table>';
$table .= '<tr>';
$table .= '<td>1st row, 1st cell</td>';
$table .= '<td>1st row, 2nd cell</td>';
$table .= '</tr>';
$table .= '<tr>';
$table .= '<td>2nd row, 1st cell</td>';
$table .= '<td>2nd row, 2nd cell</td>';
$table .= '</tr>';
$table .= '</table>';
downloadAsExcel($table, 'somefilename');

function downloadAsExcel($html, $filename)
{
    header("Content-Type: application/excel");
    header("Content-Disposition: attachment; filename=".ucwords($filename)."-Data.xls");
    header("Pragma: no-cache");
    header("Expires: 0");
    echo $html
}

This is working fine and the output is as expected. However, I was wondering if I could create multiple sheets in a single excel file using a similar approach.

It would be wonderful if it could be done as I don't want to use a library to do the job. I've looked at PHPExcel, ExcelReader, DocRaptor etc and so I'm looking for answers (if at all it is possible) which don't use such libraries.

UPDATE : Almost forgot to mention - I also came across this http://webcheatsheet.com/php/create_word_excel_csv_files_with_php.php#excelcom but again this requires me to have MS Excel installed on the server, which I can't as it's a shared hosting account.

PS: I'm fine with the warning dialog that appears while opening an Excel file created by the above method.

Community
  • 1
  • 1
asprin
  • 9,579
  • 12
  • 66
  • 119
  • Perfectly possible, and there's dozens of libraries available to help you do it... but HTML !== XLS and MS Excel's HTML import will only allow a single worksheet.... to get multiple worksheets, you need to create a __real__ xls or xlsx file – Mark Baker May 22 '13 at 10:28
  • 1
    So why don't you use one of those libraries? – Mark Baker May 22 '13 at 10:28
  • PS. `application/excel` is not a valid mime type: `application/vnd.ms-excel` for xls files, `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` for xlsx files – Mark Baker May 22 '13 at 10:30
  • Yes, I realize that but my priority here is to have multiple worksheets in a single file. About not using libraries, I want to create the application using my own code. I don't want to depend on outside factors. – asprin May 22 '13 at 10:32
  • Lol Mark Baker you're always the first to answer the `excel`-tagged questions... Do you have it on some RSS feed or something? :) – silkfire May 22 '13 at 10:32
  • @asprin That doesn't make sense. It's like saying I don't want to buy a cell phone and building one yourself instead. Good luck with that. Not exactly smart, imho. – silkfire May 22 '13 at 10:34
  • @silkfire Smart or not, I would like to stick to my decision :) – asprin May 22 '13 at 10:35
  • @aspirin - You'll never be able to create a multisheet workbook __without__ using a true spreadsheet file format, whether BIFF (xls), OfficeOpenXML (xlsx), Gnumeric, Open Document Format or SpreadsheetML... writing any of those yourself is non-trivial. SpreadsheetML is probably the easiest to "do it yourself" – Mark Baker May 22 '13 at 10:48
  • Aww...so I guess going for a third party library is the only solution then. Creating from scratch is not my cup of tea. – asprin May 22 '13 at 10:52
  • I've commented a complete set of format resources in response to Vivasaayi's answer if you do want to do it yourself.... as I say, SpreadsheetML is the easiest format to work with quickly if you do want to write your own.... but it really isn't a simple task (believe me, I know)... but if you do decide to go ahead and do it yourself, I'd be interested in following your progress – Mark Baker May 22 '13 at 10:55
  • Nah, I will stick to the ready-made libraries. Time is a constraint in my case. Thanks for the inputs though. – asprin May 22 '13 at 11:01

2 Answers2

0

You can create the file from scratch - for which you should know the file format and since office 2007 this standard is open.

You can refere http://en.wikipedia.org/wiki/Office_Open_XML to get started!

Rajan Panneer Selvam
  • 1,279
  • 10
  • 24
  • 1
    http://msdn.microsoft.com/en-us/library/cc313154(v=office.12).aspx for .xls files; http://msdn.microsoft.com/en-us/library/aa140066%28office.10%29.aspx for SpreadsheetML; http://www.ecma-international.org/news/TC45_current_work/TC45_available_docs.htm for OfficeOpenXML; http://www.oasis-open.org/committees/tc_home.php?wg_abbrev=office#technical for OpenDocumentFormat (OASIS); http://projects.gnome.org/gnumeric/doc/file-format-gnumeric.shtml for Gnumeric – Mark Baker May 22 '13 at 10:52
-1

Why cant you go for inbuilt class PHPExcel

(i.e) $objPHPExcel = new PHPExcel()

Please go through the following link for that,

Creating Multiple Excel Sheets Using PHP

Community
  • 1
  • 1
Vinoth Babu
  • 6,724
  • 10
  • 36
  • 55
  • Then you can go for this. If not can you please tell me the reason for that. – Vinoth Babu May 22 '13 at 10:38
  • Just my personal choice - do not want to depend on third party libraries. – asprin May 22 '13 at 10:42
  • Thats fine. Then you have to create by your own, as you did that may take more time to create the code and for execution also. IF you use inbuit one then execution time will be less only. – Vinoth Babu May 22 '13 at 10:43
  • What do you mean with `inbuilt` class? PHPExcel is a part of [PHPOffice](https://github.com/PHPOffice/PHPExcel), which is just ordinary PHP code that doesn't take any more advantage as any other ordinary code you write in PHP. – dbf May 28 '13 at 21:26