473,395 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Is it possible to write to an Excel template via PHP?

Is it possible to open a multi-sheet Excel template which resides on the webserver, populate it with data from a MySql query then save the spreadsheet to another folder and name on the webserver? The only classes I can find will create a new spreadsheet and populate it then open it for the user. I have a 10 sheet workbook with a substantial amount of formatting and a tremendous number of formulas that I would rather not have to duplicate in PHP in order to write them to a blank sheet every time.

I have googled myself half to death looking for a solution but to no avail!
Thanks in advance!
Jan 23 '08 #1
7 7845
harshmaul
490 Expert 256MB
As long as you have the content dispoisiton working which it sounds like you have (as you can do a single work sheet, print out this xml using that.......

BTW i got this by saving an xls file as an xml file. so if the file is static then you can just use that xml as a templat and change whatever you want.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. <?xml version="1.0"?>
  4. <?mso-application progid="Excel.Sheet"?>
  5. <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
  6.  xmlns:o="urn:schemas-microsoft-com:office:office"
  7.  xmlns:x="urn:schemas-microsoft-com:office:excel"
  8.  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
  9.  xmlns:html="http://www.w3.org/TR/REC-html40">
  10.  <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
  11.   <Author>--Author name--</Author>
  12.   <LastAuthor>--last changed by--</LastAuthor>
  13.   <Created>2008-01-25T12:06:27Z</Created>
  14.   <LastSaved>2008-01-25T12:07:02Z</LastSaved>
  15.   <Company>--Company Name--</Company>
  16.   <Version>11.9999</Version>
  17.  </DocumentProperties>
  18.  <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
  19.   <WindowHeight>9150</WindowHeight>
  20.   <WindowWidth>11100</WindowWidth>
  21.   <WindowTopX>720</WindowTopX>
  22.   <WindowTopY>270</WindowTopY>
  23.   <ActiveSheet>1</ActiveSheet>
  24.   <ProtectStructure>False</ProtectStructure>
  25.   <ProtectWindows>False</ProtectWindows>
  26.  </ExcelWorkbook>
  27.  <Styles>
  28.   <Style ss:ID="Default" ss:Name="Normal">
  29.    <Alignment ss:Vertical="Bottom"/>
  30.    <Borders/>
  31.    <Font/>
  32.    <Interior/>
  33.    <NumberFormat/>
  34.    <Protection/>
  35.   </Style>
  36.  </Styles>
  37.  <Worksheet ss:Name="--WorkSheetName--">
  38.   <Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="2" x:FullColumns="1"
  39.    x:FullRows="1">
  40.    <Row>
  41.     <Cell><Data ss:Type="String">--cell content--</Data></Cell>
  42.     <Cell><Data ss:Type="String">--cell content--</Data></Cell>
  43.    </Row>
  44.    <Row>
  45.     <Cell><Data ss:Type="String">--cell content--</Data></Cell>
  46.     <Cell><Data ss:Type="String">--cell content--</Data></Cell>
  47.    </Row>
  48.   </Table>
  49.   <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
  50.    <Panes>
  51.     <Pane>
  52.      <Number>3</Number>
  53.      <ActiveRow>1</ActiveRow>
  54.      <ActiveCol>1</ActiveCol>
  55.     </Pane>
  56.    </Panes>
  57.    <ProtectObjects>False</ProtectObjects>
  58.    <ProtectScenarios>False</ProtectScenarios>
  59.   </WorksheetOptions>
  60.  </Worksheet>
  61.  <Worksheet ss:Name="--WorkSheetName--">
  62.   <Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="2" x:FullColumns="1"
  63.    x:FullRows="1">
  64.    <Row>
  65.     <Cell><Data ss:Type="Number">--cell content--</Data></Cell>
  66.     <Cell><Data ss:Type="Number">--cell content--</Data></Cell>
  67.    </Row>
  68.    <Row>
  69.     <Cell><Data ss:Type="Number">--cell content--</Data></Cell>
  70.     <Cell><Data ss:Type="Number">--cell content--</Data></Cell>
  71.    </Row>
  72.   </Table>
  73.   <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
  74.    <Selected/>
  75.    <ProtectObjects>False</ProtectObjects>
  76.    <ProtectScenarios>False</ProtectScenarios>
  77.   </WorksheetOptions>
  78.  </Worksheet>
  79. </Workbook>
  80.  
  81.  
  82.  
Jan 25 '08 #2
Thanks for the reply. Will this work for multi-sheet workbooks too? Would you try to pass PHP variables into the template or write the XML dynamically, inserting values as you go? I am still relatively new to programming and have no experience at all with XML. When I save my workbook to xml, I get about 4500 lines of code. I have been searching for the syntax to insert PHP variables into the XML code and open the resulting document as a workbook but everything I try seems to fail and the data displays in the browser window as raw xml. Any handholding you'd be willing to offer would be greatly appreciated :-)

Thank You


As long as you have the content dispoisiton working which it sounds like you have (as you can do a single work sheet, print out this xml using that.......

BTW i got this by saving an xls file as an xml file. so if the file is static then you can just use that xml as a templat and change whatever you want.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. <?xml version="1.0"?>
  4. <?mso-application progid="Excel.Sheet"?>
  5. <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
  6.  xmlns:o="urn:schemas-microsoft-com:office:office"
  7.  xmlns:x="urn:schemas-microsoft-com:office:excel"
  8.  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
  9.  xmlns:html="http://www.w3.org/TR/REC-html40">
  10.  <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
  11.   <Author>--Author name--</Author>
  12.   <LastAuthor>--last changed by--</LastAuthor>
  13.   <Created>2008-01-25T12:06:27Z</Created>
  14.   <LastSaved>2008-01-25T12:07:02Z</LastSaved>
  15.   <Company>--Company Name--</Company>
  16.   <Version>11.9999</Version>
  17.  </DocumentProperties>
  18.  <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
  19.   <WindowHeight>9150</WindowHeight>
  20.   <WindowWidth>11100</WindowWidth>
  21.   <WindowTopX>720</WindowTopX>
  22.   <WindowTopY>270</WindowTopY>
  23.   <ActiveSheet>1</ActiveSheet>
  24.   <ProtectStructure>False</ProtectStructure>
  25.   <ProtectWindows>False</ProtectWindows>
  26.  </ExcelWorkbook>
  27.  <Styles>
  28.   <Style ss:ID="Default" ss:Name="Normal">
  29.    <Alignment ss:Vertical="Bottom"/>
  30.    <Borders/>
  31.    <Font/>
  32.    <Interior/>
  33.    <NumberFormat/>
  34.    <Protection/>
  35.   </Style>
  36.  </Styles>
  37.  <Worksheet ss:Name="--WorkSheetName--">
  38.   <Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="2" x:FullColumns="1"
  39.    x:FullRows="1">
  40.    <Row>
  41.     <Cell><Data ss:Type="String">--cell content--</Data></Cell>
  42.     <Cell><Data ss:Type="String">--cell content--</Data></Cell>
  43.    </Row>
  44.    <Row>
  45.     <Cell><Data ss:Type="String">--cell content--</Data></Cell>
  46.     <Cell><Data ss:Type="String">--cell content--</Data></Cell>
  47.    </Row>
  48.   </Table>
  49.   <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
  50.    <Panes>
  51.     <Pane>
  52.      <Number>3</Number>
  53.      <ActiveRow>1</ActiveRow>
  54.      <ActiveCol>1</ActiveCol>
  55.     </Pane>
  56.    </Panes>
  57.    <ProtectObjects>False</ProtectObjects>
  58.    <ProtectScenarios>False</ProtectScenarios>
  59.   </WorksheetOptions>
  60.  </Worksheet>
  61.  <Worksheet ss:Name="--WorkSheetName--">
  62.   <Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="2" x:FullColumns="1"
  63.    x:FullRows="1">
  64.    <Row>
  65.     <Cell><Data ss:Type="Number">--cell content--</Data></Cell>
  66.     <Cell><Data ss:Type="Number">--cell content--</Data></Cell>
  67.    </Row>
  68.    <Row>
  69.     <Cell><Data ss:Type="Number">--cell content--</Data></Cell>
  70.     <Cell><Data ss:Type="Number">--cell content--</Data></Cell>
  71.    </Row>
  72.   </Table>
  73.   <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
  74.    <Selected/>
  75.    <ProtectObjects>False</ProtectObjects>
  76.    <ProtectScenarios>False</ProtectScenarios>
  77.   </WorksheetOptions>
  78.  </Worksheet>
  79. </Workbook>
  80.  
  81.  
  82.  
Jan 29 '08 #3
Thanks for the reply. Will this work for multi-sheet workbooks too? Would you try to pass PHP variables into the template or write the XML dynamically, inserting values as you go? I am still relatively new to programming and have no experience at all with XML. When I save my workbook to xml, I get about 4500 lines of code. I have been searching for the syntax to insert PHP variables into the XML code and open the resulting document as a workbook but everything I try seems to fail and the data displays in the browser window as raw xml. Any handholding you'd be willing to offer would be greatly appreciated :-)

Thank You


As long as you have the content dispoisiton working which it sounds like you have (as you can do a single work sheet, print out this xml using that.......

BTW i got this by saving an xls file as an xml file. so if the file is static then you can just use that xml as a templat and change whatever you want.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. <?xml version="1.0"?>
  4. <?mso-application progid="Excel.Sheet"?>
  5. <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
  6.  xmlns:o="urn:schemas-microsoft-com:office:office"
  7.  xmlns:x="urn:schemas-microsoft-com:office:excel"
  8.  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
  9.  xmlns:html="http://www.w3.org/TR/REC-html40">
  10.  <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
  11.   <Author>--Author name--</Author>
  12.   <LastAuthor>--last changed by--</LastAuthor>
  13.   <Created>2008-01-25T12:06:27Z</Created>
  14.   <LastSaved>2008-01-25T12:07:02Z</LastSaved>
  15.   <Company>--Company Name--</Company>
  16.   <Version>11.9999</Version>
  17.  </DocumentProperties>
  18.  <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
  19.   <WindowHeight>9150</WindowHeight>
  20.   <WindowWidth>11100</WindowWidth>
  21.   <WindowTopX>720</WindowTopX>
  22.   <WindowTopY>270</WindowTopY>
  23.   <ActiveSheet>1</ActiveSheet>
  24.   <ProtectStructure>False</ProtectStructure>
  25.   <ProtectWindows>False</ProtectWindows>
  26.  </ExcelWorkbook>
  27.  <Styles>
  28.   <Style ss:ID="Default" ss:Name="Normal">
  29.    <Alignment ss:Vertical="Bottom"/>
  30.    <Borders/>
  31.    <Font/>
  32.    <Interior/>
  33.    <NumberFormat/>
  34.    <Protection/>
  35.   </Style>
  36.  </Styles>
  37.  <Worksheet ss:Name="--WorkSheetName--">
  38.   <Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="2" x:FullColumns="1"
  39.    x:FullRows="1">
  40.    <Row>
  41.     <Cell><Data ss:Type="String">--cell content--</Data></Cell>
  42.     <Cell><Data ss:Type="String">--cell content--</Data></Cell>
  43.    </Row>
  44.    <Row>
  45.     <Cell><Data ss:Type="String">--cell content--</Data></Cell>
  46.     <Cell><Data ss:Type="String">--cell content--</Data></Cell>
  47.    </Row>
  48.   </Table>
  49.   <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
  50.    <Panes>
  51.     <Pane>
  52.      <Number>3</Number>
  53.      <ActiveRow>1</ActiveRow>
  54.      <ActiveCol>1</ActiveCol>
  55.     </Pane>
  56.    </Panes>
  57.    <ProtectObjects>False</ProtectObjects>
  58.    <ProtectScenarios>False</ProtectScenarios>
  59.   </WorksheetOptions>
  60.  </Worksheet>
  61.  <Worksheet ss:Name="--WorkSheetName--">
  62.   <Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="2" x:FullColumns="1"
  63.    x:FullRows="1">
  64.    <Row>
  65.     <Cell><Data ss:Type="Number">--cell content--</Data></Cell>
  66.     <Cell><Data ss:Type="Number">--cell content--</Data></Cell>
  67.    </Row>
  68.    <Row>
  69.     <Cell><Data ss:Type="Number">--cell content--</Data></Cell>
  70.     <Cell><Data ss:Type="Number">--cell content--</Data></Cell>
  71.    </Row>
  72.   </Table>
  73.   <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
  74.    <Selected/>
  75.    <ProtectObjects>False</ProtectObjects>
  76.    <ProtectScenarios>False</ProtectScenarios>
  77.   </WorksheetOptions>
  78.  </Worksheet>
  79. </Workbook>
  80.  
  81.  
  82.  
Jan 29 '08 #4
lets try this again----- Thanks for the reply. Will this technique work with a multi-sheet workbook as well? How would you go about inserting the variables; writing the XML line by line from PHP or passing variables to an XML page and echoing them? I have tried saving my workbook as XML and uploading to my webserver but it displays in the browser as raw text as opposed to opening up Excel. It is about 4500 lines of code if that matters. I have little to no experience working with XML so any hand-holding you are willing to offer would be much appreciated.

Thanks,
Matthew
Jan 29 '08 #5
harshmaul
490 Expert 256MB
Basically you need to use "content disposition". (i.e modifying some of the headers).

Start with the below code and we can move forward from there...

You can see it work here

and heres the code...

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. header("Content-type: application/msexcel");
  3. header("Content-Disposition: attachment; filename=worddoc.xml");
  4. echo "<?xml version=\"1.0\"?>";?>
  5. <?php
  6. echo "<?mso-application progid=\"Excel.Sheet\"?>";
  7. ?> 
  8. <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
  9.  xmlns:o="urn:schemas-microsoft-com:office:office"
  10.  xmlns:x="urn:schemas-microsoft-com:office:excel"
  11.  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
  12.  xmlns:html="http://www.w3.org/TR/REC-html40">
  13.  <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
  14.   <Author>harjits</Author>
  15.   <LastAuthor>harjits</LastAuthor>
  16.   <Created>2008-01-25T12:06:27Z</Created>
  17.   <LastSaved>2008-01-25T12:07:02Z</LastSaved>
  18.   <Company>Coders Advocate</Company>
  19.   <Version>11.9999</Version>
  20.  </DocumentProperties>
  21.  <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
  22.   <WindowHeight>9150</WindowHeight>
  23.   <WindowWidth>11100</WindowWidth>
  24.   <WindowTopX>720</WindowTopX>
  25.   <WindowTopY>270</WindowTopY>
  26.   <ActiveSheet>1</ActiveSheet>
  27.   <ProtectStructure>False</ProtectStructure>
  28.   <ProtectWindows>False</ProtectWindows>
  29.  </ExcelWorkbook>
  30.  <Styles>
  31.   <Style ss:ID="Default" ss:Name="Normal">
  32.    <Alignment ss:Vertical="Bottom"/>
  33.    <Borders/>
  34.    <Font/>
  35.    <Interior/>
  36.    <NumberFormat/>
  37.    <Protection/>
  38.   </Style>
  39.  </Styles>
  40.  <Worksheet ss:Name="1st Sheet">
  41.   <Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="2" x:FullColumns="1"
  42.    x:FullRows="1">
  43.    <Row>
  44.     <Cell><Data ss:Type="String">A</Data></Cell>
  45.     <Cell><Data ss:Type="String">B</Data></Cell>
  46.    </Row>
  47.    <Row>
  48.     <Cell><Data ss:Type="String">C</Data></Cell>
  49.     <Cell><Data ss:Type="String">D</Data></Cell>
  50.    </Row>
  51.   </Table>
  52.   <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
  53.    <Panes>
  54.     <Pane>
  55.      <Number>3</Number>
  56.      <ActiveRow>1</ActiveRow>
  57.      <ActiveCol>1</ActiveCol>
  58.     </Pane>
  59.    </Panes>
  60.    <ProtectObjects>False</ProtectObjects>
  61.    <ProtectScenarios>False</ProtectScenarios>
  62.   </WorksheetOptions>
  63.  </Worksheet>
  64.  <Worksheet ss:Name="2nd Sheet">
  65.   <Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="2" x:FullColumns="1"
  66.    x:FullRows="1">
  67.    <Row>
  68.     <Cell><Data ss:Type="Number">1</Data></Cell>
  69.     <Cell><Data ss:Type="Number">2</Data></Cell>
  70.    </Row>
  71.    <Row>
  72.     <Cell><Data ss:Type="Number">3</Data></Cell>
  73.     <Cell><Data ss:Type="Number">4</Data></Cell>
  74.    </Row>
  75.   </Table>
  76.   <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
  77.    <Selected/>
  78.    <ProtectObjects>False</ProtectObjects>
  79.    <ProtectScenarios>False</ProtectScenarios>
  80.   </WorksheetOptions>
  81.  </Worksheet>
  82. </Workbook>
Jan 29 '08 #6
You're the best! Got it working!!! Thanks a ton!
Jan 30 '08 #7
harshmaul
490 Expert 256MB
Your gratitude is appreciated
Jan 30 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Axial | last post by:
Question: How to select columns from Excel-generated XML when some cells are empty. I've found examples where rows are to be selected, but I can't seem to extrapolate from that to selecting...
4
by: HG | last post by:
Hi gurus Sorry the rather long post... I am facing a real world problem here, and I dunno how to approach it. Don't even know if this is the right group...but since my app is ASP.NET I tried...
1
by: Bon | last post by:
Hello all I create a form with three buttons in MS Access 2000. They are Open Excel Template, Save Draft and Save Final. When I click the Open Excel Template button, the Excel template will be...
3
by: sandeshmeda | last post by:
I have a XML file that is basically a Excel file saved as XML. I need to be able to change the XML into a different format. I was thinking one approach would be to: 1. Populate the dataset...
1
by: ram venkat | last post by:
Hi, Im new to Perl programming and Im facing a problem while trying to write data into the excel template. Actually the data gets appended to the template (the file size increases), but while...
7
by: Stephenoja | last post by:
Hello Guys, I have a challenge here and would really appreciate some help. I have customer bills that are all done in excel with a template and stored in directories by month order. At the end...
4
by: ShadowLocke | last post by:
Hi, Im not very experienced with XSL, this is my first run in with it. I found an XSL file that converts a dataset to an excel readable format. The problem I have is when the dataset has a...
13
by: Peter | last post by:
VS2008 ans ASP.NET 3.5, Office 2003 What is the best way to run Excel Template from ASP.NET web page were the Excel is only installed on the client without any ActiveX? If so can someone point...
8
by: K Viltersten | last post by:
I understand that the new versions of MS Word, MS Excel etc. allow for creation of a document using XML tags (the technique or format is called OOXML, i think). I imagine that the idea behind...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.