23
2009
Excel Creation – Multiple worksheets in php
After spending few days i find a format to generate excel with multiple work sheets.
Based on that format , I created my own functions for creating excel with multiple work sheets with out using any third party tool.
Creating Header for excel
function header_excel(){
$header = <<<EOH
<?xml version=â€1.0″?>
<?mso-application progid=â€Excel.Sheetâ€?>
<Workbook xmlns=â€urn:schemas-microsoft-com:office:spreadsheetâ€
xmlns:o=â€urn:schemas-microsoft-com:office:officeâ€
xmlns:x=â€urn:schemas-microsoft-com:office:excelâ€
xmlns:ss=â€urn:schemas-microsoft-com:office:spreadsheetâ€
xmlns:html=â€http://www.w3.org/TR/REC-html40″>
<DocumentProperties xmlns=â€urn:schemas-microsoft-com:office:officeâ€>
<Author>Author Name</Author>
<LastAuthor>Author Name</LastAuthor>
<Created>Created Date</Created>
<Company>Company Name</Company>
<Version>Version</Version>
</DocumentProperties>
<ExcelWorkbook xmlns=â€urn:schemas-microsoft-com:office:excelâ€>
<WindowHeight>12240</WindowHeight>
<WindowWidth>24855</WindowWidth>
<WindowTopX>240</WindowTopX>
<WindowTopY>105</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID=â€Default†ss:Name=â€Normalâ€>
<Alignment ss:Vertical=â€Bottomâ€/>
<Borders/>
<Font ss:FontName=â€Calibri†x:Family=â€Swiss†ss:Size=â€11″ ss:Color=â€#000000″/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID=â€s18″ ss:Name=â€Currencyâ€>
<NumberFormat
ss:Format=â€_(â€$â€* #,##0.00_);_(â€$â€* \(#,##0.00\);_(â€$â€* “-â€??_);_(@_)â€/>
</Style>
<Style ss:ID=â€s66″>
<Font ss:FontName=â€Calibri†x:Family=â€Swiss†ss:Size=â€11″ ss:Color=â€#000000″/>
</Style>
<Style ss:ID=â€s68″ ss:Parent=â€s18″>
<Font ss:FontName=â€Calibri†x:Family=â€Swiss†ss:Size=â€11″ ss:Color=â€#000000″/>
</Style>
<Style ss:ID=â€s69″>
<Alignment ss:Horizontal=â€Center†ss:Vertical=â€Bottomâ€/>
<Font ss:FontName=â€Calibri†x:Family=â€Swiss†ss:Size=â€11″ ss:Color=â€#000000″
ss:Bold=â€1″/>
</Style>
<Style ss:ID=â€s70″ ss:Parent=â€s18″>
<Alignment ss:Horizontal=â€Center†ss:Vertical=â€Bottomâ€/>
<Font ss:FontName=â€Calibri†x:Family=â€Swiss†ss:Size=â€11″ ss:Color=â€#000000″
ss:Bold=â€1″/>
</Style>
<Style ss:ID=â€s73″>
<Alignment ss:Horizontal=â€Center†ss:Vertical=â€Bottomâ€/>
<Font ss:FontName=â€Calibri†x:Family=â€Swiss†ss:Size=â€11″ ss:Color=â€#000000″/>
</Style>
</Styles>
EOH;
return $header;
}
2. By using the below method we can able to add work sheets
$sheetname – Name of the worksheet
function add_excel_worksheet($sheetname) {
$worksheet_name = ‘<Worksheet ss:Name=â€â€˜.$sheetname.’â€>’;
return $worksheet_name;
}
3. This Method is used to Add the content for the particular work sheet
$cnt,$cnt1 – total number of row and column.
function formatDataHeader($cnt,$cnt1) {
$table_header = ‘<Table ss:ExpandedColumnCount=â€â€˜.$cnt1.’†ss:ExpandedRowCount=â€â€˜.$cnt.’†x:FullColumns=â€1″
x:FullRows=â€1″ ss:StyleID=â€s66″ ss:DefaultRowHeight=â€15″>
<Column ss:StyleID=â€s73″ ss:AutoFitWidth=â€1″ ss:Width=â€200″/>
<Column ss:StyleID=â€s66″ ss:AutoFitWidth=â€1″ ss:Width=â€135″/>
<Column ss:StyleID=â€s68″ ss:AutoFitWidth=â€1″ ss:Width=â€82.5″/>’;
/*$table_row = ‘<Row ss:StyleID=â€s69″>’;
$column_cnt = count($headers);
$column_cont = “â€;
for($i=0;$i<$column_cnt;$i++) {
$column_cont .= ‘<Cell><Data ss:Type=â€Stringâ€>’.$headers[$i].’</Data></Cell>’;
}
$table_row_end =â€</Row>â€;
$table = $table_header.$table_row.$column_cnt.$table_row_end;*/
return $table_header;
}
function formatDataContent($contents) {
$table_row = ‘<Row ss:StyleID=â€s69″>’;
$column_cnt = count($contents);
$column_cont = “â€;
for($i=0;$i<$column_cnt;$i++) {
$column_cont .= ‘<Cell><Data ss:Type=â€Stringâ€>’.$contents[$i].’</Data></Cell>’;
}
$table_row_end =â€</Row>â€;
$table_cont= $table_row.$column_cont.$table_row_end;
return $table_cont;
}
4. This is used to close content tag for particular worksheet.
function closetableNode() {
return “</Table>â€;
}
5. The below method is used to close the worksheet.
function close_excel_worksheet() {
$worksheet_close = ‘<WorksheetOptions xmlns=â€urn:schemas-microsoft-com:office:excelâ€>
<PageSetup>
<Header x:Margin=â€0.3″/>
<Footer x:Margin=â€0.3″/>
<PageMargins x:Bottom=â€0.75″ x:Left=â€0.7″ x:Right=â€0.7″ x:Top=â€0.75″/>
</PageSetup>
<Print>
<ValidPrinterInfo/>
<HorizontalResolution>600</HorizontalResolution>
<VerticalResolution>0</VerticalResolution>
</Print>
<Selected/>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>’;
return $worksheet_close;
}
5. The below method is used to close the workbook.
function close_workbook() {
$workbook_close = “</Workbook>â€;
return $workbook_close;
}

An article by admin




